#!/bin/sh

#
#  Install a distribution of PST (Practical Supercomputing Toolkit).
#
#  EXAMPLE
#    ./install PST.2001.06.11.14.55.tar.gz 
#    ./install PST.2001.06.11.14.55.tar
#    ./install PST.2001.06.11.14.55
#
#  INPUT
#    PST distribution file, either .tar.gz, .tar, or just a directory.
#
#  OUTPUT
#    None
#
#  OPTIONS
#    None
#
#  NOTES
#    "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 6-2001 by Joe Werne
#
#  HISTORY
#    6-30-2001: Joe Werne
#      1. Fix "awk match" bug on Sun systems.  "match" is implemented
#         only in "nawk", not "awk".  To obtain nawk/match 
#         functionality, I switched to Perl instead of awk/nawk.
#

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="PST/MD"

# Is first argument a .gz file?
a="\$tmp=\"$1\"; if (\$tmp=~/\.gz\$/) {print \"0\"} else {print \"1\"}"
ext=`echo $a | perl -`

if [ $ext -eq 0 ]
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 -`
else
  file=$1
fi

# Now do we have a .tar file?
a="\$tmp=\"$file\"; if (\$tmp=~/\.tar\$/) {print \"0\"} else {print \"1\"}"
ext=`echo $a | perl -`

if [ $ext -eq 0 ]
then
  /bin/tar xvf $file
  if [ $? -ne 0 ]
  then
    echo "`basename $0`: ERROR: tar xvf failed on '$file'"
    exit 3
  fi
  a="\$tmp=\"$file\"; \$tmp=~s/\.tar\$//; print \"\$tmp\";"
  name=`echo $a | perl -`
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\""
line=`head -1 $installcode`
echo $line | grep perl > /dev/null

if [ $? = 0 ]
then
  echo "#!"$perlloc > pst_tempfile
  nlines=`wc -l $installcode | awk '{print$1}'`
  nlines=`expr $nlines - 1 `
#  echo "  $installcode $nlines lines"
  tail -$nlines $installcode >> pst_tempfile
  mv pst_tempfile $installcode
  chmod +x $installcode
  /bin/cp $installcode $name/PST/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
