#!/bin/sh
#
# vi:set ts=40 sw=2:
#
# Install Script for UNIX Acrobat Exchange and Reader v. 2.0
#
# Copyright (c)1995, Adobe Systems Incorporated
# All Rights Reserved
#
# High Level Design:
#
# Initialization
# Output License to controlling terminal
# Does the user accept the terms and conditions of the license?
#   No: exit
#   Yes: continue
# Enumerate all of the configurations
# If just one configuration
#   Do an easy installation
# else
#   Do a custom installation
#

##########################################################################

echoawk ()
{
  echo $* | awk '{ printf "%s", $0 }'
}

echon ()
{
  echo -n "$*"
}

echoc ()
{
  echo "${*}\c"
}

##########################################################################

yesno()
{
  msg="$1"
  def="$2"
  while true ; do
    echo " "
    $echonl "$msg"
    read answer
    if [ "$answer" ] ; then
      case "$answer" in
        y|Y|yes|YES)
          return 0
          ;;
        n|N|no|NO)
          return 1
          ;;
        *)
          echo " "
          echo "ERROR: Invalid response, expected \"yes\" or \"no\"."
          continue
          ;;
      esac
    else
      return $def
    fi
  done
}

OutputLicense ()
{
  if [ -z "$1" ] ; then
    return
  fi

  if [ ! -f "$1" ] ; then
    echo " "
    echo "ERROR: Cannot find license file ... aborting"
    echo " "
    exit 1
  fi

  if [ -z "$PAGER" ] || [ "`type $PAGER`" = "$PAGER not found" ] ; then
    if [ "`type more`" != "more not found" ] ; then
      command=more
    elif [ "`type pg`" != "pg not found" ] ; then
      command=pg
    else
      command=cat
    fi
  else
    command="$PAGER"
  fi

  echo " "
  "$command" "$1"

  answer=
  while [ -z "$answer" ] ; do
    echo " "
    echo " "
    echo "To accept the terms and conditions of this agreement enter \"accept\"."
    echo "To decline the terms and conditions of this agreement enter \"decline\"."
    echo " "
    $echonl "Do you accept the terms and conditions of this license agreement? "
    read answer
    ucanswer=`echo "${answer}" | tr '[a-z]' '[A-Z]'`
    case "$ucanswer" in
    ACCEPT)
      ;;
    DECLINE)
      echo " "
      echo "License not accepted ... aborting installation"
      echo " "
      exit 1
      ;;
    *)
      echo " "
      echo "ERROR: Invalid response, expected \"accept\" or \"decline\" ... try again"
      answer=
      ;;
    esac
  done
}

PrintRequiredFree()
{
  total=0
  for i in $* ; do
    if [ "$i" -a -f "$i" ] ; then
      size=`ls -lLon "$i" | ( read perm links owner size date ; echo $size )`
      if [ "$size" ] ; then
        total=`expr $total + $size`
      fi
    fi
  done

  megs=`expr $total / 1048576 + 1`

  echo " "
  echo "This installation requires ${megs}MB of free disk space."
}

