#! /bin/sh
#------------------------------------------------------------------------------
#
#	The opposite of CompressPool. UncompressPool must be called with
#	a file (pool) name as parameter. It expects an existing file named
#	filename.C.gz. The resulting graphpool may not already exist and will be
#       recreated under its former name in the given pool directory.
#
#       $1 = filename (in current directory or $GRAS)
#       $2 = pooldir = $GRAS  (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: UncompressPool filename [ pooldir ]"
    echo "       filename[.C].gz must exist"
    echo "       "'$'"GRAS = pooldir must exist"
    echo "       filename[.C] must not exist"
    echo "       "'$'"GRAS/poolname must not exist"
    echo "       GRASlin must be in your executable path"
    echo ""
elif [ -f $1.gz ]
then
    FILE=$1
elif [ -f $1.C.gz ]
then
    FILE=$1.C
elif [ -f $GRAS/$1.gz ]
then
    FILE=$GRAS/$1
elif [ -f $GRAS/$1.C.gz ]
then
    FILE=$GRAS/$1.C
else
    echo ""
    echo "ERROR: file [$GRAS/]$1[.C].gz not found."
    echo ""
fi

if [ ${FILE:-""} != "" -a ${GRAS:-""} != "" ]
then
  if [ -d $GRAS/$1 ]
  then
      echo ""
      echo "ERROR: Remove old pool $1 first"
      echo ""
  elif [ -f $FILE ]
  then
      echo ""
      echo "ERROR: Remove old "$FILE" first"
      echo ""
  else
#     echo "Uncompressing pool "$1" ..."
      gunzip $FILE.gz
      $PROGRESROOT/bin/$OSVERSION/linGRAS < $FILE > /dev/null
      gzip $FILE
  fi
fi

rm -rf $TMPGRAS
