#! /bin/sh
#
# Written by Martin Sperl
# (sperl@dsn.ast.univie.ac.at)
#


if test $# -lt 3 ; then
  cat <<EOF
Usage: `basename $0` <basedir> <SOURCE-FILES> <DESTINATION-FILS>
  copies all files from the source-tar-files to the common
  destination-tar-file with basedir as a common base directory.
EOF
  exit 0
fi

BaseDir="$1"
shift

Sourcefiles="$1"

while test "$#" != 2 ; do
  shift
  Sourcefiles="$Sourcefiles $1"
done

shift
Final=$1

Destination=/tmp/join$$.tar

touch $Destination

curdir=`pwd`

mkdir tmp$$
mkdir tmp$$/$BaseDir

#uncompress all files
cd tmp$$/$BaseDir
for each in $Sourcefiles ; do
  ( \
  if test `basename $each gz` != `basename $each` ; then \
    gzip -dc ../../$each;\
  else \
    cat ../../$each;\
  fi; \
  ) | tar xf -
done
cd ..
#now tar everything
tar -cf $Destination *

cd ..

rm -fr tmp$$

# goto old directory
cd $curdir

if test `basename $Final gz` != `basename $Final` ; then
  gzip -c $Destination > $Final
else
  cat $Destination > $Final
fi

rm -f $Destination
