#!/bin/sh

# PLT software installer
# Configures PLTHOME path within scripts
# For certain platforms and installations, adds extra
#  directory links (to reach non-standard binaries
#  through the platform's standard path)
# Creates .zo files if the user assents

didnothing=" (nothing to do)"

showhelp ()
{
  echo "Usage: $0 [ newplthomedir ]"
  echo " newplthomedir defaults to the current directory"
  echo " use \"\" for newplthomedir to keep the current setting"
  exit 1
}

if [ $# -gt 1 ] ; then
  showhelp
fi
if [ "$1" = '-h' ] ; then
  showhelp
fi

if [ ! \( \( -x install \) -a \( -d collects \) \) ] ; then
  echo "$0: must be run from its own directory"
  exit 1
fi

if [ $# -eq 1 ] ; then
  installplthome="$1"
else
  installplthome=`pwd`
fi

PLTHOME="$installplthome"
export PLTHOME
PLTCOLLECTS=""
export PLTCOLLECTS
PLTEXTENSION=""
export PLTEXTENSION

echo "setting PLTHOME to $installplthome in scripts:"

case `uname -s` in
  *BSD) # FreeBSD and OpenBSD, at least
    chmod='chmod -RH'
    ;;
  *)
    chmod='chmod'
    ;;
esac

if [ "$installplthome" != '' ] ; then
  # Change the scripts in bin/, replacing
  #   PLTHOME=.*
  # with
  #   PLTHOME=<newpath>
  # where <newpath> is provided to this script

  PROGRAM="/set PLTHOME=.*/ { print \"  set PLTHOME=$installplthome\"; next } /PLTHOME=.*/ { print \"  PLTHOME=$installplthome\"; next } /.*/ {print} "

  for f in bin/* ; do
   if [ -f $f ] ; then
     echo " updating $f"
     didnothing=""
     awk "$PROGRAM" $f > $f.tmp
     if [ -w $f ] ; then
       cat $f.tmp > $f
     else
       # Ugh - temporarily chmod to allow writing
       oldstate=`ls -Ll $f`
       $chmod a+w $f
       cat $f.tmp > $f  
       $chmod a-w $f
       ucanwrite=`echo $oldstate | cut -c3,3`
       gcanwrite=`echo $oldstate | cut -c6,6`
       ocanwrite=`echo $oldstate | cut -c9,9`
       if [ $ucanwrite = 'w' ] ; then
        $chmod u+w $f 
       fi
       if [ $gcanwrite = 'w' ] ; then
        $chmod g+w $f 
       fi
       if [ $ocanwrite = 'w' ] ; then
        $chmod o+w $f 
       fi
     fi
     rm $f.tmp
   else
    if [ -d $f ]; then
      echo "$0: weird - $f is not a file!"
    fi
   fi
  done
fi

checklink ()
{
  PACKAGE=$1
  SPECIAL=$2
  STD=$3
  SHORTSPECIAL=$4
  SPECIALNAME=$5
  STDNAME=$6

  if [ -r $SPECIAL ] ; then
   if [ ! \( -r $STD \) ] ; then
     echo "If you *do not* plan to install the $STDNAME "
     echo " version of the PLT software, a soft-link to"
     echo " the $SPECIALNAME version should be installed for"
     echo " $PACKAGE."
     echo -n " Add this link (y/n)? [y] "
     read response
     if [ "$response" != 'n' ] ; then
      if [ "$response" != 'N' ] ; then
       didnothing=""
       ln -s $SHORTSPECIAL $STD
       echo "link from $STDNAME ($STD) to $SPECIALNAME ($SHORTSPECIAL) added"
      fi
     fi
   fi
  fi
}

checklink "MrEd/DrScheme" ".bin/rs6k-aix-xt/mred" ".bin/rs6k-aix/mred" "../rs6k-aix-xt/mred" "AIX Xt" "AIX Motif"
checklink "MrEd/DrScheme" ".bin/sparc-solaris-motif/mred" ".bin/sparc-solaris/mred" "../sparc-solaris-motif/mred" "Solaris Motif" "Solaris Xt"
checklink "MzScheme/MrEd/DrScheme" ".bin/sparc-sunos4-static" ".bin/sparc-sunos4" "sparc-sunos4-static" "SunOS4 Static" "Regular SunOS4"

if [ `bin/archsys` = "sparc-solaris" ] ; then
  checklink "MzScheme/MrEd/DrScheme" ".bin/sparc-sunos4" ".bin/sparc-solaris" "sparc-sunos4" "SunOS4" "Solaris"
  checklink "MzScheme/MrEd/DrScheme" ".bin/sparc-sunos4-static" ".bin/sparc-solaris" "sparc-sunos4-static" "SunOS4 Static" "Solaris"
fi

if [ -z "${RPM_INSTALL_PREFIX}" ] ; then
  echo 'PLT software starts up much faster with .zo files, but creating .zo'
  echo 'files now takes a few minutes and requires about 5MB of additional'
  echo 'disk space. Create .zo files later by running plt/bin/setup-plt.'
  echo -n ' Create .zo files now (y/n)? [y] '
  read response
else
  response="y"
fi
if [ "$response" != 'n' ] ; then
  if [ "$response" != 'N' ] ; then
    didnothing=""
    bin/setup-plt
 fi
fi

echo
echo "PLT installation done${didnothing}."
if [ -f bin/drscheme ] ; then
 echo "Run DrScheme as bin/drscheme."
 echo "For Help, select \`Help Desk' from DrScheme's \`Help' menu, or run bin/help-desk."
fi
