#! /bin/sh
# $Id: tarmaker,v 1.1.1.1 1999/02/22 13:49:35 makholm Exp $
# Authors:  Jens Peter Secher (jpsecher@diku.dk)
#           Arne Glenstrup (panic@diku.dk)
#           Henning Makholm (makholm@diku.dk)
# Content:  C-Mix system: tarfile generator
#
# Copyright  1998. The TOPPS group at DIKU, U of Copenhagen.
# Redistribution and modification are allowed under certain
# terms; see the file COPYING.cmix for details.

# This script creates a tarfile of the important files in one or
# more directories. Its most prominent duty is to decide which
# files are to be considered important.
#
# If the directory has a subdirectory named CVS, it is considered
# a CVS client directory. In that case the important files are
# - the files that are managed via CVS,
# - *except* those listed in a local file called 'tar-ignore'
# - *plus* those listed in a local file called 'tar-additional'
#   (which the script tries to 'make' before tar'ing them).
#
# If the directory is not a CVS client directory but a file called
# 'tar-contents' exists, that file lists the important files.
#
# If neither CVS/ nor tar-contents exists, the script tries to
# 'make clean' and every file that survives that is considered
# important.
#
# In any case, the tarfile will contain a tar-contents file so that
# it can later be repackaged.
#
#
# The *first* argument to the script - which is mandatory - is the name
# of the final tarball.
# 
# The remaining arguments are the directories that are to be searched
# for files. If none are given, '.' is assumed.

: ${MAKE=make}
: ${TAR=tar}

if [ $1 ]
then
   tarball=$1
   shift
else
   echo usage: $0 tarfile { directory ... }
   exit 1
fi

if [ $1 ]
then
   directories=$*
else
   directories=.
fi

rm -f /tmp/$$.files
rm -f /tmp/$$.delete

for d in $directories
do
   case $d in
      */) dir=`echo $d | sed 's:/^::'` ;;
      *) dir=$d ;;
   esac
   if [ -d $dir ]
   then
      if [ -r $dir/CVS/Entries ]
      then
         echo $dir is a CVS-managed directory
	 sed -e 's:/::' -e 's:/.*::' < $dir/CVS/Entries > $dir/#.$$
	 if [ -r $dir/tar-ignore ]
         then
            grep -F -v -x -f $dir/tar-ignore $dir/#.$$ > $dir/tar-contents
	    rm $dir/#.$$
         else
            mv $dir/#.$$ $dir/tar-contents
         fi
         if [ -r $dir/tar-additional ]
	 then
            for f in `grep -F -v -x -f $dir/tar-contents $dir/tar-additional`
            do
		(cd $dir; $MAKE $f)
		echo $f >> $dir/tar-contents
            done
         fi
	 echo $dir/tar-contents >> /tmp/$$.delete
      elif [ -r $dir/tar-contents ]
      then
         echo $dir has a tar-contents file already
	 # everything is well.
      else
         echo $dir has no tar-contents 'file;' trying to make one
         ( cd $dir
           $MAKE clean
           for f in *
           do
             if [ -r $f ] ; then echo $f >> tar-contents ; fi
           done )
         echo $dir/tar-contents >> /tmp/$$.delete
      fi                
      sed 's:^:'$dir/: $dir/tar-contents >> /tmp/$$.files
      echo $dir/tar-contents >> /tmp/$$.files
   else
      echo 1>&2 $dir is not a directory
      exit 1
   fi
done

case $tarball in
   *.tgz) tarreal=`echo $tarball | sed 's:tgz$:tar:'` ;;
   *.gz) tarreal=`echo $tarball | sed 's:.gz$::'` ;;
   *) tarreal=$tarball ;;
esac

echo $TAR cf $tarreal `cat /tmp/$$.files`
$TAR cf $tarreal `cat /tmp/$$.files`

case $tarball in
   *.tgz) echo gzip $tarreal ; gzip $tarreal ; mv $tarreal.gz $tarball ;;
   *.gz) echo gzip $tarreal ; gzip $tarreal ;;
   *) ;;
esac 

rm `cat /tmp/$$.delete`
rm /tmp/$$.files
rm /tmp/$$.delete

exit 0

