#!/bin/sh
# ----------------------------------------------------------------------------
#
#   Installer for Mathematica Applications
#
#   This file and all scripts and messages accessed from this file
#        (c) 2004 Wolfram Research, Inc. All rights reserved.
#
#

LANG=C
export LANG

# ----------------------------------------------------------------------------
# Function: CleanUp_()

# Trap any exit (0). Remove $InstallTempDir if it exists and check for
# errors in the $ErrorFile
CleanUp_() {
   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Function: CleanUp_() ### >>"
      echo "<< ### Function: CleanUp_() ### >>" 1>&2
   fi

   InstallErrors=`cat "${ErrorFile}" | grep -v '^$' | \
         grep -v 'decompression OK, trailing garbage ignored$' | \
         grep -v 'tar: Signal caught, cleaning up'`
   # Trapped sigint with no other errors, restore previous directory contents
   # if possible.
   if [ "${InstallErrors}" = "SIGINT" ]; then
      if [ "${Verbose}" = "true" ]; then
         PrintCopyText Verbose "RemoveLogSigintVerbose"
      fi
      rm -rf "${ErrorFile}"

      # If there is a backup directory, restore the previous files
      # if we received a sigint.
      if [ -n "${BackupDirBase}" ]; then

         if [ "${BackupComplete}" = "true" ]; then
            if [ "${Verbose}" = "true" ]; then
               PrintCopyText Verbose "RemovingInstalledFilesVerbose"
            fi

            for Dir in $InstallDir; do
               if [ "${Dir}" = "NoInstallDirSpecified" ]; then
                  BackupDir="${TargetDirectory}/${BackupDirBase}"
               else
                  BackupDir="${TargetDirectory}/${Dir}/${BackupDirBase}"
               fi
               if [ ! -d "${BackupDir}" ]; then
                  continue
               fi
               FullTargetDirectory=`dirname "${BackupDir}"`

#               cd "${FullTargetDirectory}"
#               rm -rf `ls -a1 | grep -v '^\.$' | grep -v '^\.\.$' | \
#                  grep -v "^${BackupDirBase}$" | sort` 
   
               FileName=`ls -a1 "${FullTargetDirectory}" | grep -v '^\.$' | \
                  grep -v '^\.\.$' | grep -v "^${BackupDirBase}$" | \
                  sort | sed '1p;d'`
               while [ -n "${FileName}" ]; do
                  if [ "${Verbose}" = "true" ]; then
                     PrintCopyText Verbose "RemovingFileVerbose"
                  fi

                  rm -rf "${FullTargetDirectory}/${FileName}"
                  FileName=`ls -a1 "${FullTargetDirectory}" | grep -v '^\.$' | \
                     grep -v '^\.\.$' | grep -v "^${BackupDirBase}$" | \
                     sort | sed '1p;d'`
               done
            done
         fi

         for Dir in $InstallDir; do
            if [ "${Dir}" = "NoInstallDirSpecified" ]; then
               BackupDir="${TargetDirectory}/${BackupDirBase}"
            else
               BackupDir="${TargetDirectory}/${Dir}/${BackupDirBase}"
            fi

            if [ "${Verbose}" = "true" ]; then
               PrintCopyText Verbose "RestoringFilesFromBackupVerbose"
            fi
      
            if [ ! -d "${BackupDir}" ]; then
               continue
            fi
            FullTargetDirectory=`dirname "${BackupDir}"`

            FileName=`ls -a1 "${BackupDir}" | grep -v '^\.$' | \
               grep -v '^\.\.$' | sort | sed '1p;d'`
            while [ -n "${FileName}" ]; do
               if [ "${Verbose}" = "true" ]; then
                  PrintCopyText Verbose "MovingFileVerbose"
               fi

               mv "${BackupDir}/${FileName}" "${FullTargetDirectory}/."
               FileName=`ls -a1 "${BackupDir}" | grep -v '^\.$' | \
                  grep -v '^\.\.$' | sort | sed '1p;d'`
            done
            rmdir "${BackupDir}"
         done # for Dir
      fi # if BackupDirBase
   
   # No errors, if not running with -help and the user did not
   # abort installation then remove the ErrorLog, Backup directories,
   # and any man pages that existed previously.
   elif [ -z "${InstallErrors}" ]; then
      if [ "${Verbose}" = "true" ]; then
         PrintCopyText Verbose "RemoveLogNoErrorVerbose"
      fi
      rm -rf "${ErrorFile}"
      if [ "${Help}" = "false" -a "${userexit}" = "false" ]; then
         PrintCopyText Text "InstallCompleteText"
         if [ -n "${BackupDirBase}" ]; then
            for Dir in $InstallDir; do
               if [ "${InstallDir}" = "NoInstallDirSpecified" ]; then
                  BackupDir="${TargetDirectory}/${BackupDirBase}"
               else
                  BackupDir="${TargetDirectory}/${Dir}/${BackupDirBase}"
               fi
               rm -rf "${BackupDir}"
            done
         fi
      fi

      if [ -n "${ManPageDirectory}" ]; then
         for ManPage in $ExistingManPages; do
            rm -f "${ManPageDirectory}/${ManPage}.old"
         done
      fi
   # Errors found
   else
      if [ -d "${TargetDirectory}" -a -w "${TargetDirectory}" ]; then
         if [ "${Verbose}" = "true" ]; then
            PrintCopyText Verbose "ErrorsFoundMovingLogVerbose"
         fi
         mv "${ErrorFile}" "${TargetDirectory}/InstallErrors"
         echo ""
         PrintCopyText Error "InstallFailedMovedMessageError"
      else
         if [ "${Verbose}" = "true" ]; then
            PrintCopyText Verbose "ErrorsFoundVerbose"
         fi
         echo ""
         PrintCopyText Error "InstallFailedNoMovedMessageError"
      fi
      if [ -n "${BackupDirBase}" ]; then
         for Dir in $InstallDir; do
            if [ "${Dir}" = "NoInstallDirSpecified" ]; then
               FullTargetDirectory="${TargetDirectory}"
            else
               FullTargetDirectory="${TargetDirectory}/${Dir}"
            fi
            BackupDir="${FullTargetDirectory}/${BackupDirBase}"
            if [ -d "${BackupDir}" ]; then
               PrintCopyText Error "BackupCopyError"
            fi
         done
      fi

      if [ -n "${ManPageDirectory}" ]; then
         for ManPage in $AllManPages; do
            rm -f "${ManPageDirectory}/${ManPage}"
         done
         for ManPage in $ExistingManPages; do
            mv -f "${ManPageDirectory}/${ManPage}.old" "${ManPageDirectory}/${ManPage}"
         done
      fi
   fi

   if [ -n "${InstallerLog}" \
      -a -d "${TargetDirectory}" -a -w "${TargetDirectory}" ]; then
      if [ "${Verbose}" = "true" ]; then
         PrintCopyText Verbose "MovingLogFileVerbose"
      fi
      mv "${InstallerLog}" "${TargetDirectory}/InstallerLog"
   fi

   if [ -d "${TargetDirectory}" -a -w "${TargetDirectory}" ]; then
      cd "${TargetDirectory}"
      for Dir in $InstallDir; do
         if [ "${Dir}" = "NoInstallDirSpecified" ]; then
            InstallTempDir="${InstallTempDirBase}"
         else
            InstallTempDir="${Dir}/${InstallTempDirBase}"
         fi
         if [ -d "${InstallTempDir}" ]; then
            if [ "${Verbose}" = "true" ]; then
               PrintCopyText Verbose "RemovingTempDirVerbose"
            fi
            rm -rf "${InstallTempDir}"
         fi
      done
   fi

   cd "${ExecutionDirectory}"
   
   if [ "${Verbose}" = "true" ]; then
      PrintCopyText Verbose "ExitingVerbose"
   fi

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Exiting Function: CleanUp_() ### >>"
      echo "<< ### Exiting Function: CleanUp_() ### >>" 1>&2
   fi

}

# ----------------------------------------------------------------------------
# Function: CleanUpOnDirtyExit_()

# Trap SIGINT (2). Remove the error log & TargetDirectory before exiting.
CleanUpOnDirtyExit_() {
   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Function: CleanUpOnDirtyExit_() ### >>"
      echo "<< ### Function: CleanUpOnDirtyExit_() ### >>" 1>&2
   fi

   echo ""
   echo ""

   # If we are cleaning up after a sigint make a note in the
   # ErrorFile for CleanUp.
   if [ "${sigint}" != "false" ]; then
      InstallErrors=`cat "${ErrorFile}"`
      if [ -z "`echo \"${InstallErrors}\" | egrep '^SIGINT$'`" ]; then
         echo "SIGINT" 1>&2
      fi

      if [ "${Verbose}" = "true" ]; then
         PrintCopyText Verbose "SigintCleanupVerbose"
      fi
   fi

   # If we made the target directory and it exists remove it.
   if [ "${MakeTargetDirectory}" = "true" -a -n "${TargetDirectoryParent}" \
      -a -d "${NewTargetDirectoryBase}" ]; then
      if [ "${Verbose}" = "true" ]; then
         PrintCopyText Verbose "RemoveTargetDirVerbose"
      fi

      # Remove all directories and files that we have created.
      cd "${TargetDirectoryParent}"
      rm -rf "${NewTargetDirectoryBase}"
   fi

   # If we have added the InstallDirs to the target directory,
   # remove the InstallDirs we have added. 
   if [ -d "${TargetDirectory}" -a -n "${MadeInstallDir}" ]; then
      for Dir in $MadeInstallDir; do
         if [ -d "${Dir}" ]; then
            if [ "${Verbose}" = "true" ]; then
               PrintCopyText Verbose "RemovingInstallDirVerbose"
            fi
            cd "${TargetDirectory}"
            rm -rf "${TargetDirectory}/${Dir}"
         fi
      done
   fi

   if [ -n "${ExecScripts_}" ]; then
      if [ "${MakeExecDirectory}" = "true" \
         -a -n "${ExecDirectoryParent}" -a -d "${NewExecDirectoryBase}" ]; then
         if [ "${Verbose}" = "true" ]; then
            PrintCopyText Verbose "RemoveExecDirVerbose"
         fi
         cd "${ExecDirectoryParent}"
         rm -rf "${NewExecDirectoryBase}"
      elif [ -d "${ExecDirectory}" ]; then
         for Script in $CreatedScriptList; do
            if [ "${Verbose}" = "true" ]; then
               PrintCopyText Verbose "RemoveScriptsVerbose"
            fi
            rm -f "${ExecDirectory}/${Script}"
         done
         if [ "${ScriptAction}" = "Rename" ]; then
            if [ "${Verbose}" = "true" ]; then
               PrintCopyText Verbose "RestoreScriptsVerbose"
            fi
            for Script in $ExistingScriptList; do
               mv -f "${ExecDirectory}/${Script}${AppendToName_}" "${ExecDirectory}/${Script}"
            done
         fi
      fi
   fi

   # Restore previous man pages.
   if [ -n "${ManPageDirectory}" ]; then
      for ManPage in $AllManPages; do
         rm -f "${ManPageDirectory}/${ManPage}"
      done
      for ManPage in $ExistingManPages; do
         mv -f "${ManPageDirectory}/${ManPage}.old" "${ManPageDirectory}/${ManPage}"
      done
   fi

#   if [ -n "${ComponentTarget}" \
#      -a "${ComponentTargetExists}" != "true" \
#      -a "${MakeTargetDirectory}" != "true" ]; then
#      if [ "${Verbose}" = "true" ]; then
#         PrintCopyText Verbose "RemoveComponentDirVerbose"
#      fi
#      rm -rf "${TargetDirectory}/${ComponentTarget}"
#   fi

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Exiting Function: CleanUpOnDirtyExit_() ### >>"
      echo "<< ### Exiting Function: CleanUpOnDirtyExit_() ### >>" 1>&2
   fi

   exit

}

