#!/bin/sh
# extagz.sh ARCHIVE

if [ "$1" = "" ]; then
  echo USAGE: $0 ARCHVE
  echo Example: $0 algebra-0.53.tar.gz
  exit
fi

TGZ=$1
DIR=`echo $1 | sed -e 's/\\/$//'| sed -e 's/.tar.gz$//'`
DIRR=`echo $DIR | sed -e 's/[0123456789.\-]*[a-z]*$//'`

if [ -f $TGZ ]; then
  if [ -d $DIR ]; then
    echo "$DIR exits overwrite (NONE|y|Y|b|B|n|N)?"
    echo "y|Y ... Yes, b|B Back Up, n|N  ... STOP."
    read ANS
    if [ "$ANS" != "" -a "$ANS" != y -a "$ANS" != Y ]; then
      if [ "$ANS" = b -o "$ANS" = B ]; then
        echo "$DIR BACKUPED AS $DIR-old."
        echo
        mv $DIR $DIR-old
      else
        echo STOP.
        exit
      fi
    fi
  fi
  tar xfvz $TGZ
  echo $DIR EXTRACTED.

  if [ -d $DIRR ]; then
    echo "$DIRR BACKUPED AS $DIRR-old."
    rm -r $DIRR-old
    mv $DIRR $DIRR-old
  fi

  echo mv $DIR $DIRR
  mv $DIR $DIRR
else
  echo $TGZ not exist.
fi