FilterPathName ()
{
  newpathname="$1"
  case "$newpathname" in
    ~*)
      if [ -f /bin/csh -a -x /bin/csh ] ; then
        newpathname=`/bin/csh -c "echo $newpathname"`
      fi
      ;;
    ../*|./*)
      newpathname=`pwd`/"${newpathname}"
      ;;
    *)
      ;;
  esac
  echo "${newpathname}"
}

ExtractFiles ()
{
  ( cd "$1" ; tar xf "$2" )
}

InstallFiles ()
{
  msg="$1"
  install="$2"
  shift
  shift

  echo " "

  for i in "$@" ; do
    if [ "$i" ] ; then
      if [ "$msg" ] ; then
        $echonl "$msg"
        msg=""
      fi
      ExtractFiles "$install" "$i"
    fi
  done

  if [ -z "$msg" ] ; then
    echo "Done"
  fi
}

InstallBin()
{
  install="$1"

  for i in "$install"/bin/acro*.sh ; do
    if [ -f "$i" ] ; then
      filename="$install"/bin/`basename "$i" .sh`

      ed -s "$i" <<__EOF__
        1,\$s@REPLACE_ME@$install@
        w
        q
__EOF__
      if [ $? != 0 ] ; then
        echo "ERROR installing $filename"
        continue
      fi

      chmod +x "$i"
      if [ $? != 0 ] ; then
        echo "ERROR installing $filename"
        continue
      fi

      mv "$i" "$filename"
      if [ $? != 0 ] ; then
        echo "ERROR installing $filename"
        continue
      fi
    fi
  done
}

GetInstallDirectory ()
{
  if [ -z "$1" ] ; then
    product=Acrobat
  else
    product="$1"
  fi

  var=$2

  case "$product" in
    AcroExch) productName="Acrobat Exchange" ;;
    AcroRead) productName="Acrobat Reader" ;;
    *) productName="Acrobat" ;;
  esac

  defdir="/usr/local/$product"
  case "$OSname" in
    SunOS)
      case "$OSrelease" in
        4.1.3*|4.1.4*) defdir="/usr/$product" ;;
        5.*) defdir="/opt/$product" ;;
      esac
      ;;
    HP-UX)
      case "$OSrelease" in
        *.09.*) defdir="/usr/$product" ;;
        *.10.*) defdir="/opt/$product" ;;
        *) defdir="/opt/$product" ;;
      esac
      ;;
  esac

  while true ; do

    if [ -z "$InstallDir" ] ; then
      InstallDir="$defdir"
    fi

    echo " "
    $echonl "Enter installation directory for $product [${InstallDir}] "
    read answer
    case "$answer" in
      "")
        ;;
      *)
        InstallDir="$answer"
        ;;
    esac

    InstallDir=`FilterPathName "$InstallDir"`

    if [ ! -d "$InstallDir" ] ; then
      echo " "
      $echonl "Directory \"$InstallDir\" does not exist create it now? [y] "
      read answer
      if [ "$answer" ] ; then
        case "$answer" in
          y|Y|yes|YES)
            ;;
          n|N|no|NO)
            echo " "
            echo "Acrobat installation aborted."
            echo " "
            exit 1
            ;;
          *)
            echo " "
            echo "ERROR: Invalid response, expected \"yes\" or \"no\"."
            continue
            ;;
        esac
      fi

      mkdir -p "$InstallDir"

      if [ ! -d "$InstallDir" ] ; then
        echo " "
        echo "ERROR: Cannot make directory \"$InstallDir\"."
        InstallDir=""
        continue;
      fi
    fi

    if ( echo foo > "$InstallDir"/AcroWriteTest ) 2> /dev/null ; then
      rm -f "$InstallDir"/AcroWriteTest
      break
    else
      echo " "
      echo "ERROR: Cannot write to directory \"$InstallDir\"."
      InstallDir=""
      continue
    fi
  done

  eval $var=\"$InstallDir\"
}

CheckSerialNumber()
{
  p1="^[a-zA-Z]\{3\}[0-9]\{3\}[a-zA-Z0-9]\{2\}[0-9]\{6\}-[0-9]\{3\}$"
  p2="^[a-zA-Z]\{3\}[0-9]\{3\}[a-zA-Z0-9]\{2\}[0-9]\{6\}-[0-9]\{3\}-[0-9]\{3\}$"

  if [ `expr "$1" : "$p1"` != 0 ] \
  || [ `expr "$1" : "$p2"` != 0 ] ; then
    return 0
  fi

  return 1
}

GetSerialNumber()
{
  dir="$2"
  file="$2/${1}Data"

  while true ; do
    echo " "
    $echonl "Enter product serial number? "
    read sn
    if CheckSerialNumber "$sn" ; then
      break
    fi
    echo " "
    echo "ERROR: Invalid serial number ... try again"
  done

  echo " "
  $echonl "Enter company name? "
  read co

  if [ ! -d "$dir" ] ; then
    mkdir "$dir"
  fi

  rm -f "$file"
  echo "serialNumber:$sn" > "$file"
  echo "companyName:$co" >> "$file"
}


InstallReader()
{
  ReadConfig="$1"
  ReadPlatformTar="$2"
  SearchPlatformTar="$3"
  CustomPlatformTar="$4"
  dosearch=no

  if [ "$SearchTar" -o "$CustomTar" -o "$SearchPlatformTar" -o "$CustomPlatformTar" ] ; then
    if [ "$SerialNumber" ] ; then
      dosearch=yes
    else
      echo ""
      echo "WARNING: This Acrobat Reader installation does not include Acrobat Search."
      if yesno "Proceed with installation? [y] " 0 ; then
        :
      else
        exit 1
      fi
    fi
  fi

  OutputLicense "$ReadLicense"
  ReadLicense=""

  if [ "$dosearch" = yes ] ; then

    PrintRequiredFree "$ReadTar" "$SearchTar" "$CustomTar" \
      "$ReadPlatformTar" "$SearchPlatformTar" "$CustomPlatformTar"

    if [ -z "$ReadInstallDir" ] ; then
      GetInstallDirectory AcroRead ReadInstallDir
    fi

    InstallFiles "Installing platform independent files ... " \
      "$ReadInstallDir" "$ReadTar" "$SearchTar" "$CustomTar"
    InstallFiles "Installing platform dependent files ... " \
      "$ReadInstallDir" \
      "$ReadPlatformTar" "$SearchPlatformTar" "$CustomPlatformTar"
    InstallBin "$ReadInstallDir"

    ReadTar=""
    SearchTar=""
    CustomTar=""

    if [ "$SerialNumber" ] ; then
      dir="$ReadInstallDir/$ReadConfig"

      if [ ! -d "$dir" ] ; then
        mkdir "$dir"
      fi

      rm -f "$dir/AcroReadData"
      echo "serialNumber:$SerialNumber" > "$dir/AcroReadData"
    fi

  else

    PrintRequiredFree "$ReadTar" "$ReadPlatformTar"

    if [ -z "$ReadInstallDir" ] ; then
      GetInstallDirectory AcroRead ReadInstallDir
    fi

    InstallFiles "Installing platform independent files ... " \
      "$ReadInstallDir" "$ReadTar"
    InstallFiles "Installing platform dependent files ... " \
      "$ReadInstallDir" "$ReadPlatformTar"
    InstallBin "$ReadInstallDir"

    ReadTar=""

  fi
}


InstallExchange()
{
  ExchConfig="$1"
  ExchPlatformTar="$2"
  SearchPlatformTar="$3"
  CustomPlatformTar="$4"

  OutputLicense "$ExchLicense"
  ExchLicense=""

  PrintRequiredFree "$ExchTar $SearchTar $CustomTar" \
    "$ExchPlatformTar" "$SearchPlatformTar" "$CustomPlatformTar"

  if [ -z "$ExchInstallDir" ] ; then
    GetInstallDirectory AcroExch ExchInstallDir
  fi

  GetSerialNumber AcroExch "$ExchInstallDir/$ExchConfig"

  InstallFiles "Installing platform independent files ... " \
    "$ExchInstallDir" "$ExchTar" "$SearchTar" "$CustomTar"
  InstallFiles "Installing platform dependent files ... " \
    "$ExchInstallDir" \
    "$ExchPlatformTar" "$SearchPlatformTar" "$CustomPlatformTar"
  InstallBin "$ExchInstallDir"

  ExchTar=""
  SearchTar=""
  CustomTar=""
}

##############################################################

#
# Initialization:
#

umask 022

ScriptName=`basename $0`
CurrentDirectory=`pwd`
ScriptDirectory=`dirname $0`
case "${ScriptDirectory}" in
  /*) ;;
  .) ScriptDirectory="$CurrentDirectory" ;;
  *) ScriptDirectory="$CurrentDirectory"/"$ScriptDirectory" ;;
esac

if [ "`type uname`" != "uname not found" ] ; then
  OSname=`uname -s`
  OSrelease=`uname -r`
else
  OSname=unknown
  OSrelease=unknown
fi

if [ `echo "x\c"` = "x" ] ; then
  echonl=echoc
else
  echonl=echon
fi

#
# Get the filenames:
#

for i in ${ScriptDirectory}/* ; do
  if [ -f "$i" ] ; then
    case $i in
      */sernum.txt|*/SERNUM.TXT)
        a="`cat "$i" | tr '\015' '\012' | ( read a b ; echo $a )`"
        if CheckSerialNumber "$a" ; then
          SerialNumber="$a"
        fi
        ;;
      */licread.txt|*/LICREAD.TXT)
        ReadLicense="$i"
        ;;
      */licexch.txt|*/LICEXCH.TXT)
        ExchLicense="$i"
        ;;
      */read.tar|*/READ.TAR)
        ReadTar="$i"
        ;;
      */exch.tar|*/EXCH.TAR)
        ExchTar="$i"
        ;;
      */search.tar|*/SEARCH.TAR)
        SearchTar="$i"
        ;;
      */custom.tar|*/CUSTOM.TAR)
        CustomTar="$i"
        ;;
      */ssole.tar|*/SSOLE.TAR)
        ExchSparcSolarisTar="$i"
        ;;
      */ssolr.tar|*/SSOLR.TAR)
        ReadSparcSolarisTar="$i"
        ;;
      */ssols.tar|*/SSOLS.TAR)
        SearchSparcSolarisTar="$i"
        ;;
      */ssolc.tar|*/SSOLC.TAR)
        CustomSparcSolarisTar="$i"
        ;;
      */ssune.tar|*/SSUNE.TAR)
        ExchSparcSunTar="$i"
        ;;
      */ssunr.tar|*/SSUNR.TAR)
        ReadSparcSunTar="$i"
        ;;
      */ssuns.tar|*/SSUNS.TAR)
        SearchSparcSunTar="$i"
        ;;
      */ssunc.tar|*/SSUNC.TAR)
        CustomSparcSunTar="$i"
        ;;
      */hpuxe.tar|*/HPUXE.TAR)
        ExchHppaHpuxTar="$i"
        ;;
      */hpuxr.tar|*/HPUXR.TAR)
        ReadHppaHpuxTar="$i"
        ;;
      */hpuxs.tar|*/HPUXS.TAR)
        SearchHppaHpuxTar="$i"
        ;;
      */hpuxc.tar|*/HPUXC.TAR)
        CustomHppaHpuxTar="$i"
        ;;
    esac
  fi
