#! /bin/bash
# COMPONENT_NAME: installr
#
# FUNCTIONS: Installs APL2 Runtime Library on Linux
#
# COPYRIGHT:
#
# Licensed Materials - Property of IBM
# (C) Copyright International Business Machines Corp. 2002
# All rights reserved.
#
# US Government Users Restricted Rights - Use, duplication or disclosure
# restricted by GSA ADP Schedule Contract with IBM Corp.
#

if [ "`/usr/bin/whoami`" != "root" ]
then
      echo ""
      echo "You must be root to execute the APL2 Runtime Library install script. "
      exit 1
fi

APL2=/usr/APL2run
INSFILE=apl2lr20
export APL2

echo ""
echo "APL2 Runtime Library files will be installed under $APL2."
echo -n "Do you wish to use this directory for APL2?  <Yy, Nn>: [Y] "
read foo
[ -z "$foo" ] && foo=Y
case $foo in
          [Yy] )
          ;;
          * )
             echo -n "Enter the directory you wish to use and press <ENTER>: [$APL2] "
             read APL2
             [ -z "$APL2" ] && APL2=/usr/APL2
             echo "APL2 Runtime Library will be installed in directory $APL2."
             echo -n "Is that OK?  <Yy, Nn>: [Y] "
             read spec
             [ -z "$spec" ] && spec=Y
             case $spec in
                    [Yy] )
                         export APL2
                    ;;
                    * )
                         echo "Aborting installation, please rerun"
                         exit 1
                    ;;
             esac
          ;;
esac

if [ -d $APL2 ] ; then      # Remove older version if exists
  echo "You already have a $APL2 directory. "
  echo -n "Clean the directory? <Yy, Nn>: [Y] "
  read scrub
  [ -z "$scrub" ] && scrub=Y
  case $scrub in
              [Yy] )
                   rm -fr $APL2
              ;;
              * )
                   echo "Directory will not be cleaned."
                   echo "The new level will over-write the old, but other files may remain"
              ;;
  esac

fi

if [ ! -d $APL2 ] ; then
   mkdir -p $APL2
   if [ ! -d $APL2 ] ; then
      echo "mkdir failed, aborting installation"
      exit 1
   fi
fi

cp $INSFILE.tgz  $APL2/$INSFILE.tgz
cd $APL2

gzip -d $INSFILE.tgz
tar -xovf $INSFILE.tar
rm $INSFILE.tar

if [ -x /usr/bin/apl2run ] ; then
   rm /usr/bin/apl2run
fi
if [ -x /usr/bin/apl2env ] ; then
   rm /usr/bin/apl2env
fi

ln -s $APL2/bin/apl2run  /usr/bin/apl2run
ln -s $APL2/bin/apl2env  /usr/bin/apl2env

if [ "$APL2" != "/usr/APL2run" ] ; then

ed $APL2/bin/apl2run <<ENDedit
/^APL2=\/usr\/APL2run
d
i
APL2=${APL2}
.
w
q
ENDedit

ed $APL2/bin/apl2env <<ENDedit
/^APL2=\/usr\/APL2run
d
i
APL2=${APL2}
.
w
q
ENDedit

fi

echo "                                                        "
echo "APL2 Runtime Library has been successfully installed.   "
echo " "
echo "Notes:                                                  "
echo " "
echo "1. To access the APL2 fonts, you will need to add these "
echo "   lines to your .bash_profile or other shell profile:  "
echo "       xset fp+ $APL2/fonts/X11                         "
echo "       xset fp  rehash                                  "
echo " "
echo "2. Before using APL2 Runtime Library look at the file   "
echo "   $APL2/README                                         "
echo " "
