#!/bin/sh
#
# Install Script for UNIX Acrobat Exchange and Reader v. 7.0
#
# Copyright (c)1995-2005 Adobe Systems Incorporated
# All Rights Reserved
#
##########################################################################

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
}

TypeCheck() 
{
  h=`type $1 2> /dev/null`
  case " $h " in
  *' '/*/$1' '*)
    return 0
    ;;
  *)
    return 1
    ;;
  esac
}


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

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

 TypeCheck $PAGER 
  if  [ $? -ne 0 ] || [ -z "$PAGER" ] ; then
    command=cat
    TypeCheck pg
    if [ $? -eq 0 ] ; then 
      command=pg 
    fi
    TypeCheck more
    if [ $? -eq 0 ] ; then 
      command=more 
    fi
  else
      command="$PAGER"
  fi

 
  echo " "
 
  if [ "$command" = "less" ]; then
        command="$command -f"
  fi

  $command "$LicenseFile"

  answer=
  while [ -z "$answer" ] ; do
    echo " "
    echo " "

    if [ "accept" = "accept" ]; then
    	$echonl "Please type \"accept\" to accept the terms and conditions of license agreement; Type \"decline\" to exit. "
    else
    	$echonl "Please type \"accept\" to accept the terms and conditions of license agreement; Type \"decline\" to exit. " |  /usr/bin/tr -cd [:print:]
    fi
    read answer
    case "$answer" in
    "accept" )
      ;;
    "decline" )
      echo " "
      echo "License agreement not accepted ... aborting installation"
      echo " "
      exit 1
      ;;
    *)
      NewAccept=`echo "accept" |  /usr/bin/tr -cd [:print:]`
      NewDecline=`echo "decline" |  /usr/bin/tr -cd [:print:]`
      answer=`echo "$answer" | /usr/bin/tr -cd [:print:]`

      if [ "$answer" = "$NewAccept" ]; then
	echo " "
      elif [ "$answer" = "$NewDecline" ]; then
	echo " "
      	echo "License agreement not accepted ... aborting installation"
      	echo " "
      	exit 1
      else
      	echo " "
	if [ "accept" = "accept" ]; then
		echo "Invalid response, expected \"accept\" or \"decline\" ... try again ?" 
	else
		echo "Invalid response, expected \"accept\" or \"decline\" ... try again ?" |  /usr/bin/tr -cd [:print:]
	fi
      	answer=
      fi
      ;;
    esac
  done
}

PrintRequiredFree()
{
  total=0

  for i in "$@" ; do
    if [ "$i" -a -f "$i" ] ; then

        if [ `uname -s` = "Linux" ] ; then
          size=`ls -lLn "$i" | ( read perm links owner group size date ; echo $size )`
        else
          size=`ls -lLon "$i" | ( read perm links owner size date ; echo $size )`
        fi

      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."
}

ExtractFiles ()
{

   prev_directory="`pwd 2> /dev/null`"
   cd "$1" 
   if [ `uname -s` = "Linux" ] ; then
 	tar --no-same-owner -xpf "$2"  
   else
	tar xpfo "$2"
   fi

   cd "$prev_directory"

}


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
}

GetInstallDirectory ()
{
  var=$1
  
  defdir="/usr/local/Adobe/Acrobat7.0"
  case "$OSname" in
    SunOS)
      case "$OSrelease" in
        4.1.3*|4.1.4*|4.1C) defdir="/usr/Adobe/Acrobat7.0" ;;
        5.*) defdir="/opt/Adobe/Acrobat7.0" ;;
      esac
      ;;
    HP-UX)
      case "$OSrelease" in
        *.09.*) defdir="/usr/Adobe/Acrobat7.0" ;;
        *.10.*) defdir="/opt/Adobe/Acrobat7.0" ;;
        *) defdir="/Adobe/Acrobat7.0" ;;
      esac
      ;;
    AIX)
      defdir="/usr/lpp/Adobe/Acrobat7.0"
      ;;
  esac

  while [ -z "$InstallDir" ] ; do

    InstallDir="$defdir"

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

    var=`echo $InstallDir |  cut -d "\"" -f 2`

	case "$var" in
    ~*)
      if [ -f /bin/csh -a -x /bin/csh ] ; then
        var=`/bin/csh -c "echo $var"`
      fi
      ;;
    ../*|./*)
      var=`pwd`/"${var}"
      ;;
    *)
      ;;
	esac
    eval var=\"${var}\"
    InstallDir=$var

	if [ -f "$InstallDir" ] ; then
      
        echo " "
        echo "\"$InstallDir\" exists but is not a directory. Please specify a different location. "
        InstallDir=""
        continue;
      
    fi

    if [ ! -d "$InstallDir" ] ; then
      echo " "
      echo "Directory \"$InstallDir\" does not exist."
      $echonl "Do you want to create it now? [y]  "
      read answer

      if [ "$answer" ] ; then
          case "$answer" in
             y | Y )
               mkdir -p "$InstallDir"
             ;;
             n | N )
               echo "Exiting installation..."
               exit 0
             ;;
          esac
      else
         mkdir -p "$InstallDir"
      fi

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


    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

 

  ReadInstallDir="$InstallDir"
  
  
}

MakeLibLDAPLinks()
{

InstallDir="$1/Reader"

if [ ! -e /usr/lib/libldap.so ]
then
	rm -f "$InstallDir"/intellinux/lib/libldap.so 2>/dev/null

	LIB_LDAP=`ls -tr /usr/lib/libldap.so*  2>/dev/null | tail -n 1`

	if [ "$LIB_LDAP" = "" ] 
	then
		LIB_LDAP=`ls -tr /usr/lib/libldap* 2>/dev/null | grep -v libldap_r | grep -v libldapcpp | grep '\.so' | tail -n 1`
	fi

	if [ "$LIB_LDAP" != "" ] 
	then
		ln -s $LIB_LDAP "$InstallDir"/intellinux/lib/libldap.so
	fi
fi

if [ ! -e /usr/lib/liblber.so ]
then
	rm -f "$InstallDir"/intellinux/lib/liblber.so 2>/dev/null

	LIB_LBER=`ls -tr /usr/lib/liblber.so* 2>/dev/null | tail -n 1`

	if [ "$LIB_LBER" = "" ] 
	then
		LIB_LBER=`ls -tr /usr/lib/liblber* 2>/dev/null | grep -v liblber_r | grep '\.so' | tail -n 1`
	fi

	if [ "$LIB_LBER" != "" ] 
	then
		ln -s $LIB_LBER "$InstallDir"/intellinux/lib/liblber.so
	fi
fi
}


InstallReader()
{
  ReadConfig="$1"
  ReadPlatformTar="$2"
  SearchPlatformTar="$3"
  CustomPlatformTar="$4"
  
  
  OutputLicense "$ReadLicense"
  ReadLicense=""

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


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

  echo $ReadInstallDir

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


  if [ `uname -s` = "Linux" ] ; then
    MakeLibLDAPLinks "$ReadInstallDir"
  fi 

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

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

#
# Initialization:
#

umask 022

ScriptName=`basename "$0"`
CurrentDirectory=`pwd`
eval CurrentDirectory=\"${CurrentDirectory}\"

ScriptDirectory=`dirname "$0"`
eval ScriptDirectory=\"${ScriptDirectory}\"

AcroSerNum=""
AcroCompName=""
ReadInstallDir=""
case "${ScriptDirectory}" in
  /*) ;;
  .) ScriptDirectory="$CurrentDirectory" ;;
  *) ScriptDirectory="$CurrentDirectory"/"$ScriptDirectory" ;;
esac

 TypeCheck uname
  if [ $? -eq 0 ] ; then
    OSname=`uname -s`
    if [ "$OSname" = "AIX" ] ; then
      OSrelease=`uname -a | ( read name host minor major foo ; echo $major.$minor )`
    else
      OSrelease=`uname -r`
    fi
  else
    OSname=unknown
    OSrelease=unknown
  fi



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

Version=7.0.1

#
# Get the filenames:
#

for i in "${ScriptDirectory}"/* "${ScriptDirectory}"/../COMMON/* "${ScriptDirectory}"/../common/*; do
  if [ -f "$i" ] ; then
    case $i in
      */licread.txt*|*/LICREAD.TXT*)
        ReadLicense="$i"
        ;;
      */common.tar*|*/COMMON.TAR*)
        ReadTar="$i"
        ;;
      */search.tar*|*/SEARCH.TAR*)
        SearchTar="$i"
        ;;
      */custom.tar*|*/CUSTOM.TAR*)
        CustomTar="$i"
        ;;
      */solrdr.tar*|*/SSOLR.TAR*)
        ReadSparcSolarisTar="$i"
        ;;
      */hpuxrdr.tar*|*/HPUXRDR.TAR*)
        ReadHppaHpuxTar="$i"
        ;;
      */aixrdr.tar*|*/AIXRDR.TAR*)
        ReadRS6000AIXTar="$i"
        ;;
      */ilinxr.tar*|*/ILINXR.TAR*)
        ReadIntelLinuxTar="$i"
        ;;
      */ilinxc.tar*|*/ILINXC.TAR*)
        CustomIntelLinuxTar="$i"
        ;;
    esac
  fi