done


#
# Validate the configurations:
#

NumConfigs=0

if [ "$ExchTar" -a "$ExchLicense" ] ; then
  if [ "$ExchSparcSolarisTar" ] ; then
    NumConfigs=`expr $NumConfigs + 1`
    ExchSparcSolarisNum=$NumConfigs
  fi
  if [ "$ExchSparcSunTar" ] ; then
    NumConfigs=`expr $NumConfigs + 1`
    ExchSparcSunNum=$NumConfigs
  fi
  if [ "$ExchHppaHpuxTar" ] ; then
    NumConfigs=`expr $NumConfigs + 1`
    ExchHppaHpuxNum=$NumConfigs
  fi
fi

if [ "$ReadTar" -a "$ReadLicense" ] ; then
  if [ "$ReadSparcSolarisTar" ] ; then
    NumConfigs=`expr $NumConfigs + 1`
    ReadSparcSolarisNum=$NumConfigs
  fi
  if [ "$ReadSparcSunTar" ] ; then
    NumConfigs=`expr $NumConfigs + 1`
    ReadSparcSunNum=$NumConfigs
  fi
  if [ "$ReadHppaHpuxTar" ] ; then
    NumConfigs=`expr $NumConfigs + 1`
    ReadHppaHpuxNum=$NumConfigs
  fi
