#!/bin/sh
#
# tarball
#   a simple frontend for tar
#   version 0.2   2002-09-02
#   (C) 2001-2002 Michael Peceny michael@peceny.de
#

TARBALL_VERSION='0.2'

# similar to: if test -z $1 (sh inbuilt equivalent)
if [ -z $1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
  echo "Usage: $0 <tararchive> to untar"
  echo "       $0 -l <tararchive> to list archive contents"
  echo -e "       $0 <archivename> <pattern> to archive files\n"
  exit
fi

if test "$1" = "-v" -o "$1" = "--version" ; then
  echo "tarball version $TARBALL_VERSION"
  exit
fi

# 1st parameter is -l or --list
if test "$1" = "-l" -o "$1" = "--list" ; then
  if [ -z "$2" ] ; then
    echo "Missing file parameter. See `expr "$0" : ".*/\([^/]*\)"` --help for usage."
    exit
  fi
  if test "`expr "$2" : '.*\.tar\.bz2$'`" != "0" ; then
    tar --bzip2 -tvf $2
# the following two could be joined into one test
  elif test "`expr "$2" : '.*\.tar\.gz$'`" != "0" ; then
    tar -tzvf $2
  elif test "`expr "$2" : '.*\.tgz$'`" != "0" ; then
    tar -tzvf $2
  elif test "`expr "$2" : '.*\.tar$'`" != "0" ; then
    tar -tvf $2
  else
    echo "Archive type (.tar.bz2, .tar.gz, .tgz, .tar) could not be determined"
  fi
  exit
fi

# only one parameter => untar
if test -z "$2" ; then
  if test "`expr "$1" : '.*\.tar\.bz2$'`" != "0" ; then
    tar --bzip2 -xvf $1
# the following two could be joined into one test
  elif test "`expr "$1" : '.*\.tar\.gz$'`" != "0" ; then
    tar -xzvf $1
  elif test "`expr "$1" : '.*\.tgz$'`" != "0" ; then
    tar -xzvf $1
  elif test "`expr "$1" : '.*\.tar$'`" != "0" ; then
    tar -xvf $1
  else
    echo "Archive type (.tar.bz2, .tar.gz, .tgz, .tar) could not be determined"
  fi
  exit
fi

# create tar archive
echo "Creating archive $(expr "$1" : "\(.*[^/]\)/\?$").tar.bz2 from `expr "$*" : "$1${IFS:0:1}\(.*\)"`"
tar --bzip2 -cvf $(expr "$1" : "\(.*[^/]\)/\?$").tar.bz2 `expr "$*" : "$1${IFS:0:1}\(.*\)"`
