#!/bin/sh
# <BEGIN COPYRIGHT>
# This file is part of the "Bad Penguin" GNU/Linux distribution
# Copyright (C) 1997-98-99 by Antonio GALLO
# Author of this file: Antonio GALLO aka AGX
# Home Page at ......: http://www.badpenguin.org
# Home Page at ......: http://www.linux.it/~agx/
# e-mail contact ....: mailto:agx@linux.it	mailto:agx@geocities.com
# This program is distributed under the GNU General Public License
# You are not allowed to remove the copyright notice
# <END COPYRIGHT>

# UN-TAR Extract files from a (tar.gz) file, in the way i usually did.
# V. 1.000 by AGX <agx@linux.it>
# V. 1.100 AGX add lines from a mail of Roberto Oppedisano <hope@delete-me.sis.it>
# V. 1.6.0 by AGX 1999-11-20

# Usage
case $1 in
  ""|"-h"|--help)
      echo
      echoc PURPLE "untar - uncompression utility for Bad Penguin"
      echoc GREEN  "usage: untar file_name [target_dir]"
      echo
      exit 1
  ;;
esac
SOURCEFILE="$1"
TARGETDIR="$2"


# Source archive exists ?
if [ ! -f "$SOURCEFILE" -o "$SOURCEFILE" = "" ]; then
  echoc RED "untar: file $SOURCEFILE non trovato !"
  exit 2
fi

# Prepare target dir
SOURCEDIR="`pwd`"
[ "$TARGETDIR" = "" ] && TARGETDIR="$SOURCEDIR"
if [ ! -d $2 ]; then
  mkdir  -p  "$TARGETDIR"
fi

OLDPWD=`pwd`
cd "$TARGETDIR"
if [ ! -f "$SOURCEFILE" ]; then
  SOURCEFILE="${OLDPWD}/${SOURCEFILE}"
fi


# Extract
case $SOURCEFILE in
    *tar.gz|*tgz|*bpp)  tar   -zxvf  "$SOURCEFILE" ;;
    *tar.Z|*taz)        tar   -Zxvf  "$SOURCEFILE" ;;
    *tar.bz)            bzip  -cd    "$SOURCEFILE"  | tar xvf - ;;
    *tar.bz2)           bzip2 -cd    "$SOURCEFILE"  | tar xvf - ;;
    *zip)               unzip -L     "$SOURCEFILE" ;;
    *tar)               tar   -xvf   "$SOURCEFILE" ;;
    *deb)
      if dpkg-deb --version 1>/dev/null 2>/dev/null
      then
        echo "Uso dpkg-deb per scompattare l'archivio Debian ..."
	   mkdir  -p  "$TARGETDIR/DEBIAN"
	dpkg-deb  -X  "$SOURCEFILE"  "$TARGETDIR"
	dpkg-deb  -e  "$SOURCEFILE"  "$TARGETDIR"
      else
        echo "Uso alien per scompattare l'archivio Debian ..."
	alien -t -c -g  "$SOURCEFILE"
      fi
      ;;

    *.rpm) alien -t -c -g "$SOURCEFILE" ;;
	# rpm2targz ???    
    *) 
      echoc RED "untar: L'archivio $SOURCEFILE e' di un tipo sconosciuto"
      exit 3
    ;;
esac
exit $?