fi

if [ "$NumConfigs" = "0" ] ; then
  echo " "
  echo "ERROR: No configurations found ... aborting"
  echo " "
  exit 1
elif [ "$NumConfigs" = "1" ] ; then
  SingleConfig=true
fi

NumConfigs=`expr $NumConfigs + 1`
ExitNum=$NumConfigs


#
# Check if there is a configuration supported by this OS.
#

case "$OSname" in
  SunOS)
    case "$OSrelease" in
      4.1.3*|4.1.4*)
        ExchDefaultNum="$ExchSparcSunNum"
        ReadDefaultNum="$ReadSparcSunNum"
        DefaultName="SPARC/Solaris 1.x"
        ;;
      5.1|5.1.*|5.2|5.2.*)
        ;;
      5.*)
        ExchDefaultNum="$ExchSparcSolarisNum"
        ReadDefaultNum="$ReadSparcSolarisNum"
        DefaultName="SPARC/Solaris 2.x"
        ;;
    esac
    ;;
  HP-UX)
    case "$OSrelease" in
      *.09.*|*.10.*)
        ExchDefaultNum="$ExchHppaHpuxNum"
        ReadDefaultNum="$ReadHppaHpuxNum"
        DefaultName="HP-UX"
        ;;
    esac
    ;;
esac

if [ -z "$ExchDefaultNum" -a -z "$ReadDefaultNum" ] ; then
  echo ""
  echo "WARNING: The current OS ($OSname/$OSrelease) is not supported."
  if yesno "Continue installation? [n] " 1 ; then
    :
  else
    exit 1
  fi
fi


#
# If multiple confurations, get the default configuration
#