done


#
# Validate the configurations:
#

NumConfigs=0

if [ "$ReadTar" -a "$ReadLicense" ] ; then
  if [ "$ReadSparcSolarisTar" ] ; then
    NumConfigs=`expr $NumConfigs + 1`
    ReadSparcSolarisNum=$NumConfigs
  fi
  if [ "$ReadHppaHpuxTar" ] ; then
    NumConfigs=`expr $NumConfigs + 1`
    ReadHppaHpuxNum=$NumConfigs
  fi
  if [ "$ReadRS6000AIXTar" ] ; then
    NumConfigs=`expr $NumConfigs + 1`
    ReadRS6000AIXNum=$NumConfigs
  fi
  if [ "$ReadIntelLinuxTar" ] ; then
    NumConfigs=`expr $NumConfigs + 1`
    ReadIntelLinuxNum=$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*|4.1C)
        ReadDefaultNum="$ReadSparcSunNum"
        DefaultName="SPARC/SunOS 4.1.3 and later"
        ;;
      5.1|5.1.*|5.2|5.2.*)
        ;;
      5.*)
        MachArch=`uname -p`
        case "$MachArch" in
          sparc)
            ReadDefaultNum="$ReadSparcSolarisNum"
            DefaultName="SPARC/Solaris 2.x"
            ;;
          esac
        ;;
    esac
    ;;
  HP-UX)
    case "$OSrelease" in
      *.09.*|*.10.*|*.11.*)
        ReadDefaultNum="$ReadHppaHpuxNum"
        DefaultName="HP-UX"
        ;;
    esac
    ;;
  AIX)
    case "$OSrelease" in
      4.*|5.*)
        ReadDefaultNum="$ReadRS6000AIXNum"
        DefaultName="AIX"
      ;;
    esac
    ;;
  Linux)
    ReadDefaultNum="$ReadIntelLinuxNum"
    DefaultName="Intel/Linux"
    ;;
