#!/bin/ksh

################################################################################
# 
# File:        install.sh
#
# Purpose:     Helmsman Installation Script
#
# Description: This Korn Shell script controls the installation of the 
#              Helmsman UNIX client.
#
# Other:
#
# Restriction:
#
# Future:
#
# RCS Information:
#    $Revision: 1.3 $
#    $Date: 1999/10/27 16:33:05 $
#    $Source: /mnt/mastiff1/helmsman/helmdecaf-4.0/release.common/install.sh,v $
#
################################################################################


#####################################################################
# Prompt the user to accept or decline license agreement
#####################################################################
query_accept_decline()
{
   output="$1"
   while true
   do
      echo "$output"
      read answer
      #answer="`echo $user_input | tr [[:upper:]] [[:lower:]]`"
      if [ "$answer" = "accept" ]
      then
         return 0
      elif [ "$answer" = "decline" ]
      then
         return 1
      else
         echo "\nExpecting accept or decline."
      fi
   done
}

#####################################################################
# Prompt the user to confirm an operation.
#####################################################################
query_yes_no()
{
   output="$1"
   while true
   do
      echo "$output"
      read yn
      if [ "$yn" = "" -o "$yn" = "y" -o "$yn" = "Y" -o "$yn" = "yes" -o "$yn" = "YES" -o "$yn" = "Yes" ]
      then
         return 0
      elif [ "$yn" = "n" -o "$yn" = "N" -o "$yn" = "no" -o "$yn" = "NO" -o "$yn" = "No" ]
      then
         return 1
      else
         echo "\nExpecting yes or no.\n"
      fi
   done
}
#####################################################################
# Allow Installation Adobe Reader.
#####################################################################
install_adobe_reader()
{
   echo "\nHelmsman uses Adobe(tm) Portable Document Format files."
   echo "In order to take advantage of PDF's benefits, Adobe Exchange, "
   echo "or Acrobat Reader must be installed.  You may install Adobe Reader now "
   echo "or provide the full path to Adobe Exchange or Acrobat Reader.\n"

   if query_yes_no "Do you have Acrobat Exchange Installed? [y] \c"
   then
      while true
      do
         echo "\nPlease enter the full path to the Adobe(tm) Exchange"
         read exchange
         if [ ! -f "$exchange" ]
         then
            echo "\nThe path for Adobe Exchange appears to be incorrect."
            echo "Be sure to include the name at the end."
            echo "Please re-check the path and try again."
         else
            cp $DESTINATION/config.orig $DESTINATION/config.dat
            echo "AcrobatPath=$exchange;" >> $DESTINATION/config.dat
            echo "PdfPlugIn=Exchange;" >> $DESTINATION/config.dat
            break
         fi
      done
   elif query_yes_no "Do you have Acrobat Reader Installed? [y] \c"
   then
      while true
      do
         echo "\nPlease enter the full path to the Adobe(tm) Acrobat Reader"
         read reader
         if [ ! -f "$reader" ]
         then
            echo "\nThe path for Adobe Reader appears to be incorrect."
            echo "Be sure to include the name at the end."
            echo "Please re-check the path and try again."
         else
            break
         fi
      done

      # Have Reader, determine the version
      x=`echo $reader | sed 's/\// /g' | wc -w`
      y=`expr $x - 1`
      adobeDir=`echo $reader | cut -f1-$y -d"/"`
      echo "adobeDir $adobeDir"
      acroVersionFile=""
      if [ -f $adobeDir/Distillr/AcroVersion ]
      then
         acroVersionFile=$adobeDir/Distillr/AcroVersion
      elif [ -f $adobeDir/Exchange/AcroVersion ]
      then
         acroVersionFile=$adobeDir/Exchange/AcroVersion
      elif [ -f $adobeDir/Reader/AcroVersion ]
      then
         acroVersionFile=$adobeDir/Reader/AcroVersion
      fi
      echo "acroVersionFile $acroVersionFile"
            
      askUser=0
      if [ $acroVersionFile = "" ]
      then
         msg="Unable to determine Acrobat Reader Version"
         askUser=1
      else
         ver=`cat $acroVersionFile`
         # If any of these version are detected install our Reader, other wise they
         # have a higher version
         case "$ver" in
            0.*)
               msg="Detected version $ver of Acrobat Reader installed on your system"
               askUser=1
               ;;
            1.*)
               msg="Detected version $ver of Acrobat Reader installed on your system"
               askUser=1
               ;;
            2.*)
               msg="Detected version $ver of Acrobat Reader installed on your system"
               askUser=1
               ;;
            3.*)
               msg="Detected version $ver of Acrobat Reader installed on your system"
               askUser=1
               ;;
            4.*)
               msg="Detected version $ver of Acrobat Reader installed on your system"
               askUser=1
               ;;
            5.0.[1-8]*)
               msg="Detected version $ver of Acrobat Reader installed on your system"
               askUser=1
               ;;
            *)
               msg="\nDetected version $ver of Acrobat Reader installed on your system"
               askUser=0
               ;;
         esac
      fi

      installReader=0
      echo ""
      echo $msg
      echo ""
      if [ $askUser = 1 ]
      then
         if query_yes_no "Do you wish to install Acrobat Reader 5.0.8 Now? [y] \c"
         then
            installReader=1
         else
            cp $DESTINATION/config.orig $DESTINATION/config.dat
            echo "AcrobatPath=$reader;" >> $DESTINATION/config.dat
            echo "PdfPlugIn=Reader;" >> $DESTINATION/config.dat
            installReader=0
         fi
      else
         echo "The installed version of Acrobat Reader is newer than the version included"
         echo "in this release. PDF viewer $reader will be configured in Helmsman."
         cp $DESTINATION/config.orig $DESTINATION/config.dat
         echo "AcrobatPath=$reader;" >> $DESTINATION/config.dat
         echo "PdfPlugIn=Reader;" >> $DESTINATION/config.dat
         installReader=0
      fi 

      if [ $installReader = 1 ]
      then
         echo "Installing Acrobat Reader"
         cd $DESTINATION/AcroInstall
         ./INSTALL
         if [ $? -ne 0 ]
         then
            echo "Adobe(tm) Acrobat Reader installation failed, Helmsman installation aborted."
            exit -1
         fi
         # we need the reader path to add to the config.dat
         while true
         do
            echo "\nPlease enter the full path to the Adobe(tm) Acrobat Reader"
            read reader
            if [ ! -f "$reader" ]
            then
               echo "\nThe path for Adobe Reader appears to be incorrect."
               echo "Be sure to include the name at the end."
               echo "Please re-check the path and try again."
            else
               cp $DESTINATION/config.orig $DESTINATION/config.dat
               echo "AcrobatPath=$reader;" >> $DESTINATION/config.dat
               echo "PdfPlugIn=Reader;" >> $DESTINATION/config.dat
               break
            fi
         done
      fi
   else
      # Neither Exchange nor Reader is installed, Install the packaged version of Reader
      echo "Installing Acrobat Reader"
      cd $DESTINATION/AcroInstall
      ./INSTALL
      if [ $? -ne 0 ]
      then
         echo "Adobe(tm) Acrobat Reader installation failed, Helmsman installation aborted."
         exit -1
      fi
      # we need the reader path to add to the config.dat
      while true
      do
         echo "\nPlease enter the full path to the Adobe(tm) Acrobat Reader"
         read reader
         if [ ! -f "$reader" ]
         then
            echo "\nThe path for Adobe Reader appears to be incorrect."
            echo "Be sure to include the name at the end."
            echo "Please re-check the path and try again."
         else
            cp $DESTINATION/config.orig $DESTINATION/config.dat
            echo "AcrobatPath=$reader;" >> $DESTINATION/config.dat
            echo "PdfPlugIn=Reader;" >> $DESTINATION/config.dat
            break
         fi
      done
   fi

   # remove installation directory
   cd $DESTINATION
   rm -rf AcroInstall
}

