#!/bin/sh
#
# Install Script for Acrobat Language Kit
#
# Copyright (c)1998-2002 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
        */licfont.txt*|*/LICFONT.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" ;;
        */instlang*|*/INSTLANG*) ;;
      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
}

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


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

GetOS()
{
  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
}

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

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 Acrobat Reader 5.0 or a higher version installed"
  echo "prior to this installation. If you do not have Acrobat Reader 5.0"
  echo "or a higher version already installed on your system,"
  echo "please do so before installing this Asian Language Kit."
  echo " "
}


OutputLicense ()
{
  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

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

  echo " "
  $command "$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 this license agreement; Type \"decline\" to exit.  "
    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
}

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

DefaultInstallDir()
{
  
  case "$OSname" in
    Linux)
      defdir="/usr/local/Acrobat5"
      ;;
    AIX)
      defdir="/usr/lpp/Acrobat5"
      ;;
    *)
      defdir="/opt/Acrobat5"
      ;;
  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 [ 1 -lt "`expr "$Version" : '5.*'`" ]
  then 
    echo " "
    echo "Installing..."
  else
    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
    if [ -z "$ans" ]
    then
      ans="$default"
    fi

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

  eval $var=\"$val\"
}

GetInstallInfo()
{
  InstallDir="`DefaultInstallDir`"

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

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

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

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()
{
  FontDir="$InstallDir/Resource/Font"

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

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

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

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

  InstallFiles "Installing Korean language resources ... " \
    "$FontDir" "$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