# ----------------------------------------------------------------------------
# Function: CheckDirectory_()

CheckDirectory_() {
   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Function: CheckDirectory_() ### >>"
      echo "<< ### Function: CheckDirectory_() ### >>" 1>&2
   fi

   Directory="${1}"
   if [ -z "${Directory}" ]; then
      echo "CRITICAL FAILURE: CheckDirectory_() Error"
      echo "  \$Directory not defined."
      echo "Critical Failure in CheckDirectory." 1>&2
      sigint="false"
      CleanUpOnDirtyExit_
   fi

   DirectoryOkay_="null"
   MakeDirectory_="null"
   MakeInstallDir_="null"
   ParentFound_="null"

   if [ -d "${Directory}" ]; then
      MakeDirectory_="false"

      if [ "${VarToSet}" = "TargetDirectory" ]; then
         MadeInstallDir=""
      fi

      # First verify that all target directories exist and are writable.
      for Dir in $InstallDir; do
         if [ "${Dir}" = "NoInstallDirSpecified" \
            -o "${VarToSet}" = "ExecDirectory" ]; then
            FullInstallDir="${Directory}"
         else
            FullInstallDir="${Directory}/${Dir}"
         fi

         if [ "${VarToSet}" = "TargetDirectory" \
            -a -w "${Directory}" -a ! -d "${FullInstallDir}" ]; then
            DirectoryList=`echo "${Dir}" | tr '/' ' '`
            CompositeDir=""
            for PartialDir in $DirectoryList; do
               CompositeDir="${CompositeDir}${PartialDir}/"
               if [ ! -d "${Directory}/${CompositeDir}" ]; then
                  mkdir "${Directory}/${CompositeDir}" > /dev/null 2>&1
                  if [ "${?}" != "0" ]; then
                     DirectoryOkay_="false"
                     ans="y"

                     for MadeDir in $MadeInstallDir; do
                        rmdir "${Directory}/${MadeDir}"
                     done
                     MadeInstallDir=""
                     break
                  else
                     if [ -z "${MadeInstallDir}" ]; then
                        MadeInstallDir="${CompositeDir}"
                     else
                        MadeInstallDir="${CompositeDir} ${MadeInstallDir}"
                     fi
                  DirectoryOkay_="true"
                  fi # $?
               fi # -d CompositeDir
            done # PartialDir
         fi # $VarToSet = TargetDirectory

         if [ -w "${FullInstallDir}" -a "${DirectoryOkay_}" != "false" ]; then
            touch "${FullInstallDir}/thisisatest" > /dev/null 2>&1
            if [ "${?}" != "0" ]; then
               DirectoryOkay_="false"
               ans="y"

               for MadeDir in $MadeInstallDir; do
                  rmdir "${Directory}/${MadeDir}"
               done
               MadeInstallDir=""
               break
            else
               rm "${FullInstallDir}/thisisatest"
               DirectoryOkay_="true"
            fi
         else
            DirectoryOkay_="false"
            ans="y"
         fi

         if [ "${VarToSet}" != "TargetDirectory" ]; then
            break
         fi
      done

      # Next determine what message to print.
      MsgToPrint="None"
      for Dir in $InstallDir; do
         if [ "${VarToSet}" != "TargetDirectory" ]; then
            break
         fi

         if [ "${Dir}" = "NoInstallDirSpecified" ]; then
            FullInstallDir="${Directory}"
         else
            FullInstallDir="${Directory}/${Dir}"
         fi

         if [ "${DirectoryOkay_}" = "true" \
            -a "${VarToSet}" = "TargetDirectory" ]; then

            CurrentInstallationDir="${FullInstallDir}"
            CurrentInstallation="${CurrentInstallationDir}/.Revision"
            CurrentInstallationCreationID="${CurrentInstallationDir}/.CreationID"
            CurrentInstallationInfo="${CurrentInstallationDir}/.info"
            CurrentInstallationManifest="${CurrentInstallationDir}/.install-manifest"
            VersionsMatch="null"
            case "${MsgToPrint}" in
               FilesExist)
                  # This is the most generic message we can print.
                  # If it is already selected break out of the loop.
                  break;;
               ThankYouForUpgrading)
                  if [ -f "${CurrentInstallationRevision}" \
                  	 -o -f "${CurrentInstallationCreationID}" ]; then
                     MsgToPrint="FilesExist"
                     VersionsMatch="false"
                  fi;;
               VersionAlreadyInstalled)
                  if [ -f "${CurrentInstallationInfo}" \
                     -o -f "${CurrentInstallationManifest}" ]; then
                     VersionsMatch="false"
                  fi
                  if [ -f "${CurrentInstallationRevision}" ]; then
                     CurrentVersion=`grep 'FullVersionNumber:' \
                     "${CurrentInstallationRevision}" | awk '{print $2}'`
                  elif [ -f "${CurrentInstallationCreationID}" ]; then
                  	   CurrentVersion=`grep 'FullVersionNumber:' \
                       "${CurrentInstallationCreationID}" | awk '{print $2}'`
                  fi
                  if [ "${CurrentVersion}" != "${FullVersionNumber}" \
                  	 -o "${ProductName}" != "Mathematica" ]; then
                     VersionsMatch="false"
                  else
                     VersionsMatch="false"
                  fi
                  if [ "${VersionsMatch}" = "false" ]; then
                     MsgToPrint="FilesExist"
                  fi;;
               *)
                  if [ -f "${CurrentInstallationRevision}" ]; then
                  	 CurrentVersion=`grep 'FullVersionNumber:' \
                  	 "${CurrentInstallationRevision}" | awk '{print $2}'`
                  elif [ -f "${CurrentInstallationCreationID}" ]; then
                  	   CurrentVersion=`grep 'FullVersionNumber:' \
                       "${CurrentInstallationCreationID}" | awk '{print $2}'`
                  elif [ -f "${CurrentInstallationInfo}" \
                       -o -f "${CurrentInstallationManifest}" ]; then
                       MsgToPrint="ThankYouForUpgrading"
                       VersionsMatch="false"
                  else
                 	 if [ "${MsgToPrint}" = "None" -a \
                        -z "`ls -a1 "${CurrentInstallationDir}" \
                         | grep -v '^\.$' | grep -v '^\.\.$'`" ]; then
                       	MsgToPrint="None"
                        VersionsMatch="false"
                     else
                        MsgToPrint="FilesExist"
                        VersionsMatch="false"
                     fi 
                  fi
                  if [ -f "${CurrentInstallationRevision}" \
                  	 -o -f "${CurrentInstallationCreationID}" ]; then
                  	 if [ "${CurrentVersion}" = "${FullVersionNumber}" \
                    	-a "${ProductName}" = "Mathematica" ]; then
                     	MsgToPrint="VersionAlreadyInstalled"
                     	VersionsMatch="true"
                  	 else
                    	MsgToPrint="FilesExist"
                    	VersionsMatch="false"
                 	 fi
                  fi;;    
            esac
         fi # DirectoryOkay
      done # Dir

      if [ "${DirectoryOkay_}" = "true" \
         -a "${VarToSet}" = "TargetDirectory" ]; then

         if [ "${MsgToPrint}" != "None" ]; then

            PrintCopyText Text "${MsgToPrint}IntroductionText"

            if [ "${Automatic}" = "false" ]; then
               ans=""
               Overwriteans=""
            elif [ "${Overwriteans}" = "y" \
               -o "${Overwriteans}" = "Y" ]; then
               Overwriteans="y"
            elif [ "${Overwriteans}" = "n" \
               -o "${Overwriteans}" = "N" ]; then
               Overwriteans="n"
               for MadeDir in $MadeInstallDir; do
                  rmdir "${Directory}/${MadeDir}"
               done
               MadeInstallDir=""
               PrintCopyText Error "OverwriteExitError"
               echo "${CopyText_}" 1>&2
               sigint="false"
               CleanUpOnDirtyExit_
            elif [ "${Overwriteans}" != "" ]; then
               for MadeDir in $MadeInstallDir; do
                  rmdir "${Directory}/${MadeDir}"
               done
               MadeInstallDir=""
               PrintCopyText Error "BadOverwriteansError"
               echo "${CopyText_}" 1>&2
               sigint="false"
               CleanUpOnDirtyExit_
            fi

            while [ "${Overwriteans}" != "y" \
               -a "${Overwriteans}" != "n" ]; do

               if [ "${VersionsMatch}" = "true" ]; then
                  PrintCopyText Prompt "OverwriteFilesPrompt" ""
               else
                  PrintCopyText Prompt "OverwriteDirectoryPrompt" ""
               fi

               if [ "${Automatic}" = "false" ]; then
                  read Overwriteans
               else
                  Overwriteans=${Overwriteans:="y"}
                  echo "${Overwriteans}"
               fi

               echo ""
               if [ "${Overwriteans}" = "n" \
                  -o "${Overwriteans}" = "N" ]; then
                  Overwriteans="n"
                  DirectoryOkay_="false"
                  for MadeDir in $MadeInstallDir; do
                     rmdir "${Directory}/${MadeDir}"
                  done
                  MadeInstallDir=""
                  if [ "${VersionsMatch}" = "true" ]; then
                     userexit="true"
                     sigint="false"
                     CleanUpOnDirtyExit_
                  fi
               elif [ "${Overwriteans}" = "y" \
                  -o "${Overwriteans}" = "Y" ]; then
                  Overwriteans="y"
               fi
            done
         fi # MsgToPrint
      fi # DirectoryOkay_
   else
      MakeDirectory_="true"
      Parent_="${Directory}"
      while [ "${ParentFound_}" != "true" ]; do
         NewDirectoryBase="${Parent_}"
         Parent_=`dirname "${Parent_}"`
         DirectoryParent="${Parent_}"
         if [ -d "${Parent_}" ]; then
            ParentFound_="true"
            touch "${Parent_}/thisisatest" > /dev/null 2>&1
            if [ "${?}" != "0" ]; then
               DirectoryOkay_="false"
               ans="y"
            else
               rm "${Parent_}/thisisatest"
               DirectoryOkay_="true"
            fi
         else
            ParentFound_="false"
         fi
         if [ "${DEBUG}" = "true" ]; then
            echo "<< \$Parent_ set to ${Parent_}. >>"
            echo "<< \$DirectoryOkay_ set to ${DirectoryOkay_}. >>"
         fi
      done
   fi # Directory

   if [ "${MakeDirectory_}" = "true" \
      -a "${Directory}" != "${DefaultDirectory}" \
      -a "${DirectoryOkay_}" = "true" ]; then

      if [ "${Automatic}" = "false" ]; then
         ans=""
         Overwriteans=""
      elif [ "${ans}" = "y" -o "${ans}" = "Y" ]; then
         ans="y"
      elif [ "${ans}" = "n" -o "${ans}" = "N" ]; then
         ans="n"
         PrintCopyText Error "ansExitError"
         echo "${CopyText_}" 1>&2
         sigint="false"
         CleanUpOnDirtyExit_
      elif [ "${ans}" != "" ]; then
         PrintCopyText Error "BadansError"
         echo "${CopyText_}" 1>&2
         sigint="false"
         CleanUpOnDirtyExit_
      fi
   
      while [ "${ans}" != "y" -a "${ans}" != "n" ]; do
      
         PrintCopyText Prompt "CreateDirectoryPrompt" ""

         if [ "${Automatic}" = "false" ]; then
            read ans
         else
            ans=${ans:="y"}
            echo "${ans}"
         fi
         echo ""

         if [ "${ans}" = "n" -o "${ans}" = "N" ]; then
            ans="n"
            DirectoryOkay_="null"
         elif [ "${ans}" = "y" -o "${ans}" = "Y" ]; then
            ans="y"
         fi
      done
   fi

   Defaultans=""
   Directory=""
   MsgToPrint=""

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Exiting Function: CheckDirectory_() ### >>"
      echo "<< ### Exiting Function: CheckDirectory_() ### >>" 1>&2
   fi

}