esac

if  [ -z "$ReadDefaultNum" ] ; then
  echo ""
  echo "WARNING: The chosen configuration will not run on your current platform ($OSname/$OSrelease)."
  if yesno "Continue installation? [n] " 1 ; then
    :
  else
    exit 1
  fi
fi


#
# If multiple confurations, get the default configuration
#

if [ -z "$SingleConfig" ] ; then
  if [ "$ReadDefaultNum" ] ; then
    if yesno "Install default configuration, Acrobat Reader ${Version} 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 [ "$AcroSparcSolarisNum" ] ; then
      echo "  ${AcroSparcSolarisNum}. Acrobat ${Version} for SPARC/Solaris 2.x"
    fi
    if [ "$AcroHppaHpuxNum" ] ; then
      echo "  ${AcroHppaHpuxNum}. Acrobat ${Version} for HP-UX"
    fi
    if [ "$AcroRS6000AIXNum" ] ; then
      echo "  ${AcroRS6000AIXNum}. Acrobat ${Version} for AIX"
    fi
    if [ "$AcroIntelLinuxNum" ] ; then
      echo "  ${AcroIntelLinuxNum}. Acrobat ${Version} for Intel/Linux"
    fi
    if [ "$ReadSparcSolarisNum" ] ; then
        echo "  ${ReadSparcSolarisNum}. Acrobat Reader ${Version} for SPARC/Solaris 2.x"
    fi
    if [ "$ReadHppaHpuxNum" ] ; then
        echo "  ${ReadHppaHpuxNum}. Acrobat Reader ${Version} for HP-UX"
    fi
    if [ "$ReadRS6000AIXNum" ] ; then
      echo "  ${ReadRS6000AIXNum}. Acrobat Reader ${Version} for AIX"
    fi
    if [ "$ReadIntelLinuxNum" ] ; then
      echo "  ${ReadIntelLinuxNum}. Acrobat Reader ${Version} for Intel/Linux"
    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" = "$ReadSparcSolarisNum" ] ; then

    InstallReader sparcsolaris "$ReadSparcSolarisTar" \
      "$SearchSparcSolarisTar" "$CustomSparcSolarisTar"
    ReadSparcSolarisNum=""
  elif [ "$ConfigNum" = "$ReadHppaHpuxNum" ] ; then

    InstallReader hppahpux "$ReadHppaHpuxTar" \
      "$SearchHppaHpuxTar" "$CustomHppaHpuxTar"
    ReadHppaHpuxNum=""
  elif [ "$ConfigNum" = "$ReadRS6000AIXNum" ] ; then

    InstallReader rs6000aix "$ReadRS6000AIXTar" \
      "$SearchRS6000AIXTar" "$CustomRS6000AIXTar"
    ReadRS6000AIXNum=""

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

    InstallReader intellinux "$ReadIntelLinuxTar" \
      "$SearchIntelLinuxTar" "$CustomIntelLinuxTar"
    ReadIntelLinuxNum=""

  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