if [ -z "$SingleConfig" ] ; then
  if [ "$ExchDefaultNum" ] ; then
    if yesno "Install default configuration, Acrobat Exchange for $DefaultName? [y] " 0 ; then
      DefaultConfig="$ExchDefaultNum"
    fi
  elif [ "$ReadDefaultNum" ] ; then
    if yesno "Install default configuration, Acrobat Reader for $DefaultName? [y] " 0 ; then
      DefaultConfig="$ReadDefaultNum"
    fi
  fi
fi


#
# If a single configuration available the loop will be executed once
# otherwise it will ask for each configuration to install
#

while [ "$NumConfigs" -gt 1 ] ; do

  #
  # If multiple configuration ask for which to install
  #

  if [ "$SingleConfig" ] ; then

    ConfigNum=1

  elif [ "$DefaultConfig" ] ; then

    ConfigNum="$DefaultConfig"

  else

    #
    # Display multiple configurations
    #

    echo " "
    echo "The following configurations are available for installation:"
    echo " "

    if [ "$ExchSparcSolarisNum" ] ; then
      echo "  ${ExchSparcSolarisNum}. Acrobat Exchange for SPARC/Solaris 2.x"
    fi
    if [ "$ExchSparcSunNum" ] ; then
      echo "  ${ExchSparcSunNum}. Acrobat Exchange for SPARC/Solaris 1.x"
    fi
    if [ "$ExchHppaHpuxNum" ] ; then
      echo "  ${ExchHppaHpuxNum}. Acrobat Exchange for HP-UX"
    fi

    if [ "$ReadSparcSolarisNum" ] ; then
        echo "  ${ReadSparcSolarisNum}. Acrobat Reader for SPARC/Solaris 2.x"
    fi
    if [ "$ReadSparcSunNum" ] ; then
        echo "  ${ReadSparcSunNum}. Acrobat Reader for SPARC/Solaris 1.x"
    fi
    if [ "$ReadHppaHpuxNum" ] ; then
        echo "  ${ReadHppaHpuxNum}. Acrobat Reader for HP-UX"
    fi
    if [ "$ExitNum" ] ; then
        echo "  ${ExitNum}. Exit Acrobat installation"
    fi

    echo " "
    $echonl "Enter the configuration number to install: "
    read ConfigNum

  fi


  #
  # Install the configuration
  #

  if [ -z "$ConfigNum" ] ; then

    echo " "
    echo "ERROR: Invalid response ... try again"
    continue

  elif [ "$ConfigNum" = "$ExchSparcSolarisNum" ] ; then

    InstallExchange sparcsolaris "$ExchSparcSolarisTar" \
      "$SearchSparcSolarisTar" "$CustomSparcSolarisTar"
    ExchSparcSolarisNum=""

  elif [ "$ConfigNum" = "$ReadSparcSolarisNum" ] ; then

    InstallReader sparcsolaris "$ReadSparcSolarisTar" \
      "$SearchSparcSolarisTar" "$CustomSparcSolarisTar"
    ReadSparcSolarisNum=""

  elif [ "$ConfigNum" = "$ExchSparcSunNum" ] ; then

    InstallExchange sparcsun "$ExchSparcSunTar" \
      "$SearchSparcSunTar" "$CustomSparcSunTar"
    ExchSparcSunNum=""

  elif [ "$ConfigNum" = "$ReadSparcSunNum" ] ; then

    InstallReader sparcsun "$ReadSparcSunTar" \
      "$SearchSparcSunTar" "$CustomSparcSunTar"
    ReadSparcSunNum=""

  elif [ "$ConfigNum" = "$ExchHppaHpuxNum" ] ; then

    InstallExchange hppahpux "$ExchHppaHpuxTar" \
      "$SearchHppaHpuxTar" "$CustomHppaHpuxTar"
    ExchHppaHpuxNum=""

  elif [ "$ConfigNum" = "$ReadHppaHpuxNum" ] ; then

    InstallReader hppahpux "$ReadHppaHpuxTar" \
      "$SearchHppaHpuxTar" "$CustomHppaHpuxTar"
    ReadHppaHpuxNum=""

  elif [ "$ConfigNum" = "$ExitNum" ] ; then

    break

  else

    echo " "
    echo "ERROR: Invalid response ... try again"
    continue

  fi

  if [ "$DefaultConfig" ] ; then
    break;
  fi

  if [ -z "$MultipleInstall" ] ; then
    break;
  fi

  NumConfigs=`expr $NumConfigs - 1`

done


echo " "
exit 0