# ----------------------------------------------------------------------------
# Function: CheckSpace_()

CheckSpace_() {
   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Function: CheckSpace_() ### >>"
      echo "<< ### Function: CheckSpace_() ### >>" 1>&2
   fi

   Directory="${1}"
   if [ "${DEBUG}" = "true" ]; then
      echo "<< Directory set to \"${Directory}\". >>"
   fi
   if [ -n "`echo \"${Directory}\" | egrep '^/'`" ]; then
           testdir="${Directory}"
   else
           testdir="`pwd`/${Directory}"
   fi

   while [ ! -d "${testdir}" ]; do
           testdir=`dirname "${testdir}"`
   done

   case "${DefaultSystemID}" in
      "AIX-Power64"|"HPUX-PA64"|IRIX-MIPS64)
              dfcommand="df -kP";;
      *)
              dfcommand="df -k";;
   esac

   dfresults=`${dfcommand} "${testdir}" | sed "1d" | tr '\n' ' ' | awk '{print $4}'`

   if [ "${DEBUG}" = "true" ]; then
      echo "<< dfresults set to ${dfresults} >>"
      echo "<< InstallSize set to ${InstallSize} >>"
   fi

   if [ -z "${dfresults}" ]; then
      echo "CRITICAL ERROR: dfresults not defined."
      echo "Critical Failure in CheckSpace_()" 1>&2
      sigint="false"
      CleanUpOnDirtyExit_
   fi

   if [ -z "${InstallSize}" ]; then
      echo "CRITICAL ERROR: InstallSize not defined."
      echo "Critical Failure in CheckSpace_()" 1>&2
      sigint="false"
      CleanUpOnDirtyExit_
   fi

   if [ ${dfresults} -ge ${InstallSize} ]; then
      SpaceToInstall=1
   fi

   if [ ${SpaceToInstall} -eq 0 -a "${Automatic}" = "true" ]; then
      PrintCopyText Error "NoSpaceAutomaticError"
      echo "${CopyText_}" 1>&2
      sigint="false"
      CleanUpOnDirtyExit_
   fi

   # Clean up
   dfcommand=""
   dfresults=""
   testdir=""
   
   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Exiting Function: CheckSpace_() ### >>"
      echo "<< ### Exiting Function: CheckSpace_() ### >>" 1>&2
   fi

}

# ----------------------------------------------------------------------------
# Function: CopyManPages()

CopyManPages() {
   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Function: CopyManPages() ### >>"
      echo "<< ### Function: CopyManPages() ### >>" 1>&2
   fi

   # Possible man page directories. 
   DefaultManPageDir="/usr/man/man1"
   LinuxManPageDir="/usr/share/man/man1"
   SGIManPageDir="/usr/share/catman/u_man/cat1"
   LocalManPageDir="/usr/local/man/man1"

   for Dir in $InstallDir; do    
     if [ "${Dir}" = "NoInstallDirSpecified" ]; then
         ManSrcDir="${TargetDirectory}/SystemFiles/SystemDocumentation/Unix/"
      else
         ManSrcDir="${TargetDirectory}/${Dir}/SystemFiles/SystemDocumentation/Unix/"
      fi
      if [ -d "${ManSrcDir}" ]; then
         ManPages=`ls -1 "${ManSrcDir}"`
      fi
      if [ -n "${AllManPages}" ]; then
         AllManPages="${AllManPages} ${ManPages}"
      else
         AllManPages="${ManPages}"
      fi

      if [ -n "${ManPages}" -a -z "${ManPageDirectory}" ]; then
         if [ ! -w "${DefaultManPageDir}" ]; then
            if [ ! -w "${LinuxManPageDir}" ]; then
               if [ ! -w "${SGIManPageDir}" ]; then
                  if [ ! -w "${LocalManPageDir}" ]; then
                     ManPageDirectory=""
                  else
                     ManPageDirectory="${LocalManPageDir}"
                  fi
               else
                  ManPageDirectory="${SGIManPageDir}"
               fi
            else
               ManPageDirectory="${LinuxManPageDir}"
            fi
         else
            ManPageDirectory="${DefaultManPageDir}"
         fi
      fi

      if [ -n "${ManPageDirectory}" ]; then
         for ManPage in $ManPages; do
            if [ -f "${ManPageDirectory}/${ManPage}" ]; then
               if [ -n "${ExistingManPages}" ]; then
                  ExistingManPages="${ExistingManPages} ${ManPage}"
               else
                  ExistingManPages="${ManPage}"
               fi
               mv -f "${ManPageDirectory}/${ManPage}" "${ManPageDirectory}/${ManPage}.old"
            fi
            cp -f "${ManSrcDir}/${ManPage}" "${ManPageDirectory}/${ManPage}"
         done
      fi
   done

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Exiting Function: CopyManPages() ### >>"
      echo "<< ### Exiting Function: CopyManPages() ### >>" 1>&2
   fi

}

# ----------------------------------------------------------------------------
# Function: GetDefaultSystemID_()

GetDefaultSystemID_() {
   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Function: GetDefaultSystemID_() ### >>"
      echo "<< ### Function: GetDefaultSystemID_() ### >>" 1>&2
   fi

   Default64SystemID=""
   case `uname -s` in
      AIX)
         DefaultSystemID="AIX-Power64";;
      Darwin)
         DefaultSystemID="MacOSX";;
      HP-UX)
         DefaultSystemID="HPUX-PA64";;
      IRIX64)
         DefaultSystemID="IRIX-MIPS64";;
      Linux)
         case `uname -m` in
            ia64)
               DefaultSystemID="Linux-IA64";;
            i?86)
               DefaultSystemID="Linux";;
            x86_64)
               DefaultSystemID="Linux";;
            *)
               DefaultSystemID="Unknown";;
         esac;;
      SunOS)
         case `uname -m` in
            sun*)
               DefaultSystemID="Solaris-SPARC";;
            i86pc)
               DefaultSystemID="Solaris-x86-64";;
            *)
               DefaultSystemID="Unknown";;
         esac;;
      *)
         DefaultSystemID="Unknown";;
   esac

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Exiting Function: GetDefaultSystemID_() ### >>"
      echo "<< ### Exiting Function: GetDefaultSystemID_() ### >>" 1>&2
   fi

}

# ----------------------------------------------------------------------------
# Function: GetDirectory()

GetDirectory() {
   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Function: GetDirectory() ### >>"
      echo "<< ### Function: GetDirectory() ### >>" 1>&2
   fi

   VarToSet="${1}"

   DirectoryOkay_="null"

   if [ "${Automatic}" = "true" ]; then
      DirSetTo=`eval echo '${'${VarToSet}'}'`
   else
      DirSetTo=""
   fi

   DefaultDirectory=`eval echo '${Default'${VarToSet}'}'`
   DirectoryBase="${DirSetTo}"

   if [ -z "${DefaultDirectory}" ]; then
      echo "CRITICAL FAILURE: GetDirectory() Error"
      echo "  \$Default${VarToSet} not defined."
      echo "Critical Failure in GetDirectory." 1>&2
      sigint="false"
      CleanUpOnDirtyExit_
   fi

   while [ "${DirectoryOkay_}" != "true" ]; do
      PrintCopyText Prompt "${VarToSet}Prompt" "${DefaultDirectory}"

      if [ "${Automatic}" = "false" ]; then
         read DirectoryBase
      else
         echo "${DirectoryBase}"
      fi

      echo ""
      DirectoryBase=${DirectoryBase:="$DefaultDirectory"}
      eval "${VarToSet}Base=\"${DirectoryBase}\""

      if [ "${DirectoryBase}" != "/" ]; then
         DirectoryBase=`echo "${DirectoryBase}" | \
            sed -e 's/\/$//'`
      fi

      CheckDirectory_ "${DirectoryBase}"

      if [ "${ans}" = "y" -o "${Overwriteans}" = "y" ]; then
         if [ "${DirectoryOkay_}" = "false" ]; then
            if [ "${MakeDirectory_}" = "true" ]; then
               PrintCopyText Error "CreateDirectoryError"
            else
               PrintCopyText Error "WriteToDirectoryError"
            fi
            PrintCopyText Error "MayNeedRootError"
         fi
      fi

      if [ "${DirectoryOkay_}" != "true" \
         -a "${Automatic}" = "true" ]; then
         echo "${CopyText_}" 1>&2
         sigint="false"
         CleanUpOnDirtyExit_
      fi
   done

   # Set variables for the directory, the directory's pre-existing
   # parent directory, and the lowest created directory.
   eval "${VarToSet}=\"${DirectoryBase}\""
   eval "${VarToSet}Parent=\"${DirectoryParent}\""
   eval "New${VarToSet}Base=\"${NewDirectoryBase}\""

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ${VarToSet} set to \"${DirectoryBase}\". >>"
      echo "<< ${VarToSet}Parent set to \"${DirectoryParent}\". >>"
      echo "<< New${VarToSet}Base set to \"${NewDirectoryBase}\". >>"
   fi

   if [ "${MakeDirectory_}" = "true" ]; then
      eval "Make${VarToSet}=\"true\""
   fi

   DefaultDirectory=""
   DirectoryBase=""
   DirectoryParent=""
   DirSetTo=""
   NewDirectoryBase=""
   VarToSet=""

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Exiting Function: GetDirectory() ### >>"
      echo "<< ### Exiting Function: GetDirectory() ### >>" 1>&2
   fi

}

# ----------------------------------------------------------------------------
# Function: InfoInitialize()

