#!/bin/bash -e

unpme()
{
	local packer;
	local i;
	packer="$1";
	shift;
	for i in "$@"; do
		if [ ! -f "$i" ]; then
			continue;
		fi;
		echo "$i";
		tar -C xusrc/ "${packer:+--use=$packer}" -xf "$i" || :;
	done;
}

if [ "$#" -eq 0 ]; then
	set -- *;
fi;
for i in "$@"; do
	if [ ! -d "$i" ]; then
		continue;
	fi;
	pushd "$i" >/dev/null;
	echo "====> $i";
	mkdir -p xusrc;
	if [ -z "$SKIP_TAR" ]; then
		unpme "" *.{tar,tar.gz,tgz};
		unpme pbzip2 *.{tar.bz2,tbz,tbz2,tar.bz};
		unpme xz *.tar.lzma;
		unpme pixz *.{tar.xz,txz};
		unpme plzip *.{tar.lz,tlz};
	fi;
	if [ -z "$SKIP_ZIP" ]; then
		for i in *.zip; do
			if [ ! -e "$i" ]; then
				continue;
			fi;
			echo "$i";
			pushd xusrc/ >/dev/null;
			unzip -q -o "../$i" || :;
			popd >/dev/null;
		done;
	fi;
	popd >/dev/null;
done 2>&1 | less -S
