#!/bin/sh
#
# Install Script for Acrobat Language kit
#
# Copyright (c)1998 Adobe Systems Incorporated
# All Rights Reserved
#
##########################################################################

success=0
failure=1

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

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

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

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

InitEchonl()
{
  if [ -x "/bin/echo" ] ; then
    EchoCommand="/bin/echo"
  fi
  if [ -x "/usr/bin/echo" ] ; then
    EchoCommand="/usr/bin/echo"
  fi

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

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

yesno()
{
  msg="$1"
  def="$2"
  while true ; do
    echo " "
    $echonl "$msg [$def] "
    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
      if [ "$def" = "y" ]
      then
        return 0
      else
        return 1
      fi
    fi
  done
}

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

FindDistFiles()
{
  # Get the filenames:
  for i in ${ScriptDirectory}/* ; do
    if [ -f "$i" ] ; then
      case $i in
        */licread.txt*|*/LICREAD.TXT*)   LicenseFile="$i" ;;
        */langcom.tar*|*/LANGCOM.TAR*)   LangComTar="$i" ;;
        */langchs.tar*|*/LANGCHS.TAR*)   LangCHSTar="$i" ;;
        */langcht.tar*|*/LANGCHT.TAR*)   LangCHTTar="$i" ;;
        */langjpn.tar*|*/LANGJPN.TAR*)   LangJPNTar="$i" ;;
        */langkor.tar*|*/LANGKOR.TAR*)   LangKORTar="$i" ;;
        */install*|*/INSTALL*) ;;
      esac
    fi
  done

  # Validate the configurations, need at least one lang tar
  if [ -z "$LangCHSTar" \
    -a -z "$LangCHTTar" \
    -a -z "$LangJPNTar" \
    -a -z "$LangKORTar" ]
  then
    return $failure
  fi

  if [ -z "$LangComTar" ]
  then
    return $failure
  fi

  return $success
}

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

GetOS()
{
  if [ "`type uname`" != "uname not found" ] ; 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
}

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

OutputWelcome()
{
  echo " "
  echo "Welcome to the Asian Language Kit installation."
  echo " "
  echo "This installation will not work if you do not have the"
  echo "Unix Adobe Reader version 7.0 installed prior to this installation."
  echo "If you do not have Adobe Reader version 7.0 already installed on your"
  echo "system please do so before installing this Asian Language Kit."
  echo " "
}

OutputLicense ()
{
  if [ -z "$PAGER" ] || [ "`type $PAGER`" = "$PAGER not found" ]
  then
    if [ "`type more`" != "more not found" ] ; then
      Pager=more
    elif [ "`type pg`" != "pg not found" ] ; then
      Pager=pg
    else
      Pager=cat
    fi
  else
    Pager="$PAGER"
  fi

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

  echo " "
  $Pager "$LicenseFile"

  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 "Please type \"accept\" to accept the terms and conditions of license agreement; Type \"decline\" to exit. "
    read answer
    ucanswer=`echo "${answer}" | tr '[a-z]' '[A-Z]'`
    case "$ucanswer" in
    ACCEPT)
      ;;
    DECLINE)
      echo " "
      echo "License agreement not accepted ... aborting installation"
      echo " "
      exit 1
      ;;
    *)
      echo " "
      echo "ERROR: Invalid response, expected \"accept\" or \"decline\" ... try again ?"
      answer=
      ;;
    esac
  done
}

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

DefaultInstallDir()
{
  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="/opt/Adobe/Acrobat7.0" ;;
      esac
      ;;
    IRIX|IRIX64)
      defdir="/opt/Adobe/Acrobat7.0"
      ;;
    AIX)
      defdir="/usr/lpp/Adobe/Acrobat7.0"
      ;;
  esac

  echo "$defdir"
}

TestInstallDir()
{
  dir="$1"

  if [ "`expr "X$dir" : 'X/'`" != 2 ]
  then
    echo " "
    echo "ERROR: directory must be an absolute path"
    return $failure
  fi
 
  if [ ! -d "$dir" ]
  then
    echo " "
    echo "ERROR: Directory  \"$dir\" does not exist."
    return $failure
  fi

  if [ ! -f "$dir/Reader/AcroVersion" ]
  then
    echo " "
    echo "ERROR: AcroVersion file does not exist."
    return $failure
  fi

  read Version < "$dir/Reader/AcroVersion"
  if [ "$Version" != "7.0.1" ]
  then
    echo " "
    echo "ERROR: Incorrect Acrobat version: $Version."
    return $failure
  fi

  if ( echo foo > "$dir"/AcroWriteTest ) 2> /dev/null
  then
    rm -f "$dir"/AcroWriteTest
  else
    echo " "
    echo "ERROR: Cannot write to directory \"$dir\"."
    return $failure
  fi

  return $success
}

GetInfo()
{
  var="$1"
  test="$2"
  default="$3"
  tagline="$4"
  val=""
 
  while [ -z "$val" ]
  do
    echo " "
    $echonl "$tagline [$default] "

    read ans
    var=`echo $ans |  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}\"
    ans=$var

    if [ -z "$ans" ]
    then
      ans="$default"
    fi

    if $test "$ans"
    then
      val="$ans"
    fi
  done

  eval InstallDir=\"$val\"
}

GetInstallInfo()
{
  InstallDir="`DefaultInstallDir`"

  GetInfo InstallDir TestInstallDir "$InstallDir" "Enter the location where you installed the Adobe Reader"
}

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

ExtractFiles ()
{
  id_out="`id`"
  uid=`id | awk '{ printf("%s",$1) }'| cut -d "(" -f 2 |  cut -d ")" -f 1`
  gid=`id | awk '{ printf("%s",$2) }'| cut -d "(" -f 2 |  cut -d ")" -f 1`
  if [ "$uid" = "root" ] ; then
    ( cd "$1" ; tar xpfo "$2" ; chown -R $uid . ; chgrp -R $gid . )
  else
    ( cd "$1" ; tar xpfo "$2" )
  fi
}

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

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

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

InstallLang()
{
  ResourceDir="$InstallDir/Resource"

  InstallFiles "Installing Common resources ... " \
    "$ResourceDir" "$LangComTar"

  InstallFiles "Installing Simplified Chinese language resources ... " \
    "$ResourceDir" "$LangCHSTar"

  InstallFiles "Installing Traditional Chinese language resources ... " \
    "$ResourceDir" "$LangCHTTar"

  InstallFiles "Installing Japanese language resources ... " \
    "$ResourceDir" "$LangJPNTar"

  InstallFiles "Installing Korean language resources ... " \
    "$ResourceDir" "$LangKORTar"
}

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

#
# Initialization:
#

umask 022

InitEchonl

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

GetOS

FindDistFiles
if [ $? != $success ]
then
  echo " "
  echo "ERROR: Could not find distribution ... aborting"
  echo " "
  exit 1
fi

OutputWelcome
if yesno "Continue installation?" "y"
then
  :
else
  echo ""
  exit 1
fi

OutputLicense

GetInstallInfo

InstallLang

echo " "
echo "Installation completed."
echo " "

exit 0