InfoInitialize() {
   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Function: InfoInitialize() ### >>"
      echo "<< ### Function: InfoInitialize() ### >>" 1>&2
   fi

   # Get all installation options and information from the info files.
   ProductName=`grep '^ProductName' ${ProductInfoFileList} | \
      awk '{print $2}' | sort | uniq`

   OrderList ProductName "Mathematica"

   ProductTitle=`grep '^ProductTitle' ${ProductInfoFileList} | \
      tr -s ' ' | sed -e 's/^.*ProductTitle //g' | \
      sort | uniq | tr '\n' ' '`
   ProductTitle=`echo "${ProductTitle}" | sed -e 's/ $//g'`
   
   FullProductName=`grep '^FullProductName' ${ProductInfoFileList} | \
      tr -s ' ' | sed -e 's/^.*FullProductName //g' | \
      sort | uniq | tr '\n' ' '`
   FullProductName=`echo "${FullProductName}" | sed -e 's/ $//g'`
   
   VersionNumber=`grep '^VersionNumber' ${ProductInfoFileList} | \
      awk '{print $2}' | sort | uniq | tr '\n' ' ' | \
      tr -s ' '`
   VersionNumber=`echo "${VersionNumber}" | sed -e 's/^ //g' | \
      sed -e 's/ $//g'`

   ReleaseNumber=`grep '^ReleaseNumber' ${ProductInfoFileList} | \
      awk '{print $2}' | sort | uniq | tr '\n' ' ' | \
      tr -s ' '`
   ReleaseNumber=`echo "${ReleaseNumber}" | sed -e 's/^ //g' | \
      sed -e 's/ $//g'`

   MinorReleaseNumber=`grep '^MinorReleaseNumber' ${ProductInfoFileList} \
      | awk '{print $2}' | sort | uniq | tr '\n' ' ' | \
      tr -s ' '`
   MinorReleaseNumber=`echo "${MinorReleaseNumber}" | sed -e 's/^ //g' | \
      sed -e 's/ $//g'`

   CopyrightYearList=`grep '^CopyrightYear' ${ProductInfoFileList} | \
      awk '{print $2}' | sort | uniq | tr '\n' ' ' | tr -s ' '`
   CopyrightYearList=`echo "${CopyrightYearList}" | sed -e 's/ $//g'`
   CopyrightYear=`echo "${CopyrightYear}" | sed -e 's/^ //g' | \
      sed -e 's/ $//g' | sed -e 's/ /, /g'`

   CreationID=`grep '^CreationID' ${ProductInfoFileList} | \
      awk '{print $2}' | sort | uniq | tr '\n' ' ' | \
      tr -s ' '`
   CreationID=`echo "${CreationID}" | sed -e 's/^ //g' | sed -e 's/ $//g'`

   SystemIDList=`grep '^SystemID' ${ProductInfoFileList} | \
      awk '{print $2}' | sed -e 's/^None//g' | sort | \
      uniq | tr '\n' ' '`
   SystemIDList=`echo "${SystemIDList}" | sed -e 's/^ //g' | \
      sed -e 's/ $//g'`

   OrderList SystemIDList "${DefaultSystemID}"

   IDList=`grep '^InstallID' ${ProductInfoFileList} | \
      awk '{print $2}' | sed -e 's/^None//g' | sort | \
      uniq | tr '\n' ' '`

   if [ "${DefaultSystemID}" = "Linux-x86-64" ]; then
	OrderList IDList "Linux"
   else
	OrderList IDList "${DefaultSystemID}"
   fi 

   LanguageList=`grep '^Language' ${ProductInfoFileList} | \
      awk '{print $2}' | sort | uniq | tr '\n' ' '`
   LanguageList=`echo "${LanguageList}" | sed -e 's/^ //g' | \
      sed -e 's/ $//g'`

   OrderList LanguageList "${DefaultLanguage}"

   FeatureList=`grep '^Feature' ${ProductInfoFileList} | \
      awk '{print $2}' | tr ',' ' ' | tr ' ' '\n' | \
      sort | uniq | tr '\n' ' '`

   OrderList FeatureList "Full"

   ExecScriptsList=`grep '^ExecScripts' ${ProductInfoFileList} | \
      tr -s ' ' | sed -e 's/^.*ExecScripts //g' | \
      sort | uniq | tr '\n' ' '`
   ExecScriptsList=`echo "${ExecScriptsList}" | sed -e 's/ $//g'`

   InstallBase=`grep '^InstallBase' ${ProductInfoFileList} | \
      awk '{print $2}' | sort | uniq | \
      sed -e 's/~/\$\{HOME\}/g' | sed '1p;d'`
   
   InstallBase=`eval echo "${InstallBase}"`

   if [ "${DefaultTargetDirectory}" = "" ]; then
      DefaultTargetDirectory="${InstallBase}"
   fi

   InstallDir=`grep '^InstallDir' ${ProductInfoFileList} | \
      awk '{print $2}' | grep -v '^$' | sort | uniq | tr '\n' ' '`
   InstallDir=`echo "${InstallDir}" | tr -s ' ' | sed -e 's/^ //g' |
      sed -e 's/ $//g'`

   if [ -z "${InstallDir}" ]; then
      InstallDir="NoInstallDirSpecified"
   fi
   
   DefaultExecDirectory=`grep '^DefaultExecDirectory' ${ProductInfoFileList} | \
      awk '{print $2}' | grep -v '^$' | sort | uniq | tr '\n' ' '`
   DefaultExecDirectory=`echo "${DefaultExecDirectory}" | tr -s ' ' | sed -e 's/^ //g' |
      sed -e 's/ $//g'`
      
   if [ -z "${DefaultExecDirectory}" ]; then
      DefaultExecDirectory="/usr/local/bin"
   fi
   
   PasswordFile=`grep '^PasswordFile' ${ProductInfoFileList} | \
      awk '{print $2}' | grep -v '^$' | sort | uniq | tr '\n' ' '`
   PasswordFile=`echo "${PasswordFile}" | tr -s ' ' | sed -e 's/^ //g' |
      sed -e 's/ $//g'`
      

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ProductName set to \"${ProductName}\". >>"
      echo "<< ProductTitle set to \"${ProductTitle}\". >>"
      echo "<< FullProductName set to \"${FullProductName}\". >>"
      echo "<< VersionNumber set to \"${VersionNumber}\". >>"
      echo "<< ReleaseNumber set to \"${ReleaseNumber}\". >>"
      echo "<< MinorReleaseNumber set to \"${MinorReleaseNumber}\". >>"
      echo "<< CopyrightYearList set to \"${CopyrightYearList}\". >>"
      echo "<< CreationID set to \"${CreationID}\". >>"
      echo "<< SystemIDList set to \"${SystemIDList}\". >>"
      echo "<< IDList set to \"${IDList}\". >>"
      echo "<< LanguageList set to \"${LanguageList}\". >>"
      echo "<< FeatureList set to \"${FeatureList}\". >>"
      echo "<< ExecScriptsList set to \"${ExecScriptsList}\". >>"
      echo "<< InstallBase set to \"${InstallBase}\". >>"
      echo "<< InstallDir set to \"${InstallDir}\". >>"
   fi

   if [ -n "`echo ${VersionNumber} | awk '{print $2}'`" \
      -o -n "`echo ${ReleaseNumber} | awk '{print $2}'`" \
      -o -n "`echo ${MinorReleaseNumber} | awk '{print $2}'`" \
      ]; then
      echo "CRITICAL FAILURE: InfoInitialize() Error"
      echo "  Version number mismatch."
      echo "Critical Failure in InfoInitialize. Version number mismatch." 1>&2 
      sigint="false"
      CleanUpOnDirtyExit_
   fi

   FullVersionNumber="${VersionNumber}.${ReleaseNumber}.${MinorReleaseNumber}"

   if [ -n "`echo ${CreationID} | awk '{print $2}'`" ]; then
      echo "CRITICAL FAILURE: InfoInitialize() Error"
      echo "  CreationID mismatch."
      echo "Critical Failure in InfoInitialize. Creation ID mismatch." 1>&2
      sigint="false"
      CleanUpOnDirtyExit_
   fi

   # clean up
   Feature=""
   InfoFindPath_=""
   InfoFile_=""
   InfoFileList_=""
   Language=""
   ProductNameMatch_=""
   SystemID=""
   InstallID=""

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Exiting Function: InfoInitialize() ### >>"
      echo "<< ### Exiting Function: InfoInitialize() ### >>" 1>&2
   fi

}

# ----------------------------------------------------------------------------
# Function: Initialize()

Initialize() {
   umask 022

   # Set Flags to initial values
   Help="false"
   sigint="true"
   userexit="false"

   if [ -z "${InstallerPath}" ]; then
      echo "CRITICAL FAILURE: Fundamental Error"
      echo "  \$InstallerPath not defined."
      echo "Critical Failure in Initialize." 1>&2 
      sigint="false"
      CleanUpOnDirtyExit_
   fi

   # Make sure a TERM is defined.
   if [ "${TERM}" = "unknown" -o "${TERM}" = "dumb" -o -z "${TERM}" ]; then
      TERM="vt100"
      export TERM
   fi

   # Default term width (for early error messages).
   TermWidth=80
   TWmin=75

   # Create the ErrorFile. It will be removed by CleanUp_() if it
   # is empty when the install finishes.
   ErrorFile="/tmp/InstallErrors-$$"
   touch "${ErrorFile}"
   exec 2>"${ErrorFile}"

   GetDefaultSystemID_

   # Get text from the greeting file until the installer language selection

   GreetingFile=`ls -1 "${InstallerPath}/TextResources" | grep "Greeting"`

   InstallerTitle="${InstallerPath}/TextResources/${GreetingFile}"
   InstallerText="${InstallerPath}/TextResources/${GreetingFile}"
   InstallerError="${InstallerPath}/TextResources/${GreetingFile}"
   InstallerPrompt="${InstallerPath}/TextResources/${GreetingFile}"
   InstallerVerbose="${InstallerPath}/TextResources/${GreetingFile}"

   if [ ! -f "${InstallerTitle}" ]; then
      echo "CRITICAL FAILURE: Fundamental Error"
      echo " Installer text not found."
      echo "Critical Failure in Initialize." 1>&2
      sigint="false"
      CleanUpOnDirtyExit_
   fi

   # Check for gunzip
   gunzip --version > /dev/null 2>&1
   if [ "${?}" != "0" ]; then
      PrintCopyText Text "GunzipError"
      echo "${CopyText_}" 1>&2
      sigint="false"
      CleanUpOnDirtyExit_
   fi

   # The user and group id are used later to fix ownership
   # permissions on the installed files.
   InstallUser=`id -u`
   InstallGroup=`id -g`

   # see what echo does and set ${n} and ${c} accordingly
   echo "test\c" | grep -s c > /dev/null 2>&1
   if [ "${?}" != "0" ]; then
      n=""
      c="\c"
   else
      n="-n"
      c=""
   fi

   # Find the info files.
   InfoFindPath_=`dirname "${InstallerPath}"`
   InfoFileList_=`find "${InfoFindPath_}" -name "info" -print`
   ProductInfoFileList=`echo "${InfoFileList_}" | sed -e 's/^ //g'` 

   if [ -z "${ProductInfoFileList}" ]; then
      echo "CRITICAL FAILURE: Initialize() Error"
      echo "  No info files found."
      echo "Critical Failure in Initialize." 1>&2 
      sigint="false"
      CleanUpOnDirtyExit_
   fi

   # Set the file name of the install archives.
   ArchiveFileName="contents.tar.gz"

   # Initialize space test.
   SpaceToInstall=0

   # Clear all install variables.
   AllManPages=""
   ans=""
   ExecDirectory=""
   ExistingManPages=""
   InstallerLanguage=""
   InstallFeature=""
   InstallLanguage=""
   InstallIDList=""
   InstallSystemIDList=""
   ManPageDirectory=""
   TargetDirectory=""
   
}

