#! /bin/sh
#------------------------------------------------------------------------------
#
#	This shellscript takes the name of a graph pool in a given directory
#	and compresses the pool, giving the resulting file the default name
#	poolname followed by ".C.gz" or the given filename followed by ".gz".
#    
#       $1 = PoolName = ProjectName
#       $2 = PoolDir  = $GRAS (default value)
#       $3 = FileName = PoolName.C (default value)
#
#	We recommend that you use CompressPool and UncompressPool for
#	graph pools which are not used for longer periods of time, such
#	as demo graphs.
#
#	Our experiences show that the average compression rate is 80% !
#
#------------------------------------------------------------------------------
 
# Determine which operating system version the host has
osver=`uname -a`
case $osver in
SunOS*5.*)    # Solaris 2.x
        PLATFORM=sol2
        OSVERSION=SOL2
        OSDYNLIBEXT=so;;
SunOS*4.1.*)  # SunOS 4.1.x
        PLATFORM=sunos4
        OSVERSION=SUNOS4
        OSDYNLIBEXT=so.0.0;;
Linux*2.*.*)  # Linux 2.x.x
        PLATFORM=linuxlibc6
        OSVERSION=LINUXLIBC6
        OSDYNLIBEXT=so;;	
*)      echo 'unknown os version' ;;  
esac
export PLATFORM OSVERSION OSDYNLIBEXT

# Definition of the PROGRES environment's root directory
# i.e. the root of the directory which contains all
# installed PROGRES files. $HOME or $HOME/progres might
# be a reasonable value if PROGRES has an own account
# or is a subdirectory of the current account.
 
PROGRESROOT=${PROGRESROOT:-$HOME}
export PROGRESROOT
 
# test whether PROGRESROOT exists and
# actually contains PROGRES-installation
# files.
 
if [ ! -f $PROGRESROOT/bin/$OSVERSION/ProgControl ]
then
  PROGRESROOT=$HOME/progres
  if [ ! -f $PROGRESROOT/bin/$OSVERSION/ProgControl ]
  then
    PROGRESROOT=/home/projects/progres
    if [ ! -f $PROGRESROOT/bin/$OSVERSION/ProgControl ]
    then
      echo "Variable PROGRESROOT has wrong value!"
      echo "Change default value in this script!"
      exit 1
    fi
  fi
fi

# Initialize additional environment variables

LD_LIBRARY_PATH="$PROGRESROOT/lib/$OSVERSION:$PROGRESROOT/lib:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH

. $PROGRESROOT/bin/InitEnvVars

#---------------------------------------------------

TMPGRAS=${TMPGRAS:-/tmp/compress}$$
export TMPGRAS
if [ -d $TMPGRAS ]
then
  rm -r $TMPGRAS
fi
mkdir -p $TMPGRAS

GRAS=${GRAS:-${DATAROOT}/gras/${OSVERSION}}
GRAS=${2:-${GRAS}}
export GRAS

if [ ${1:-""} = "" -o ${GRAS:-""} = "" ]
then
  echo ""
  echo "USAGE: CompressPool poolname [ pooldir [ filename ] ]"
  echo "       "'$'"GRAS/poolname must exist"
  echo "       "'$'"GRAS = pooldir must exist"
  echo "       default filename := $GRAS/poolname.C"
  echo "       "'$'"GRAS/filename.gz must not exist"
  echo "       "'$'"GRAS/filename will be overwritten"
  echo "       GRASlin must be in your executable PATH"
  echo ""
elif [ ! -d $GRAS/$1 ]
then
  echo ""
  echo "ERROR: pool $GRAS/$1 not existent"
  echo ""
else
  FILE=${3:-"$GRAS/$1.C"}
  if [ -f "$FILE.gz" ]
  then
    echo ""
    echo "ERROR: Remove old "$FILE.gz" first"
    echo ""
  else
    echo "Compressing pool "$1" ..."
    $PROGRESROOT/bin/$OSVERSION/GRASlin -r $1 > $FILE
    gzip $FILE
    echo "Old Size: "`du -s $GRAS/$1 | cut -f1`\
	 " kB; new size: "`du -a $FILE.gz | cut -f1`" kB"
  fi
fi

rm -rf $TMPGRAS

