#!/bin/sh
DATE="03/10/1997"
MYCOMMAND=/usr/local/share/vecfem3/vempfade
#**************************************************************************
#**                                                                     ***
#**        Copyrights University of Karlsruhe, 1996                     ***
#**        Program by L. Grosz                                          ***
#**                                                                     ***
#**************************************************************************
#**                                                                     ***
if [ $# -lt 1 ]
 then
   echo "illegal number of arguments:" >&2
   echo "call:  vemtar [-{da}]  <problem>" >&2
   exit 1
fi
echo "VECFEM: create tar-files"
echo "Version: $DATE"
echo "Copyrights by University of Karlsruhe 1996"
#---------------------------------
#
#** finde th vecfem variables
#
if [ "$MYCOMMAND" = "" ] ; then
  . vempfade
  echo vempfade from `dirname $(which vempfade)`
else
  VEMPATH=`dirname $MYCOMMAND`
  SC=`basename $MYCOMMAND`
  if test -r $VEMPATH/vempfade ; then
     . $VEMPATH/vempfade
     echo vempfade from $VEMPATH
  else
    HELP=`which $SC`
    VEMPATH=`dirname $HELP`
    if test -r $VEMPATH/vempfade ; then
      . $VEMPATH/vempfade
      echo vempfade from $VEMPATH
    else
      . vempfade
      echo vempfade from `dirname $(which vempfade)`
    fi
  fi
fi
#
#---------------------------------
EXT=$$

if [ $# -lt 2 ]
 then
  OPTION=" "
  WORKPIECE=$1
else
  OPTION=$1
  if [ "$OPTION" != "-a"  -a  "$OPTION" != "-d" ]
    then
      echo "illegal option "$OPTION". allowed options: -d or -a"
      exit 1
  fi
  WORKPIECE=$2
fi
#
#-- create the list of file should be tared:
#
FILELIST0=`ls $WORKPIECE.* | sed /$WORKPIECE.tar.Z/d`

if test -r $WORKPIECE.lst
then
  FILELIST1=`$VECFEM_AWK 'BEGIN{getline;split($0,ARG," ");
		       c=0
		       while (c < ARG[1]) {getline; print $0;c++}
                       getline;split($0,ARG," ");
		       c=0
		       while (c < ARG[1]) {getline; printf $0;c++}
		       getline;getline;getline}
                 {split($0,ARG,"|");print ARG[6]}' $WORKPIECE.lst | sort -u`
fi
#
#-- select existing files
#
TARLIST=""
for FILE in $FILELIST0 $FILELIST1
 do
  if test -r $FILE 
   then
    TARLIST=$TARLIST" "$FILE
  fi
done
#
#-- tar files and compress
#
NTAR=`echo $TARLIST | wc -w` 
NDEL=0
if [ $NTAR -ne 0 ] 
  then
    tar cf /tmp/$WORKPIECE.tar $TARLIST
    if [ $? -ne 0 ]
      then
        rm -f /tmp/$WORKPIECE.tar
	echo "tar command failed !"
	exit 1
    fi
    compress -c /tmp/$WORKPIECE.tar > $WORKPIECE.tar.Z
    if [ $? -ne 0 ]
      then
	echo "compress command failed !"
        rm -f $WORKPIECE.tar.Z
	exit 1
    fi
    rm -f /tmp/$WORKPIECE.tar
#
#-- remove tared files 
#
    if [ "$OPTION" = "-d" ]
      then 
        NDEL=`echo $FILELIST1 | wc -w` 
        rm -f $FILELIST1
    fi
    if [ "$OPTION" = "-a" ]
      then 
        NDEL=`echo $TARLIST | wc -w` 
        rm -f $TARLIST
    fi
fi
echo $NTAR" files has been tared in "$WORKPIECE".tar.Z"
echo $NDEL" files has been deleted."