# ----------------------------------------------------------------------------
# Function: InstallProduct()

InstallProduct() {
   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Function: InstallProduct() ### >>"
      echo "<< ### Function: InstallProduct() ### >>" 1>&2
   fi

   # Just to let people know we're doing something...
   PrintCopyText Text "NowInstallingText"

   DotTextWidth_=0
   TotalDots_=0
   # Print the open and close [] for the dots to go between
   if [ "${Silent}" != "true" ]; then
      if [ "${Verbose}" != "true" \
         -o "${DefaultSystemID}" = "DEC-AXP" \
         -o "${DefaultSystemID}" = "HPUX-PA64" \
         -o "${DefaultSystemID}" = "IRIX-MIPS64" \
         -o "${DefaultSystemID}" = "MacOSX" ]; then
         Spacing_=`expr ${TermWidth} - 3`
         WhiteSpace_=`printf "%-${Spacing_}s"`
         DotText_="[${WhiteSpace_}]"
         DotTextWidth_=`echo "${DotText_}" | wc -c`
         ExtraTextSpace=`expr ${TermWidth} - ${DotTextWidth_}`
         Spacing_=`expr ${ExtraTextSpace} / 2`
         WhiteSpace_=`printf "%-${Spacing_}s"`

         echo $n "${WhiteSpace_}${DotText_}$c"

         DotTextWidth_=`expr ${DotTextWidth_} - 2`
         BackupSpace_=`printf "%-${DotTextWidth_}s" | tr ' ' '\b'`
         echo $n "${BackupSpace_}$c"
         DotTextWidth_=`expr ${DotTextWidth_} - 1`
         TotalDots_=${DotTextWidth_}
      fi
   fi

   InstallSize=0
   InstallInfoFileList=""

   # From $ProductInfoFileList, we need to extract only the info files
   # that contain the InstallIDs from $InstallIDList, the Language
   # from $InstallLanguage, and the Feature from $InstallFeature.
   # We will call this new info file list $InstallInfoFileList.
   for InfoFile_ in ${ProductInfoFileList}; do

      TestLanguage_=`grep '^Language ' "${InfoFile_}" | awk '{print $2}'`
      if [ "${InstallLanguage}" != "${TestLanguage_}" ]; then
         continue
      fi

      TestInstallID_=`grep '^InstallID ' "${InfoFile_}" | awk '{print $2}'`

      TestInstallIDList_=`echo "${InstallIDList}" | sed -e 's/ /::/g'`
      TestInstallIDList_=":${TestInstallIDList_}:"
      InstallIDCheck_=`echo "${TestInstallIDList_}" | \
         sed -e 's/:'"${TestInstallID_}"'://g'`
      if [ "${InstallIDCheck_}" = "${TestInstallIDList_}" ]; then
         continue
      fi
      TestFeatureList_=`grep "Feature " "${InfoFile_}" | \
                awk '{print $2}' | tr ',' ' '`
      TestFeatureList_=" ${TestFeatureList_} "

      FeatureCheck_="${InstallFeature}"
      for TestFeature_ in $TestFeatureList_; do
         FeatureCheck_=`echo "${FeatureCheck_}" | \
            sed -e 's/'"${TestFeature_}"'//g'`
      done
      if [ "${FeatureCheck_}" = "${InstallFeature}" ]; then
         continue
      fi

      InfoDir_=`dirname "${InfoFile_}"`
      InstallSizeTemp_=`gunzip -l "${InfoDir_}"/*.gz | sed "2p;d" | awk '{ print $2 }'`
      InstallSize=`expr ${InstallSizeTemp_} / 1024 + ${InstallSize} + 1`
      InfoFile_=`echo "${InfoFile_}" | tr ' ' ':'`
      InstallInfoFileList="${InstallInfoFileList} ${InfoFile_}"
   done

   CheckSpace_ "${TargetDirectory}"

   if [ ${SpaceToInstall} -eq 1 ]; then

      InstallInfoFileList=`echo "${InstallInfoFileList}" | sed -e 's/^ //g'`

      # Create $TargetDirectory if necessary.
      if [ "${MakeTargetDirectory}" = "true" ]; then
         if [ ! -d "${TargetDirectory}" ]; then
            mkdir -p "${TargetDirectory}"
         fi
      fi
      
      TargetDirectory=`cd "${TargetDirectory}"; pwd`

      # Make the InstallDirs if necessary.
      for Dir in $InstallDir; do
         if [ ! -d "${TargetDirectory}/${Dir}" \
            -a "${Dir}" != "NoInstallDirSpecified" ]; then
            mkdir -p "${TargetDirectory}/${Dir}"
         fi
      done

      # Backup files in the TargetDirector(y/ies).
      BackupComplete="null"
      if [ "${Overwriteans}" = "y" -a "${VersionsMatch}" != "true" ]; then
         BackupDirBase="BackupDir-$$"
         for Dir in $InstallDir; do
            if [ "${Dir}" = "NoInstallDirSpecified" ]; then
               BackupDir="${TargetDirectory}/${BackupDirBase}"
            else
               BackupDir="${TargetDirectory}/${Dir}/${BackupDirBase}"
            fi
            FullTargetDirectory=`dirname "${BackupDir}"`
            FileName=`ls -a1 "${FullTargetDirectory}" | grep -v '^\.$' | \
               grep -v '^\.\.$' | sort | sed '1p;d'`
            if [ -n "${FileName}" ]; then
               mkdir "${BackupDir}"

               if [ "${Verbose}" = "true" ]; then
                  PrintCopyText Verbose "MovingFilesToBackupVerbose"
               fi

#               CurrentDir=`pwd`
#               cd "${FullTargetDirectory}"
#               mv ${FileName} ${BackupDirBase}/.
#               cd "${CurrentDir}"

               FileName=`ls -a1 "${FullTargetDirectory}" | \
                  grep -v '^\.$' | grep -v '^\.\.$' | \
                  grep -v "^${BackupDirBase}$" | sort | \
                  sed '1p;d'`
               while [ -n "${FileName}" ]; do
                  if [ "${Verbose}" = "true" ]; then
                     PrintCopyText Verbose "MovingFileVerbose"
                  fi

                  mv "${FullTargetDirectory}/${FileName}" "${BackupDir}/."
                  FileName=`ls -a1 "${FullTargetDirectory}" | \
                     grep -v '^\.$' | grep -v '^\.\.$' | \
                     grep -v "^${BackupDirBase}$" | sort | \
                     sed '1p;d'`
               done
            fi
         done
         BackupComplete="true"
      else
         BackupDirBase=""
      fi

      # Make the temporary installation director(y/ies).
      InstallTempDirBase=".InstallTemp-$$"
      for Dir in $InstallDir; do
         if [ "${Dir}" = "NoInstallDirSpecified" ]; then
            FullInstallTempDir="${TargetDirectory}/${InstallTempDirBase}"
         else
            FullInstallTempDir="${TargetDirectory}/${Dir}/${InstallTempDirBase}"
         fi

         if [ ! -d "${FullInstallTempDir}" ]; then
            mkdir "${FullInstallTempDir}"
         fi
      done

      ComponentTargetList_=""
      LastInfoFile_=`echo "${InstallInfoFileList}" | tr ' ' '\n' | \
               tail -n 1 | tr ':' ' '`

      # Extract the files from the product archives.
      for InfoFile_ in $InstallInfoFileList; do

         InfoFile_=`echo "${InfoFile_}" | tr ':' ' '`
         SourceDirectory_=`dirname "${InfoFile_}"`

         # If we are using multiple installation directories, we need
         # to determine which temporary directory to put files in.
         if [ "${InstallDir}" != "NoInstallDirSpecified" ]; then
            InstallTempDir=`grep "^InstallDir" "${InfoFile_}" | \
               awk '{print $2}' | tr -s ' ' | sed -e 's/^ //g' | \
               sed -e 's/ $//g'`
            FullInstallTempDir="${TargetDirectory}/${InstallTempDir}/${InstallTempDirBase}"
         fi

         cd "${FullInstallTempDir}"

         FullTargetDirectory=`dirname "${FullInstallTempDir}"`

         InstallSizeTemp_=`gunzip -l "${SourceDirectory_}/${ArchiveFileName}" \
            | sed "2p;d" | awk '{ print $2 }'`
         InstallSizeTemp_=`expr ${InstallSizeTemp_} / 1024`

         ComponentTarget=`gunzip -c "${SourceDirectory_}/${ArchiveFileName}" | \
            tar -tf - | grep '^[A-Za-z0-9]' | tr '/' ' ' | \
            awk '{print $1}' | sort | uniq`

         ComponentNameList=""

         # This will eventually be used for better clean up on exit...
         if [ "${InstallDir}" = "NoInstallDirSpecified" ]; then
            ComponentTargetList_="${ComponentTargetList_} ${ComponentTarget}"
            export ComponentTargetList_
            export ComponentNameList
         else
            ComponentName=`echo "${InstallTempDir}" | sed -e 's/\///g' | \
		sed -e 's/ //g'`
            eval "${ComponentName}ComponentTargetList_=\"`echo '${'${ComponentName}'ComponentTargetList_}'` ${ComponentTarget}\""
            export `echo "${ComponentName}ComponentTargetList_"`
            ComponentNameList="${ComponentNameList} ${ComponentName}"
            export ComponentNameList
         fi

         for Target in $ComponentTarget; do
            if [ -d "${FullTargetDirectory}/${Target}" \
               -a "${Overwriteans}" != "y" ]; then
               ComponentTargetExists="true"
               echo ""
               echo ""
               PrintCopyText Error "ComponentTargetError"
               echo "${CopyText_}" 1>&2
               sigint="false"
               CleanUpOnDirtyExit_
            elif [ -d "${FullTargetDirectory}/${Target}" \
               -a "${VersionsMatch}" != "true" ]; then
               rm -rf "${FullTargetDirectory}/${Target}"
            fi
         done

         if [ "${Verbose}" = "true" \
            -a "${DefaultSystemID}" != "DEC-AXP" \
            -a "${DefaultSystemID}" != "HPUX-PA64" \
            -a "${DefaultSystemID}" != "IRIX-MIPS64" \
            -a "${DefaultSystemID}" != "MacOSX" ]; then
            gunzip -c "${SourceDirectory_}/${ArchiveFileName}" | tar -xvmf -
         else
            gunzip -c "${SourceDirectory_}/${ArchiveFileName}" | tar -xmf -

            if [ "${Silent}" != "true" ]; then
               if [ "${InfoFile_}" = "${LastInfoFile_}" ]; then
                  Dots_=`printf "%-${DotTextWidth_}s" | tr ' ' '*'`
                  DotTextWidth_=0
               else
                  Dots_=`expr ${InstallSize} / ${TotalDots_}`
                  Dots_=`expr ${InstallSizeTemp_} / ${Dots_}`
                  DotTextWidth_=`expr ${DotTextWidth_} - ${Dots_}`
                  Dots_=`printf "%-${Dots_}s" | tr ' ' '*'`
               fi

               if [ ${DotTextWidth_} -eq 0 ]; then
                  echo "${Dots_}"
                  echo ""
               else
                  echo ${n} "${Dots_}${c}"
               fi
            fi
         fi

         # This should never happen, but just to be sure...
         if [ -f "${ArchiveFileName}" ]; then
            if [ "${DEBUG}" = "true" ]; then
               echo "<< Removing \"${ArchiveFileName}\". >>"
            fi
            rm -f "${ArchiveFileName}"
         fi
      done # InfoFile_

      for Dir in $InstallDir; do
         if [ "${InstallDir}" = "NoInstallDirSpecified" ]; then
            FullInstallTempDir="${TargetDirectory}/${InstallTempDirBase}"
         else
            FullInstallTempDir="${TargetDirectory}/${Dir}/${InstallTempDirBase}"
         fi

         FullTargetDirectory=`dirname "${FullInstallTempDir}"`

         if [ "${DEBUG}" = "true" ]; then
            echo "<< Changing file owner to \"${InstallUser}\" and group to \"${InstallGroup}\". >>"
         fi
         
         if [ "${DefaultSystemID}" = "HPUX-PA64" ]; then
            chgrp -hR "${InstallGroup}" "${FullInstallTempDir}"
            chown -hR "${InstallUser}" "${FullInstallTempDir}"
         else
            chgrp -fhR "${InstallGroup}" "${FullInstallTempDir}"
            chown -fhR "${InstallUser}" "${FullInstallTempDir}"
         fi

         if [ "${InstallDir}" = "NoInstallDirSpecified" ]; then
            ComponentTargetList="${ComponentTargetList_}"
         else
            ComponentName=`echo "${Dir}" | sed -e 's/\///g' | sed -e 's/ //g'`
            ComponentTargetList=`eval echo '${'${ComponentName}'ComponentTargetList_}'`
         fi

         ComponentTargetList=`echo "${ComponentTargetList}" | tr ' ' '\n' |  \
            sort | uniq | tr '\n' ' '`
         ComponentTargetList=`echo "${ComponentTargetList}" | \
            sed -e 's/^ //g' | sed -e 's/ $//g'`

         for ComponentTarget_ in ${ComponentTargetList}; do
            if [ "${Verbose}" = "true" ]; then
               PrintCopyText Verbose "MoveComponentDirVerbose"
            fi
            if [ "${VersionsMatch}" = "true" ]; then
               cp -pfR "${FullInstallTempDir}/${ComponentTarget_}" "${FullTargetDirectory}" 1>&2
            else
               mv -f "${FullInstallTempDir}/${ComponentTarget_}" "${FullTargetDirectory}" 1>&2
            fi
         done
      
         echo "FullVersionNumber: ${FullVersionNumber}" > "${FullTargetDirectory}/.Revision"
         echo "CreationID: ${CreationID}" >> "${FullTargetDirectory}/.Revision"

         echo "${CreationID}" > "${FullTargetDirectory}/.CreationID"
         echo "${FullVersionNumber}" > "${FullTargetDirectory}/.VersionID"
         
      done

      # If we are running Linux or Linux-x86-64, check selinux settings.
      if [ "${DefaultSystemID}" = "Linux" \
         -o "${DefaultSystemID}" = "Linux-x86-64" ]; then
         # If selinuxenabled returns with 0 status it is enabled.
         SELinuxEnabled=`selinuxenabled 2>/dev/null;echo ${?}`
         if [ -d "${FullTargetDirectory}/SystemFiles/Libraries"  \
               -a "${SELinuxEnabled}" = "0" ]; then

            if [ "${Automatic}" = "false" ]; then
               SELinux=""
            elif [ "${SELinux}" = "y" -o "${SELinux}" = "Y" ]; then
               SELinux="y"
            elif [ "${SELinux}" = "n" -o "${SELinux}" = "N" ]; then
               SELinux="n"
            else
               PrintCopyText Error "BadSELinuxAutomaticError"
               SELinux="n"
            fi

            PrintCopyText Text "SELinuxText"
            PrintCopyText Prompt "SELinuxPrompt"
            if [ "${Automatic}" = "true" ]; then
               echo "${SELinux}"
            fi
            while [ "${SELinux}" != "y" -a "${SELinux}" != "n" ]; do
               read SELinux
               if [ "${SELinux}" = "y" -o "${SELinux}" = "Y" ]; then
                  SELinux="y"
               elif [ "${SELinux}" = "n" -o "${SELinux}" = "N" ]; then
                  SELinux="n"
               else
                  PrintCopyText Error "BadSELinuxError"
                  echo ""
                  PrintCopyText Text "SELinuxText"
                  PrintCopyText Prompt "SELinuxPrompt"
               fi
            done

            echo ""

            # Fix settings for the included Mathematica libraries.
            if [ "${SELinux}" = "y" ]; then
               chconFailure=`find "${FullTargetDirectory}" -name "*.so*" -exec chcon -t textrel_shlib_t {} \; 2>/dev/null;echo ${?}`
            fi
            if [ "${SELinux}" = "n" -o "${chconFailure}" != "0" ]; then
               PrintCopyText Error "ChconError"
               SELinux="n"
            fi
         fi
      fi
   else
      PrintCopyText Error "NoSpaceError"
   fi

   cd "${ExecutionDirectory}"

   # Clean up
   ComponentTarget=""
   ComponentTarget_=""
   ComponentTargetList=""
   Dots_=""
   DotTextWidth_=""
   InfoDir_=""
   InstallInfoFileList=""
   InstallSize=""
   InstallSizeTemp_=""
   TotalDots_=""
   WhiteSpace_=""

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Exiting Function: InstallProduct() ### >>"
      echo "<< ### Exiting Function: InstallProduct() ### >>" 1>&2
   fi

}

