#!/bin/sh

# This extracts the given archive. It detects gzip, bzip2, zip, tar, etc.
#
# TODO This should attempt to detect exploding archives.
# These are archives that don't specify a subdirectory, so when
# they are extracted they spew files all over the current directory.
#
#     tar tzf tarball.tar.gz | awk -F '/' '{print $1}' | uniq | wc -l
#
# A well behaved archive should only create one directory or
# one file in the current directory.
#
# Noah Spurrier 2010-04-16 23:35:46-07:00
#
OPT='xf'
for FILENAME in "$@"; do
	case "${FILENAME}" in
	*.tar.Z)   uncompress -c "${FILENAME}" | tar ${OPT} - ;;
	*.tar.bz2) bunzip2 -c "${FILENAME}" | tar ${OPT} - ;;
	*.tar.gz)  gunzip -c "${FILENAME}" | tar ${OPT} - ;;
	*.tbz2)    bunzip2 -c "${FILENAME}" | tar ${OPT} - ;;
	*.tgz)     gunzip -c "${FILENAME}" | tar ${OPT} - ;;
	*.tZ)      uncompress -c "${FILENAME}" | tar ${OPT} - ;;
	*.Z)       uncompress "${FILENAME}" ;;
	*.bz2)     bunzip2 "${FILENAME}" ;;
	*.gz)      gunzip "${FILENAME}" ;;
	*.tar)     tar ${OPT} ${FILENAME} ;;
	*.zip)
		mkdir -p ${FILENAME%.zip}
		cd ${FILENAME%.zip}
		unzip ../${FILENAME}
		cd ..
	;;
	*.7z)      7z x "${FILENAME}" ;;
	*.rar)
		if type unrar >/dev/null; then
			unrar x "${FILENAME}"
		else
			unrar-free -x "${FILENAME}"
		fi
	;;
	*)      echo "ERROR: Could not find a tool to uncompress ${FILENAME}." >&2
		exit 1
	;;
	esac
done

exit 0
