#!/bin/sh
#
#
#  NAME
#    install: Install distribution of PST (Practical Supercomputing Toolkit).
#
#
#  USAGE
#    ./install pst_distribution
#
#
#  EXAMPLES
#    ./install PST.2001.06.11.14.55.tar.gz
#    ./install PST.2001.06.11.14.55.tar
#    ./install PST.2001.06.11.14.55
#
#
#  ARGUMENT
#    PST distribution file (either .tar.gz or .tar) or directory.
#
#
#  DESCRIPTION
#    "install" uses the input PST distribution file to set up PST for the
#    user.  If appropriate, install gunzip's and untar's the distribution
#    file.  "install" next locates Perl using /bin/csh -c "which perl".
#    "install" then edits the Perl program PST/MD/bin/pstinstall
#    in the distribution directory to ensure the first line properly
#    identifies the location of Perl on the current platform.  Finally,
#    "install" executes "pstinstall," which interacts with the user
#    to set up the user's distribution.  "pstinstall" then edits the
#    remaining Perl programs in the distribution, ensuring that they
#    also properly identify the location of Perl on the local system,
#    and making sure any "use lib" lines correctly identify the user's
#    chosen PST location.
#
#
#  AUTHOR
#    Written by Joe Werne and Michael J. Gourlay

usage ()
{
  echo "usage: `basename $0` { PST*.tar.gz | PST*.tar | PST* }"
  exit 1
}

if [ $# = 0 ]; then
  echo "`basename $0`: ERROR: command line argument missing."
  usage
fi

if [ ! -r $1 ]; then
  echo "`basename $0`: ERROR: could not read distribution file '$1'"
  usage
fi

installcode="pstinstall"
edir="MD"

####
#### TO DO: combine gunzip and tar into one command.
#### That runs faster and leaves less mess behind.
####

# Is first argument a ".gz" file?
no_gz_suffix=`echo $1 | sed -e 's/\.gz$//'`
if [ "$1" != "$no_gz_suffix" ]
then
  gunzip $1 2>&1
  if [ $? -ne 0 ]
  then
    echo "`basename $0`: ERROR: gunzip failed on '$1'"
    exit 2
  fi
  ####a="\$tmp=\"$1\"; \$tmp=~s/\.gz\$//; print \"\$tmp\";"
  ####file=`echo $a | perl -`
  file=$no_gz_suffix
else
  file=$1
fi

# Now do we have a ".tar" file?
no_tar_suffix=`echo $file | sed -e 's/\.tar$//'`
if [ "$file" != "$no_tar_suffix" ]
then
  /bin/tar xf $file
  if [ $? -ne 0 ]
  then
    echo "`basename $0`: ERROR: tar extraction failed on '$file'"
    exit 3
  fi
  ####a="\$tmp=\"$file\"; \$tmp=~s/\.tar\$//; print \"\$tmp\";"
  ####name=`echo $a | perl -`
  name=$no_tar_suffix
else
  name=$file
fi

# Get $installcode from distribution
/bin/cp $name/$edir/bin/$installcode .
perlloc=`/bin/csh -c "which perl"`
#echo "`basename $0`: Converting perl program \"$installcode\""

## Make sure installcode is a perl script. [I guess.  --MJG]
line=`head -1 $installcode`

# Note the "$?" in the "if" test expression.
# The "if" must therefore immediately follow "grep perl". [--MJG]
echo $line | grep perl > /dev/null

if [ $? = 0 ]
then
  echo "#!"$perlloc > pst_tempfile
  cat $installcode >> pst_tempfile
  mv pst_tempfile $installcode
  chmod +x $installcode
  /bin/cp $installcode $name/MD/bin/$installcode
  export HOME
  # Remove
  echo ""
  echo ""
  echo "   If install succeeds, please remove '$file'."
  exec ./${installcode} $name
else
  echo "`basename $0`: ERROR: bad $installcode file."
  echo "`basename $0`: ERROR: install PST failed."
  exit 4
fi

exit 0