# ----------------------------------------------------------------------------
# Function: MakeExecLinks()

MakeExecLinks() {
   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Function: MakeExecLinks() ### >>"
      echo "<< ### Function: MakeExecLinks() ### >>" 1>&2
   fi

   ExecScripts_=""
   for Dir in InstallDir; do

      if [ "${InstallDir}" = "NoInstallDirSpecified" ]; then
         if [ -d "${TargetDirectory}/Executables" ]; then
	    if [ -n "${ExecScriptsList}" ]; then
	       ExecScripts_="${ExecScriptsList}"
	    else
               ExecScripts_=`ls -1 "${TargetDirectory}/Executables"`
	    fi
         fi
      else
         if [ -d "${TargetDirectory}/${Dir}/Executables" ]; then
	    if [ -n "${ExecScriptsList}" ]; then
	       ExecScripts_="${ExecScriptsList}"
	    else
               ExecScripts_=`ls -1 "${TargetDirectory}/${Dir}/Executables"`
	    fi
         fi
      fi

      if [ -z "${AllExecScripts}" ]; then
         AllExecScripts="${ExecScripts_}"
      else
         AllExecScripts="${AllExecScripts} ${ExecScripts_}"
      fi
   done

   if [ -n "${AllExecScripts}" ]; then

      GetDirectory "ExecDirectory"

      if [ "${DirectoryOkay_}" = "true" ]; then
         if [ "${MakeExecDirectory}" = "true" ]; then
            mkdir -p "${ExecDirectory}"
         fi

         if [ -n "${ExecDirectory}" ]; then
            ExecDirectory=`cd "${ExecDirectory}"; pwd`
         fi

         ExistingScriptList=""
         ScriptExists=""
         for Script_ in $AllExecScripts; do
            ScriptExists=`ls -1 "${ExecDirectory}" | egrep "^${Script_}$"`
            ExistingScriptList=`echo "${ExistingScriptList} ${ScriptExists}" | tr -s ' ' | sed -e 's/^ //g' | sed -e 's/ $//g'`
         done

         if [ "${ExistingScriptList}" = "" ]; then
            CreatedScriptList=""
            for Dir in $InstallDir; do
               if [ "${InstallDir}" = "NoInstallDirSpecified" ]; then
                  FullTargetDirectory="${TargetDirectory}"
               else
                  FullTargetDirectory="${TargetDirectory}/${Dir}"
               fi
               if [ -d "${FullTargetDirectory}/Executables" ]; then
	          if [ -n "${ExecScriptsList}" ]; then
		     ExecScripts_="${ExecScriptsList}"
                  else
                     ExecScripts_=`ls -1 "${FullTargetDirectory}/Executables"`
		  fi
               fi
               for Script_ in $ExecScripts_; do
                  ln -sf "${FullTargetDirectory}/Executables/${Script_}" "${ExecDirectory}/${Script_}"
                  CreatedScriptList="${CreatedScriptList} ${Script_}"
               done
            done
         else
            ModifyOkay="true"
            for Script_ in $ExistingScriptList; do
               if [ ! -w "${ExecDirectory}/${Script_}" \
                  -a ! -h "${ExecDirectory}/${Script_}" ]; then
                  ModifyOkay="false"
               fi
            done
            if [ "${ModifyOkay}" = "true" ]; then

               DisplayExistingScriptList=`echo "${ExistingScriptList}" | sed -e "s/^/'/g" | sed -e "s/$/'/g" | sed -e "s/ /', '/g"`

               SelectionScriptActionList="Overwrite Rename"
               SelectFromList Single "ScriptAction"
               
               if [ "${ScriptAction}" = "Rename" ]; then
                  DefaultAppendToName_=".old"

                  PrintCopyText Prompt "NewScriptNamePrompt" "${DefaultAppendToName_}"
                  if [ "${Automatic}" = "false" ]; then
                     read AppendToName_
                  else
                     echo ""
                  fi
                  echo ""
                  AppendToName_=${AppendToName_:="$DefaultAppendToName_"}
                  for Script_ in $ExistingScriptList; do
                     mv "${ExecDirectory}/${Script_}" "${ExecDirectory}/${Script_}${AppendToName_}"
                  done
               fi
               for Script_ in $ExistingScriptList; do
                  rm -f "${ExecDirectory}/${Script_}"
               done
               CreatedScriptList=""
               for Dir in $InstallDir; do
                  if [ "${InstallDir}" = "NoInstallDirSpecified" ]; then
                     FullTargetDirectory="${TargetDirectory}"
                  else
                     FullTargetDirectory="${TargetDirectory}/${Dir}"
                  fi
                  if [ -d "${FullTargetDirectory}/Executables" ]; then
		     if [ -n "${ExecScriptsList}" ]; then
		        ExecScripts_="${ExecScriptsList}"
		     else
                        ExecScripts_=`ls -1 "${FullTargetDirectory}/Executables"`
		     fi
                  fi
                  for Script_ in $ExecScripts_; do
                     ln -sf "${FullTargetDirectory}/Executables/${Script_}" "${ExecDirectory}/${Script_}"
                     CreatedScriptList="${CreatedScriptList} ${Script_}"
                  done
               done
            else
               PrintCopyText Error "FileExistsError"
            fi
         fi
      fi
      echo ""

   fi

   DefaultAppendToName_=""
   Dir=""
   DirectoryOkay_=""
   FullTargetDirectory=""
   Script_=""
   ScriptExists=""
}