#####################################################################
# Set Verity configuration params 
#####################################################################
set_verity_config_params()
{
  # Verity shared libraries
  echo "VdkLibPath=$DESTINATION/verity/lib;" >> $DESTINATION/config.dat
  echo "VdkLib221=$DESTINATION/verity/lib/libbroker221.so;" >> $DESTINATION/config.dat
  echo "VdkLib403=$DESTINATION/verity/lib/libbroker403.so;" >> $DESTINATION/config.dat
  echo "VdkCutOver=vgwfsys.cfg;" >> $DESTINATION/config.dat
  echo "Vdk221Common=$DESTINATION/verity/common/221;" >> $DESTINATION/config.dat
  echo "Vdk403Common=$DESTINATION/verity/common/403;" >> $DESTINATION/config.dat
}

# Entry point of installation script
umask 0
CLIENT_VERSION="4.4_B07"

echo "\nInstalling Helmsman UNIX Client $CLIENT_VERSION\n"
if query_yes_no "Do you wish to continue [y]: \c"
then
   echo
else
   exit -1
fi

echo "\nYou must read the license agreement to continue with the installation."
echo "(Press ENTER to page through the agreement)"
if query_yes_no "\nDo you wish to continue? [y]: \c"
then
   pg license.txt 2>/dev/null
   if [ $? -ne 0 ]
   then
      echo "\nYou must have the license.txt file to install the Helmsman Unix Client"
      exit -1
   fi
else
   exit -1
fi

if query_accept_decline "\nDo you (accept/decline) the terms and conditions of the license agreement?"
then
   echo ""
else
   exit -1
fi


# Make sure root is running the script
id | grep root >/dev/null
if [ $? -ne 0 ]
then
   echo "You must be root to install Helmsman"
   exit -1
fi

# Set PLATFORM variable to check for supported platforms and set PLATDIR variable
PLATFORM=`uname -s`
# Check for supported platforms
if [ $PLATFORM != "SunOS" ] && [ $PLATFORM != "HP-UX" ]
then
  echo "Unsupported platform detected...Installation aborted."
  exit -1
fi

# Display license agreement


# Set PLATDIR (not used)
if [ "$PLATFORM" = "HP-UX" ]
then
   PLATDIR=hpux10
else
   PLATDIR=solaris
fi

# Set OSLEVEL (not used yet)
OSLEVEL=`uname -r`

# Set the DESTINATION directory path
DESTINATION="/helmsman/helmclient"

# Set the SOURCE directory path
SOURCE=`pwd`

echo "\nInstalling Helmsman at /helmsman/helmclient"

if [ -d $DESTINATION ]
then
   if query_yes_no "\nThe previous version will be removed. Continue? [y] \c"
   then
      rm -rf $DESTINATION >/dev/null 2>&1
      mkdir -p $DESTINATION >/dev/null 2>&1
   else
      exit -1
   fi
else
   mkdir -p $DESTINATION >/dev/null 2>&1
fi

if [ ! -d $DESTINATION ]
then
   echo "Could not create directory: $DESTINATION"
   exit -1
fi

# OK, now extract files from the tar-file

cd $DESTINATION

echo "\nExtracting Helmsman files.  This will take awhile...\c"
$SOURCE/gzip -cd $SOURCE/helmclient4.tar.gz | tar xf -

# Allow the installation of Adobe(tm) Exchange
install_adobe_reader

# Set configuration params
set_verity_config_params


echo "Installation Complete.\n"
