#!/bin/sh
# this extracts the given archive.
# It detects gzip, bzip2, zip, tar, etc.
# TODO attempt to detect exploding tarballs
# (ones that don't create a subdirectory and spew
# files all over the current directory).
# tar tzf tarball.tar.gz | awk -F '/' '{print $1}' | uniq | wc -l
# A well behaved tarball should only create one directory or file
# in the current directory.

OPT='xf'
FILE=$1
case "${FILE}" in
    *.zip)     unzip -lv "${FILE}" ;;
    *.tgz)     gunzip -c "${FILE}" | tar $OPT - ;;
    *.bz2)     bunzip2 -c "${FILE}" | tar $OPT - ;;
    *.tar.gz)  gunzip -c "${FILE}" | tar $OPT - ;;
    *.tar.bz2) bunzip2 -c "${FILE}" | tar $OPT - ;;
    *.tar.Z)   uncompress -c "${FILE}" | tar $OPT - ;;
    *.rar)
        UNRAR=`which unrar`
        if $?
        then
            unrar-free -x "${FILE}"
        else
            unrar x "${FILE}"
        fi
        ;;
    *)         tar $OPT ${FILE} ;;
esac