# ----------------------------------------------------------------------------
# Function: MakeMathPass()

MakeMathPass() {

   if [ "${ProductName}" = "Mathematica" \
      -a "${Automatic}" != "true" ]; then

      ValidPasswordExists=""
      for TestSystemID in ${DefaultSystemID}; do
            
         if [ -n "`echo \" ${InstallIDList} \" | \
            grep \" ${TestSystemID} \"`" ]; then

            KernelOutput=`"${ExecDirectory}/MathKernel" -runfirst "Exit[]" &`
            if [ -n "${KernelOutput}" ]; then
               KernelOutput=`echo "${KernelOutput}" | grep 'Enter your password:'`
            else
               ValidPasswordExists="error"
            fi

            if [ "${ValidPasswordExists}" != "error" ]; then
               if [ -z "${KernelOutput}" ]; then
                  ValidPasswordExists="true"
               else
                  ValidPasswordExists="false"
               fi
            fi

            if [ "${DEBUG}" = "true" ]; then
               echo "<< \$ValidPasswordExists set to \"${ValidPasswordExists}\". >>"
               echo "<< \$TestSystemID set to \"${TestSystemID}\". >>"
            fi

            break
         fi

      done

      if [ "${DEBUG}" = "true" ]; then
         echo "<< \$TestSystemID set to \"${TestSystemID}\". >>"
      fi

      if [ "${ValidPasswordExists}" = "false" ]; then

         ValidOption="false"

         while [ "${ValidOption}" != "true" ]; do

            PrintCopyText Text "MathpassIntroductionText"

            PrintCopyText Text "MathpassFirstSelectionText"

            PrintCopyText Text "MathpassSecondSelectionText"

            PrintCopyText Text "MathpassThirdSelectionText"

            DefaultAction=1
            PrintCopyText Prompt "MathpassActionPrompt" "${DefaultAction}"

            read Action
            echo ""

            Action=${Action:="$DefaultAction"}

            if [ -n "`echo \"${Action}\" | grep '[^0-9]'`" ]; then
               PrintCopyText Error "NumericInputExpectedError"
            elif [ -n "`echo \"${Action}\" | awk '{print $2}'`" ]; then
               PrintCopyText Error "SingleInputError"
            elif [ "${Action}" = "1" ]; then
               ValidOption="true"
               Action="Single"
            elif [ "${Action}" = "2" ]; then
               ValidOption="true"
               Action="Network"
            elif [ "${Action}" = "3" ]; then
               ValidOption="true"
               Action="Later"
            else
               Item=${Action}
               PrintCopyText Error "ItemNotFoundError"
            fi

         done

         if [ -z "${TestSystemID}" \
            -a "${Action}" = "Single" ]; then

            PrintCopyText Error "PleaseRunKernelError"

         elif [ "${Action}" = "Single" ]; then
            if [ ! -x "${ExecDirectory}/MathKernel" ]; then
               PrintCopyText Error "KernelNotFoundError"
            else
               PrintCopyText Text "ConfiguringPasswordText"
               "${ExecDirectory}/MathKernel" -run "Exit[]"
            fi
         elif [ "${Action}" = "Network" ]; then
            ServerName=""
            while [ -z "${ServerName}" ]; do
               PrintCopyText Prompt "LicenseServerPrompt" ""
               read ServerName
               echo ""
            done
            mathpass="${TargetDirectory}/Configuration/Licensing/mathpass"
            mkdir -p "${TargetDirectory}/Configuration/Licensing"
            PrintCopyText Text "ConfiguringNetworkPasswordText"
            echo "!${ServerName}" >> "${mathpass}"
         fi
      elif [ "${ValidPasswordExists}" = "true" ]; then
         PrintCopyText Text "ValidMathpassFoundText"
      else # ValidPasswordExists = error
         PrintCopyText Error "RunningMathKernelError"
         exit
      fi
   fi

}

# ----------------------------------------------------------------------------
# Function: OrderList()

OrderList() {

   ListToSort="${1}"

   InitialList=`eval echo '${'${ListToSort}'}'`

   SortedList=""

   DefaultHead="${2}"

   for Item in ${DefaultHead}; do

      Item=`echo "${InitialList}" | tr ' ' '\n' | \
         grep "^${Item}$" | tr '\n' ' '`
      Item=`echo "${Item}" | sed -e 's/^ //g' | \
         sed -e 's/ $//g'`

      InitialList=`echo "${InitialList}" | tr ' ' '\n' | \
         grep -v "^${Item}$" | tr '\n' ' '`

      SortedList="${SortedList} ${Item}"
   done

   SortedList=`echo "${SortedList} ${InitialList}" | \
      tr -s ' ' | sed -e 's/^ //g' | sed -e 's/ $//g'`

   eval "${ListToSort}=\"${SortedList}\""

   DefaultHead=""
   InitialList=""
   ListToSort=""
   SortedList=""

}

# ----------------------------------------------------------------------------
# Function: ParseInput()

ParseInput() {
   Input="$@"

   Verbose="false"
   DEBUG="false"
   Silent="false"
   Automatic="false"

   DefaultTargetDirectory=""
   DefaultInstallerLanguage=`echo "${GreetingFile}" | \
      tr '.' ' ' | awk '{print $2}'`
   DefaultLanguage="${DefaultInstallerLanguage}"
   InstallerLog=""

   i=1
   Option=`echo "${Input}" | sed -e 's/^-//' | \
      eval \`echo "awk 'BEGIN { FS=\" -\" } { print $"${i}" }'"\``

   while [ -n "${Option}" ]; do
      InputType=""
      InputVar="Junk"
      case "${Option}" in
         auto)
            Automatic="true"
            ;;
         createdir=*)
            InputVar="ans"
            ;;
         debug)
            DEBUG="true"
            Verbose="true"
            ;;
         execdir=*)
            InputVar="ExecDirectory"
            ;;
         help)
            PrintHelpScreen
            ;;
         installerlanguage=*)
            InputVar="InstallerLanguage"
            InputType="Single"
            ;;
         language=*)
            InputVar="InstallLanguage"
            InputType="Single"
            ;;
         method=*)
            InputVar="InstallFeature"
            ;;
         overwrite=*)
            InputVar="Overwriteans"
            ;;
         platforms=*)
            InputVar="InstallIDList"
            ;;
         products=*)
            InputVar="InstallProductList"
            ;;
         selinux=*)
            InputVar="SELinux"
            ;;
         silent)
            Silent="true"
            Automatic="true"
            InstallerLog="/tmp/InstallerLog-$$"
            exec 1>"${InstallerLog}"
            ;;
         targetdir=*)
            InputVar="TargetDirectory"
            ;;
         verbose)
            Verbose="true"
            ;;
         *)
            PrintCopyText Error "BadOptionError"
            PrintHelpScreen
            ;;
      esac

      OptionInput="`echo \"${Option}\" | awk 'BEGIN { FS=\"=\" } { print $2 }' | tr ',' ' '`"

      eval "${InputVar}=\"${OptionInput}\""

      ItemCount=`echo \`eval echo '${'${InputVar}'}'\` | wc -w`

      if [ "${InputType}" = "Single" -a ${ItemCount} -gt 1 ]; then
         Option=`echo "${Option}" | awk 'BEGIN { FS=\"=\" } { print $1 }'`
         PrintCopyText Error "SingleOptionExpectedError"
         echo "${CopyText_}" 1>&2
         sigint="false"
         CleanUpOnDirtyExit_
      fi
      i=`expr ${i} + 1`
      
      Option=`echo "${Input}" | sed -e 's/^-//' | \
      eval \`echo "awk 'BEGIN { FS=\" -\" } { print $"${i}" }'"\``
      
   done

   if [ "${Automatic}" = "true" ]; then
      TermWidth=80
   else
      TermWidth=`stty -a | tr ';' '\n' | grep 'columns' | tr ' ' '\n' | grep '[1-9]' 2>/dev/null` 
      if [ -z "${TermWidth}" ]; then
         TermWidth=80
      fi
   fi

   if [ -n "`echo ${TermWidth} | grep '[^0-9]'`" ]; then
      TermWidth=80
   fi

   if [ ${TermWidth} -lt 50 ]; then
      TermWidth=50
   fi

   TWmin=`expr ${TermWidth} - 5`

   # clear the screen before we go on
   if [ "${DEBUG}" != "true" -a "${Silent}" != "true" ]; then
      clear
   fi

   i=""
   ItemCount=""
   Input=""
   InputType=""
   InputVar=""
   Junk=""
   Option=""
   OptionInput=""

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Exiting Function: ParseInput() ### >>"
      echo "<< ### Exiting Function: ParseInput() ### >>" 1>&2
   fi

}

# ----------------------------------------------------------------------------
# Function: PrintCopyText()

PrintCopyText() {
   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Function: PrintCopyText() ### >>"
      echo "<< ### Function: PrintCopyText() ### >>" 1>&2
   fi

   CopyTextType_="${1}"
   
   InputFile="Installer${1}"
   InputFile=`eval echo '${'${InputFile}'}'`

   CopyText_=`awk '/:::'"${2}"':::/ {getline; while (substr($1,1,3) != ":::") {print; getline}}' < "${InputFile}"`

   PromptDefault_="${3}"

   CopyText_=`eval echo "\"${CopyText_}\""`

   case "${CopyTextType_}" in
   "Error")
      if [ "${DefaultSystemID}" = "MacOSX" ]; then
         echo "${CopyText_}" | fmt ${TWmin} ${TermWidth}
      else
         echo "${CopyText_}" | fmt -${TermWidth}
      fi
      echo "";;
   "Prompt")
      if [ "${DefaultSystemID}" = "MacOSX" ]; then
         echo "${CopyText_}" | fmt ${TWmin} ${TermWidth}
      else
         echo "${CopyText_}" | fmt -${TermWidth}
      fi
      echo ${n} "> ${c}";;
   "Text")
      if [ "${CopyText_}" != "" ]; then
         if [ "${DefaultSystemID}" = "MacOSX" ]; then
            echo "${CopyText_}" | fmt ${TWmin} ${TermWidth}
         else
            echo "${CopyText_}" | fmt -${TermWidth}
         fi
         echo ""
      fi;;
   "Title")
      TitleWidth_=`echo "${CopyText_}" | wc -c`
      ExtraBannerSpace_=`expr ${TermWidth} - ${TitleWidth_}`
      Spacing_=`expr ${ExtraBannerSpace_} / 2`
      WhiteSpace_=`printf "%-${Spacing_}s"`
      echo "${WhiteSpace_}${CopyText_}";;
   "Verbose")
      if [ "${DefaultSystemID}" = "MacOSX" ]; then
         echo "<< ${CopyText_} >>" | fmt ${TWmin} ${TermWidth}
      else
         echo "<< ${CopyText_} >>" | fmt -${TermWidth}
      fi;;
   esac

   # Clean up
   CopyTextType_=""
   PromptDefault_=""
   TitleWidth_=""
   ExtraBannerWidth_=""
   Spacing_=""
   WhiteSpace_=""

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Exiting Function: PrintCopyText() ### >>"
      echo "<< ### Exiting Function: PrintCopyText() ### >>" 1>&2
   fi

}

# ----------------------------------------------------------------------------
# Function: PrintHelpScreen()

PrintHelpScreen() {
   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Function: PrintHelpScreen() ### >>"
      echo "<< ### Function: PrintHelpScreen() ### >>" 1>&2
   fi

   Help="true"

   InstallerText="${InstallerPath}/TextResources/${DefaultInstallerLanguage}/Text"

   if [ "${DEBUG}" = "true" ]; then
      echo "InstallerText set to ${InstallerText}"
   fi

   PrintCopyText Text "HelpScreenText"

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Exiting Function: PrintHelpScreen() ### >>"
      echo "<< ### Exiting Function: PrintHelpScreen() ### >>" 1>&2
   fi

   exit

}

# ----------------------------------------------------------------------------
# Function: PrintIntroduction()

PrintIntroduction() {
   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Function: PrintIntroduction() ### >>"
      echo "<< ### Function: PrintIntroduction() ### >>" 1>&2
   fi

   # fail if $ProductTitle is not defined.
   if [ -z "${ProductTitle}" ]; then
      echo "CRITICAL FAILURE: PrintIntroduction() Error"
      echo "  \$ProductTitle not defined."
      echo "Critical failure in PrintIntroduction." 1>&2
      sigint="false"
      CleanUpOnDirtyExit_
   fi

   # fail if $FullProductName is not defined.
   if [ -z "${FullProductName}" ]; then
      echo "CRITICAL FAILURE: PrintIntroduction() Error"
      echo "  \$FullProductName not defined."
      echo "Critical failure in PrintIntroduction." 1>&2
      sigint="false"
      CleanUpOnDirtyExit_
   fi

   # fail $TermWidth is not defined.
   if [ -z "${TermWidth}" ]; then
      echo "CRITICAL FAILURE: PrintIntroduction() Error"
      echo "  \$TermWidth not defined."
      echo "Critical failure in PrintIntroduction." 1>&2
      sigint="false"
      CleanUpOnDirtyExit_
   fi

   Line_=`printf "%-${TermWidth}s" | tr ' ' '-'`

   echo "${Line_}"
   PrintCopyText Title "InstallerTitleText"
   echo "${Line_}"
   echo ""

   PrintCopyText Text "CopyrightText"

   # clean up
   Line_=""

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Exiting Function: PrintIntroduction() ### >>"
      echo "<< ### Exiting Function: PrintIntroduction() ### >>" 1>&2
   fi

}

# ----------------------------------------------------------------------------
#   SelectFromList routine

SelectFromList() {
   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Function: SelectFromList() ### >>"
      echo "<< ### Function: SelectFromList() ### >>" 1>&2
   fi

   InputType="${1}"
   
   VarToSelect="${2}"

   SelectedItem=""

   SelectionList=`eval echo '${Selection'${VarToSelect}'List}'`
   
   if [ "${InputType}" = "Multi" ]; then
      OutputVar="${VarToSelect}List"
   else
      OutputVar="${VarToSelect}"
   fi

   if [ "${Automatic}" = "true" ]; then
      Output=`eval echo '${'${OutputVar}'}'`
   else
      Output=""
   fi

   if [ "${Output}" = "" ]; then

      while [ -z "${SelectedItem}" ]; do

         ListLength=`echo ${SelectionList} | wc -w`

         if [ ${ListLength} -gt 1 ]; then
            DisplayList="true"
         elif [ ${ListLength} -eq 1 ]; then
            DisplayList="false"
         fi
         
         if [ "${DisplayList}" = "true" ]; then
         	if [ ${ListLength} -gt 1 ]; then
            	PrintCopyText Text "${VarToSelect}IntroductionText"
            fi
            i=1
            for Item in ${SelectionList}; do
               Item=`echo "${Item}" | sed -e 's/:::/ /g'`

       	       if [ "${ProductName}" = "Mathematica" ]; then
                  case "${Item}" in
                     Linux|Linux-x86-64)
                     	if [ "${ProductTitle}" = "Wolfram Mathematica Player" ]; then
                        	Item="Linux x86 (32-bit)"
                        else
                        	Item="Linux x86 (32-bit and 64-bit)"
                        fi;;
                     HPUX-PA64)
                        Item="HP-UX PA-RISC (64-bit)";;
                     AIX-Power64)
                        Item="IBM AIX Power (64-bit)";;
                     Linux-IA64)
                        Item="Linux Itanium (64-bit)";;
                     IRIX-MIPS64)
                        Item="SGI IRIX (64-bit)";;
                     Solaris-SPARC)
                        Item="Sun Solaris SPARC (64-bit)";;
                     Solaris-x86-64)
                        Item="Sun Solaris x86 (64-bit)";;
                     *)
                        ;;
                  esac
               fi
               echo "  (${i}) ${Item}"
               i=`expr ${i} + 1`
            done
            echo ""
            DefaultItem=1
            SelectionPrompt="${VarToSelect}Prompt"
            PrintCopyText Prompt "${SelectionPrompt}" "${DefaultItem}"
            if [ "${Automatic}" = "false" ]; then
               read ItemSelection
            else
               echo ""
            fi
            echo ""
            Selections=${ItemSelection:="$DefaultItem"}
            Selections=`echo ${Selections} | tr ',' ' ' | \
               tr ' ' '\n' | sort | uniq | tr '\n' ' '`
            InputLength=`echo ${Selections} | wc -w`
            InvalidSelection=`echo ${Selections} | \
                tr ' ' '\n' | grep '[^0-9]'`
            if [ "${InvalidSelection}" != "" ]; then
               PrintCopyText Error "NumericInputExpectedError"
               SelectedItem=""
            elif [ "${InputType}" = "Multi" ]; then
               SelectedList=""
               for Item in ${Selections}; do
                  SelectedItem=`echo ${SelectionList} | tr ' ' '\n' | sed "${Item}p;d"`
                  if [ "${SelectedItem}" = "" ]; then
                     PrintCopyText Error "ItemNotFoundError"
                  else
                     SelectedList=`eval echo "${SelectedItem} ${SelectedList}"`
                  fi
               done
               SelectedItem="${SelectedList}"
            elif [ "${InputType}" = "Single" -a ${InputLength} -gt 1 ]; then
               PrintCopyText Error "SingleInputError"
               SelectedItem=""
            elif [ "${InputType}" = "Single" ]; then
               Item=`eval echo "${Selections}"`
                SelectedItem=`echo "${SelectionList}" | \
                  tr ' ' '\n' | sed "${Item}p;d"`
               if [ "${SelectedItem}" = "" ]; then
                  PrintCopyText Error "ItemNotFoundError"
               fi
            fi
         else
            if [ "${VarToSelect}" = "InstallID" ]; then
               SelectedItem="None ${SelectionList}"
            else
               SelectedItem="${SelectionList}"
            fi
         fi
      done
   else
      ListOkay="1"
      for Item in ${Output}; do
         CheckItem=`echo "${SelectionList}" | grep "${Item}"`
         if [ "${CheckItem}" = "" ]; then
            ListOkay="0"
            PrintCopyText Error "Bad${VarToSelect}Error"
            echo "${CopyText_}" 1>&2
            sigint="false"
            CleanUpOnDirtyExit_
         fi
      done
      SelectedItem="${Output}"
   fi

   if [ "${VarToSelect}" = "InstallID" ]; then
      SelectedItem=`echo "None ${SelectedItem}" | tr ' ' '\n' | \
         sort | uniq | tr '\n' ' '`
   fi

   eval "${OutputVar}=\"${SelectedItem}\""

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ${OutputVar} set to `eval echo '${'${OutputVar}'}'` >>"
   fi

   # Clean up
   CheckItem=""
   DefaultItem=""
   DisplayList=""
   InputType=""
   Item=""
   ListLength=""
   ListOkay=""
   Output=""
   OutputVar=""
   SelectedItem=""
   SelectionList=""
   Selections=""
   VarToSelect=""

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Exiting Function: SelectFromList() ### >>"
      echo "<< ### Exiting Function: SelectFromList() ### >>" 1>&2
   fi

}

# ----------------------------------------------------------------------------
#   SelectInstallerLanguage routine

SelectInstallerLanguage() {
   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Function: SelectInstallerLanguage() ### >>"
      echo "<< ### Function: SelectInstallerLanguage() ### >>" 1>&2
   fi

   SelectionInstallerLanguageList=`ls -1 "${InstallerPath}/TextResources" \
                | grep -v '^Greeting'`

   SelectFromList Single "InstallerLanguage"

   InstallerTitle="${InstallerPath}/TextResources/${InstallerLanguage}/Text"
   InstallerText="${InstallerPath}/TextResources/${InstallerLanguage}/Text"
   InstallerError="${InstallerPath}/TextResources/${InstallerLanguage}/Error"
   InstallerPrompt="${InstallerPath}/TextResources/${InstallerLanguage}/Prompt"
   InstallerVerbose="${InstallerPath}/TextResources/${InstallerLanguage}/Verbose"

   if [ ! -f "${InstallerTitle}" -o ! -f "${InstallerText}" \
      -o ! -f "${InstallerPrompt}" -o ! -f "${InstallerError}" ]; then
      echo "CRITICAL FAILURE: Fundamental Error"
      echo " Installer text not found."
      echo "Critical Failure in SelectInstallerLanguage." 1>&2
      sigint="false"
      CleanUpOnDirtyExit_
   fi

   if [ "${DEBUG}" = "true" ]; then
      echo "<< ### Exiting Function: SelectInstallerLanguage() ### >>"
      echo "<< ### Exiting Function: SelectInstallerLanguage() ### >>" 1>&2
   fi

}

# ----------------------------------------------------------------------------
#   Main installer routine

trap CleanUp_ 0
trap CleanUpOnDirtyExit_ 2

# Make certain that $PATH contains /bin and /usr/bin.
#   /usr/xpg4/bin is for Solaris
#   /usr/contrib/bin is for HP-UX
#   /usr/sbin and /usr/bsd are for IRIX
PATH="/usr/xpg4/bin:/usr/contrib/bin:/bin:/usr/bin:/usr/sbin:/usr/bsd:/usr/local/bin"
CMD_ENV="xpg4"
export PATH

# Who and where am I?
ExecutionDirectory=`pwd`
InstallerScript=`basename "${0}"`
InstallerPath=`dirname "${0}"`
InstallerPath=`cd "${InstallerPath}"; pwd`

Initialize
ParseInput "$@"
InfoInitialize
PrintIntroduction

SelectInstallerLanguage

if [ "${ProductTitle}" = "Wolfram Mathematica Player" ]; then
	PrintCopyText Text "PlayerIntroductionText"
else
	PrintCopyText Text "IntroductionText"
fi

while [ ${SpaceToInstall} != 1 ]; do

   # Get Language to install
   SelectionInstallLanguageList="${LanguageList}"
   SelectFromList Single "InstallLanguage"

   # Get Platform to install for
   SelectionInstallIDList="${IDList}"
   SelectFromList Multi "InstallID"

   # Get Features to install
   SelectionInstallFeatureList="${FeatureList}"
   SelectFromList Single "InstallFeature"

   GetDirectory "TargetDirectory"

   InstallProduct
done

CopyManPages
MakeExecLinks
if [ "${PasswordFile}" != "None" ]; then
	MakeMathPass
fi

exit 0

