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


DestroySelf ()
{
    AcrobatTmpDir="`dirname $ScriptDirectory 2>/dev/null`";
    rm -rf "${AcrobatTmpDir}" > /dev/null 2>&1;
    exit;
}


readlink()
{
    OLDPWD="`pwd`"
    lfile="$1"

    while $TestCmd -h "$lfile" -a $TestCmdOption "$lfile" ; do
        CWD="`dirname "$lfile"`"
        lfile="`basename "$lfile"`"
        cd $CWD
        lfile="`ls -l "$lfile"|sed 's/^.*-> *\(.*\) *$/\1/'`"
    done

    if $TestCmd $TestCmdOption "$lfile" ; then
        CWD="`dirname "$lfile"`"
        lfile="`basename "$lfile"`"
        cd $CWD
        printf "%s\n" "`pwd`/$lfile"
        cd "$OLDPWD"
        return 0
    fi

    cd "$OLDPWD"
    return 1
}


MkTempInternal()
{
    if [ "${mktemp_count+set}" != "set" ]; then
        mktemp_count="0"
    fi

    mktemp_file="/tmp/acrobat.$$.${mktemp_count}"

    while "${TestCmd}" "${TestCmdOption}" "$mktemp_file"
    do
        mktemp_count="`expr $mktemp_count + 1`"
        mktemp_file="/tmp/acrobat.$$.${mktemp_count}"
    done

    touch "$mktemp_file" && chmod 600 "$mktemp_file" && echo "$mktemp_file"
}

MkTemp()
{
    template="tmp.XXXXXXXXXX";
    MKTEMP="`Which mktemp 2>/dev/null`";
    if [ "$MKTEMP" != "" ]; then
        :
    else
        MKTEMP="MkTempInternal";
    fi

    $MKTEMP /tmp/acrobat.$template
}


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

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


PrintRequiredFree()
{
  total=0

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

        if [ "${OSname}" = "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`

  printf "This installation requires %s MB of free disk space.\n" "${megs}"
}


ExtractFiles ()
{
   prev_directory="`pwd 2> /dev/null`"
   cd "$1" 

   case "${OSname}" in
     Linux)
        tar -xvpf "$2" ;;
     HP-UX)
        tar xfo "$2" && tar tf "$2" ;;
     *)
        tar xpfo "$2" && tar tf "$2" | sed -e 's# symbolic link to [a-zA-Z0-9._+]* *$##g' ;;
   esac | sed -e "s#^#${1}/#" >> "$installLogFile"

   cd "$prev_directory"
}


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


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

  if [ -z "$msg" ] ; then
    printf "%s\n" "Done"
  fi
}


ExpandDirPath ()
{
    OLDPWD="`pwd`";
	DestDir="$@"
    if [ "${LzmaBaseDir}" != "" -a -d "${LzmaBaseDir}" ]; then
        cd "${LzmaBaseDir}"
        if [ "${DestDir}" = "${OLDPWD}" ]; then
            DestDir="${LzmaBaseDir}";
        fi
    fi

    var=`eval echo ${DestDir} |  cut -d "\"" -f 2`
    case "$var" in
    /*) ;;
    *)  var="`pwd`/${var}" ;;
	esac

    cd "${OLDPWD}"

    printf "%s" "$var"
}


CheckInstallDir ()
{
    DirectoryToCheck="${1}";

    if [ -f "$DirectoryToCheck" ] ; then
        printf "%s exists but is not a directory. Please specify a different location.\n" "\"$DirectoryToCheck\"";
		return 1;
    fi

    if [ ! -d "$DirectoryToCheck" ] ; then
        printf "\nDirectory  %s  does not exist.\n\n" "\"$DirectoryToCheck\""
        while :
        do
            printf "%s" "Do you want to create it now? [y] "
            read answer

            case "$answer" in
                "y" | "Y" | "" )
                    mkdir -p "$DirectoryToCheck" 2> /dev/null
                    break ;;
                "n" | "N" )
                    printf "%s\n" "Exiting installation..."
                    exit 0 ;;
                 *)
                    printf "%s\n" "ERROR: Invalid response ... try again"
            esac
        done

        if [ ! -d "$DirectoryToCheck" ] ; then
            printf "ERROR: Cannot make directory %s.\n" "\"$DirectoryToCheck\""
            return 1;
        fi
    else
        # The install directory already exists.
        # Check if it is an existing Reader installation.
        # If yes, ask the user if she wants to overwrite.
        unset PrintOnce;
        for i in "${DirectoryToCheck}"/Adobe/Reader*/Reader/*/bin/acroread ; do
			if "${TestCmd}" "${TestCmdOption}" "${i}" ; then
				if [ "${PrintOnce+set}" != "set" ]; then
					printf "\nPrevious installation of Adobe Reader found in %s:\n" "\"${DirectoryToCheck}\"";
					PrintOnce=1;
				fi
				printf "\t%s\n" "\"${i}\"";
			fi
		done

		if [ "${PrintOnce+set}" = "set" ]; then
			unset OverwriteInstallDir;
			while [ "${OverwriteInstallDir+set}" != "set" ]; do
				printf "\nContinue installation? [y] ";
				read answer;
				case "$answer" in
					"y" | "Y" | "" )
						OverwriteInstallDir=1;;
					"n" | "N" )
						OverwriteInstallDir=0;
						printf "%s\n" "Exiting installation...";
						exit 0;;
					* )
					printf "%s\n" "ERROR: Invalid response ... try again"
				esac
			done
		fi
    fi

    return 0;
}


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

  while [ -z "$InstallDir" ] ; do

    InstallDir="$defdir"

    printf "\nEnter installation directory for Adobe Reader %s [%s]  " "${Version}" "${InstallDir}"
    read answer
    case "$answer" in
      "")
        ;;
      *)
        eval InstallDir=\"$answer\"
        ;;
    esac

    InstallDir="`ExpandDirPath "$InstallDir"`"

    CheckInstallDir "${InstallDir}"
    if [ $? -ne 0 ]; then
        InstallDir="";
        continue;
    fi
 
    export "InstallDir"

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

 

  ReadInstallDir="$InstallDir"
}


Which()
{
    OLD_IFS="$IFS"
    IFS=":"
    status=1

    for i in $PATH; do
        if [ -x "$i/$1" ]; then
            printf "%s\n" "$i/$1"
            status=0
            break
        fi
    done

    IFS="$OLD_IFS"
    return $status
}


#Make Soft Links To gnome-speech libraries
MakeLibGnomeSpeechLinks()
{
    install_dir="$1/Adobe/Reader9/Reader"
    configured_dir="$2"
    lib_dir="$install_dir/$configured_dir/lib"

    if TESTSPEECHEXEC="`Which test-speech`" && Which ldd >/dev/null && Which awk >/dev/null; then
        rm -f "$lib_dir/libORBit-2.so" 2>/dev/null
        rm -f "$lib_dir/libbonobo-2.so" 2>/dev/null
        rm -f "$lib_dir/libbonobo-activation.so" 2>/dev/null
        rm -f "$lib_dir/libgnomespeech.so" 2>/dev/null

        lib_orbit2="`ldd $TESTSPEECHEXEC 2>/dev/null | grep -i libORBit-2.* | cut -d '>' -f2 | cut -d '(' -f 1 |awk '{print $1}' `"
        lib_bonobo="`ldd $TESTSPEECHEXEC 2>/dev/null | grep -i bonobo-2.* | cut -d '>' -f2 | cut -d '(' -f 1 |awk '{print $1}' `"
        lib_bonoboactivation="`ldd $TESTSPEECHEXEC 2>/dev/null | grep -i bonobo-activation.* | cut -d '>' -f2 | cut -d '(' -f 1 |awk '{print $1}' `"
        lib_gnomespeech="`ldd $TESTSPEECHEXEC 2>/dev/null | grep -i libgnomespeech.* | cut -d '>' -f2 | cut -d '(' -f 1 |awk '{print $1}' `"

        if [ -f "$lib_orbit2" -a -f "$lib_bonobo" -a -f "$lib_bonoboactivation" -a -f "$lib_gnomespeech" ]; then
            ln -s "$lib_orbit2" "$lib_dir/libORBit-2.so" && record_config remove "$lib_dir/libORBit-2.so"
            ln -s "$lib_bonobo" "$lib_dir/libbonobo-2.so" && record_config remove "$lib_dir/libbonobo-2.so"
            ln -s "$lib_bonoboactivation" "$lib_dir/libbonobo-activation.so" && record_config remove "$lib_dir/libbonobo-activation.so"
            ln -s "$lib_gnomespeech" "$lib_dir/libgnomespeech.so" && record_config remove "$lib_dir/libgnomespeech.so"
        fi
    fi
}


MakeLibLDAPLinks()
{
install_dir="$1/Adobe/Reader9/Reader"
read_config="$2"

if ${TestCmd} ! ${TestCmdOption} /usr/lib/libldap.so
then
	rm -f "$install_dir/${read_config}/lib/libldap.so" 2>/dev/null

	LIB_LDAP="`ls -tr /usr/lib/libldap.so*  2>/dev/null | tail -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 -1`"
	fi

	if [ "$LIB_LDAP" != "" ] 
	then
		ln -s "$LIB_LDAP" "$install_dir/${read_config}/lib/libldap.so" && record_config remove "$install_dir/${read_config}/lib/libldap.so"
	fi
fi

if ${TestCmd} ! ${TestCmdOption} /usr/lib/liblber.so
then
	rm -f "$install_dir/${read_config}/lib/liblber.so" 2>/dev/null

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

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

	if [ "$LIB_LBER" != "" ] 
	then
		ln -s "$LIB_LBER" "$install_dir/${read_config}/lib/liblber.so" && record_config remove "$install_dir/${read_config}/lib/liblber.so"
	fi
fi
}


InstallBrowserPlugin()
{
    install_dir="$1"
    opts="-silent -global -user"

    if [ -f "$install_dir/Adobe/Reader9/Browser/install_browser_plugin" ]; then
        "$install_dir/Adobe/Reader9/Browser/install_browser_plugin" $opts "$install_dir"
    else
        printf "\n%s\n" "Could not find browser plugin install script."
        exit 1
    fi
}


MakeMozillaConfig()
{
    install_dir="$1"

    for i in /etc/gre.d/gre.conf /etc/gre.d/firefox.conf /etc/gre.d/mozilla.conf /etc/gre.d/*.conf /etc/gre.conf
    do
        if [ -f "$i" ] && grep "^GRE_PATH=" "$i" >/dev/null 2>&1; then
            return;
        fi
    done

    while :; do
        printf "Enter the directory where mozilla or firefox or xulrunner is installed: "
        read mozilla_dir

        if [ -n "$mozilla_dir" ]; then
            if [ -d "$mozilla_dir" ]; then
                if [ -f "$mozilla_dir/libgtkembedmoz.so" ]; then
                    break
                elif [ -f "$mozilla_dir/lib/libgtkembedmoz.so" ]; then
                    mozilla_dir="$mozilla_dir/lib"
                    break
                fi
            fi
        else
            while :
            do
                printf "You didn't enter the installation directory for mozilla/firefox/xulrunner, do you want to skip this [y/n]: "
                read REPLY
                case "$REPLY" in
                    y|Y)
                        printf "\n%s\n" "Some of the features like Adobe Help, DownTown and HowTo require mozilla/firefox/xulrunner libraries and may not work correctly. This can be set later by setting MOZILLA_COMP_PATH to the path of mozilla or firefox or xulrunner libraries before launching the Adobe Reader."
                        break 2;;
                    n|n) break ;;
                esac
            done
        fi
    done

    if [ -n "$mozilla_dir" ]; then
        config_file="$install_dir/Adobe/Reader9/Reader/GlobalPrefs/mozilla_config"
        mozilla_dir="`echo $mozilla_dir | sed -e 's/\"/\\\\\"/g'`"
        echo "MOZILLA_COMP_PATH=\"$mozilla_dir\"" > "$config_file"
        echo "export MOZILLA_COMP_PATH" >> "$config_file"
        record_config remove "$config_file"
    fi
}


record_config()
{
    config_file="$ReadInstallDir/Adobe/Reader9/Reader/GlobalPrefs/.config"
    echo "$@" >> "$config_file"
}


install_icon()
{
    read_config="${1}";
    shift;

    PATH="$ReadInstallDir/Adobe/Reader9/Reader/${read_config}/bin:$PATH:/usr/ucb" xdg-icon-resource install "$@" 2>/dev/null

    if [ $? -eq 0 ]; then
        record_config uninstall_icon "$@"
    fi
}


install_desktop_menu()
{
    read_config="${1}";
    shift;

    eval desktop_file="\$$#"
    tempfile="`MkTemp`"

    PATH="$ReadInstallDir/Adobe/Reader9/Reader/${read_config}/bin:$PATH:/usr/ucb" xdg-desktop-menu uninstall --mode system "$@" >/dev/null 2>&1
    PATH="$ReadInstallDir/Adobe/Reader9/Reader/${read_config}/bin:$PATH:/usr/ucb" xdg-desktop-menu uninstall --mode user "$@" >/dev/null 2>&1
    sed -e "s#^Exec=acroread#Exec=$ReadInstallDir/Adobe/Reader9/bin/acroread#g" < "$desktop_file" > "$tempfile"
    mv -f "$tempfile" "$desktop_file"
    chmod 644  "$desktop_file"
    PATH="$ReadInstallDir/Adobe/Reader9/Reader/${read_config}/bin:$PATH:/usr/ucb" xdg-desktop-menu install "$@" 2>/dev/null

    if [ $? -eq 0 ]; then
        record_config uninstall_desktop_menu "$@"
    fi
}


install_desktop_icon()
{
    read_config="${1}";
    shift;

    eval desktop_file="\$$#"
    tempfile="`MkTemp`"

    PATH="$ReadInstallDir/Adobe/Reader9/Reader/${read_config}/bin:$PATH:/usr/ucb" xdg-desktop-icon uninstall "$@" 2>/dev/null
    sed -e "s#^Exec=acroread#Exec=$ReadInstallDir/Adobe/Reader9/bin/acroread#g" < "$desktop_file" > "$tempfile"
    mv -f "$tempfile" "$desktop_file"
    PATH="$ReadInstallDir/Adobe/Reader9/Reader/${read_config}/bin:$PATH:/usr/ucb" xdg-desktop-icon install "$@" 2>/dev/null

    if [ $? -eq 0 ]; then
		desktop_dir=""
		if [ "${OSname}" = "Linux" ]; then
			PATH="$ReadInstallDir/Adobe/Reader9/Reader/${read_config}/bin:$PATH:/usr/ucb" xdg-user-dirs-update >/dev/null 2>&1
			desktop_dir="`PATH="$ReadInstallDir/Adobe/Reader9/Reader/${read_config}/bin:$PATH:/usr/ucb" xdg-user-dir DESKTOP 2>/dev/null`"
		fi
		if [ ! -n "${desktop_dir}" -o ! -d "${desktop_dir}" ]; then
			desktop_dir="${HOME}/Desktop"
		fi

        chmod 777 "${desktop_dir}/`basename "$desktop_file"`" 2>/dev/null
        record_config uninstall_desktop_icon "$@"
    fi
}


install_mime()
{
    read_config="${1}";
	shift;
    PATH="$ReadInstallDir/Adobe/Reader9/Reader/${read_config}/bin:$PATH:/usr/ucb" xdg-mime install "$@" 2>/dev/null
        record_config unset_mime "AdobeReader.desktop"
}


InstallIcons()
{
    install_dir="$1/Adobe/Reader9/Resource/Icons";
	read_config="${2}";

    for icon_size in 16 20 22 24 32 36 48 64 96 128 192; do
        install_icon "${read_config}" --noupdate --novendor --context apps --size $icon_size "$install_dir/${icon_size}x${icon_size}/AdobeReader9.png"
        install_icon "${read_config}" --noupdate --novendor --context apps --size $icon_size "$install_dir/${icon_size}x${icon_size}/adobe.pdf.png"
        install_icon "${read_config}" --noupdate --novendor --context mimetypes --size $icon_size "$install_dir/${icon_size}x${icon_size}/adobe.pdf.png" 'application-pdf'
        install_icon "${read_config}" --noupdate --novendor --context apps --size $icon_size "$install_dir/${icon_size}x${icon_size}/vnd.fdf.png"
        install_icon "${read_config}" --noupdate --novendor --context mimetypes --size $icon_size "$install_dir/${icon_size}x${icon_size}/vnd.fdf.png" 'application-vnd.fdf'
        install_icon "${read_config}" --noupdate --novendor --context apps --size $icon_size "$install_dir/${icon_size}x${icon_size}/vnd.adobe.pdx.png"
        install_icon "${read_config}" --noupdate --novendor --context mimetypes --size $icon_size "$install_dir/${icon_size}x${icon_size}/vnd.adobe.pdx.png" 'application-vnd.adobe.pdx'
        install_icon "${read_config}" --noupdate --novendor --context apps --size $icon_size "$install_dir/${icon_size}x${icon_size}/vnd.adobe.xdp+xml.png"
        install_icon "${read_config}" --noupdate --novendor --context mimetypes --size $icon_size "$install_dir/${icon_size}x${icon_size}/vnd.adobe.xdp+xml.png" 'application-vnd.adobe.xdp+xml'
        install_icon "${read_config}" --noupdate --novendor --context apps --size $icon_size "$install_dir/${icon_size}x${icon_size}/vnd.adobe.xfdf.png"
        install_icon "${read_config}" --noupdate --novendor --context mimetypes --size $icon_size "$install_dir/${icon_size}x${icon_size}/vnd.adobe.xfdf.png" 'application-vnd.adobe.xfdf'
    done
}


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

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

  if [ -z "$ReadInstallDir" ] ; then
    GetInstallDirectory
    SilentMode="No"
  else
    ReadInstallDir="`ExpandDirPath "$ReadInstallDir"`"
    SilentMode="Yes"

    if [ -f "$ReadInstallDir" ]; then
        printf "%s exists but is not a directory. Please specify a different location.\n" "\"$ReadInstallDir\""
        exit 1
    fi

    mkdir -p "$ReadInstallDir" 2>/dev/null

    if [ $? -ne 0 ]; then
        printf "ERROR: Cannot make directory %s.\n" "\"$ReadInstallDir\""
        exit 1
    fi

    touch "$ReadInstallDir"/AcroWriteTest 2>/dev/null 

    if [ $? -eq 0 ]; then
        rm -f "$ReadInstallDir"/AcroWriteTest 2>/dev/null
    else
        printf "ERROR: Cannot write to directory %s.\n" "\"$ReadInstallDir\""
        exit 1
    fi
  fi

  installLogFile="`MkTemp`"
  printf "%s\n\n" "$ReadInstallDir"

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

  rm -f /usr/bin/acroread 2>/dev/null && \
  ln -s "$ReadInstallDir/Adobe/Reader9/bin/acroread" /usr/bin/acroread 2>/dev/null && \
  record_config remove /usr/bin/acroread

  if [ "${OSname}" = "Linux" ]; then
      rm -f /etc/bash_completion.d/acroread.sh 2>/dev/null && \
      ln -s "$ReadInstallDir/Adobe/Reader9/Resource/Shell/acroread_tab" /etc/bash_completion.d/acroread.sh 2>/dev/null && \
      record_config remove /etc/bash_completion.d/acroread.sh
  else
      if [ -d "/opt/csw/etc/bash_completion.d" ] ; then
	  rm -f /opt/csw/etc/bash_completion.d/acroread.sh 2>/dev/null && \
	  ln -s "$ReadInstallDir/Adobe/Reader9/Resource/Shell/acroread_tab" /opt/csw/etc/bash_completion.d/acroread.sh 2>/dev/null && \
          record_config remove /opt/csw/etc/bash_completion.d/acroread.sh
      fi
      if [ -d "/usr/local/etc/bash_completion.d" ] ; then
	  rm -f /usr/local/etc/bash_completion.d/acroread.sh 2>/dev/null && \
	  ln -s "$ReadInstallDir/Adobe/Reader9/Resource/Shell/acroread_tab" /usr/local/etc/bash_completion.d/acroread.sh 2>/dev/null && \
          record_config remove /usr/local/etc/bash_completion.d/acroread.sh
      fi
  fi
  if [ "${OSname}" = "Linux" ]; then
    manPageFile="acroread.1.gz"
  else
    manPageFile="acroread.1"
    gunzip "$ReadInstallDir/Adobe/Reader9/Resource/Shell/acroread.1.gz" 2>/dev/null 
  fi

  rm -f "/usr/share/man/man1/${manPageFile}" 2>/dev/null && \
  ln -s "$ReadInstallDir/Adobe/Reader9/Resource/Shell/${manPageFile}" "/usr/share/man/man1/${manPageFile}" 2>/dev/null && \
  record_config remove "/usr/share/man/man1/${manPageFile}"

  if [ "${OSname}" = "Linux" -o \( "${OSname}" = "SunOS" -a "`uname -m`" = "i86pc" \) ]; then
      printf "%s" "Setting up libraries ... "
      MakeLibGnomeSpeechLinks "$ReadInstallDir" "$ReadConfig"
      MakeLibLDAPLinks "${ReadInstallDir}" "${ReadConfig}"
      printf "%s\n" "Done"
      
      printf "%s" "Setting up desktop and menu icons ... "
      if Which kde-config >/dev/null 2>&1; then
        KDE_BASE_DIR="`kde-config --prefix 2>/dev/null`"
      elif [ -x /opt/kde3/bin/kde-config ]; then
        KDE_BASE_DIR="/opt/kde3"
      elif [ -x /opt/kde/bin/kde-config ]; then
        KDE_BASE_DIR="/opt/kde"
      else
        KDE_BASE_DIR="/usr"
      fi

      if GNOME_BASE_DIR="`Which gnome-open`"; then
        GNOME_BASE_DIR="`dirname "$GNOME_BASE_DIR"`"
        GNOME_BASE_DIR="`dirname "$GNOME_BASE_DIR"`"
      elif [ -x /opt/gnome/bin/gnome-open ]; then
        GNOME_BASE_DIR="/opt/gnome"
      else
        GNOME_BASE_DIR="/usr"
      fi

      InstallIcons "$ReadInstallDir" "${ReadConfig}"
      install_mime "${ReadConfig}" --novendor "$ReadInstallDir/Adobe/Reader9/Resource/Support/AdobeReader.xml"

      for i in application/pdf application/vnd.adobe.xfdf application/vnd.fdf application/vnd.adobe.xdp+xml application/vnd.adobe.pdx application/fdf application/xdp application/xfdf application/pdx; do
        PATH="$ReadInstallDir/Adobe/Reader9/Reader/${read_config}/bin:$PATH:/usr/ucb" APP_PATH="${ReadInstallDir}/Adobe/Reader9/bin/acroread" xdg-mime default "AdobeReader.desktop" "$i" 2>/dev/null
      done

      install_desktop_menu "${ReadConfig}" --novendor "$ReadInstallDir/Adobe/Reader9/Resource/Support/AdobeReader.desktop"
      install_desktop_icon "${ReadConfig}" --novendor "$ReadInstallDir/Adobe/Reader9/Resource/Support/AdobeReader.desktop"
      PATH="$ReadInstallDir/Adobe/Reader9/Reader/${read_config}/bin:$PATH:/usr/ucb" xdg-icon-resource forceupdate >/dev/null 2>&1
      printf "%s\n" "Done"
  fi

  sort -r < "$installLogFile" | \
  while read REPLY
  do
     record_config remove "$REPLY"
  done

  rm -f "$installLogFile"

  printf "%s" "Setting up the browser plugin ... "
  InstallBrowserPlugin "$ReadInstallDir" >/dev/null 2>&1
  printf "%s\n" "Done"

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


ShowUsage()
{
	printf "\nSupported options:\n";
	printf "\t[--install_path=<DIR>]: To install Adobe Reader inside the <DIR> directory.\n";
	printf "\t[-h | --help]: To display this help message\n";
}


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

#
# Initialization:
#

umask 022
ReadInstallDir=""
LzmaBaseDir=""

if [ -x /usr/bin/test ]; then
	TestCmd=/usr/bin/test
else
	TestCmd=test
fi

if $TestCmd -e . 2>/dev/null; then
	TestCmdOption="-e"
else
	TestCmdOption="-f"
fi

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

while [ "${1+set}" = "set" ]; do
    case "$1" in 
	--install_path=*)
		ReadInstallDir="`echo $1 | cut -d= -f2-`";;

    --lzma=*)
        LzmaBaseDir="`echo $1 | cut -d= -f2-`";
        if [ "$LzmaBaseDir" = "" -a -d "$LzmaBaseDir" ]; then
            printf "\nInvalid option used.\n";
            ShowUsage "$@";
            exit 1;
        fi
        trap DestroySelf INT;;

    -h|--help)
        printf "\nAdobe Reader, an application to easily view, print and collaborate on PDF files.\n"
		ShowUsage;
		exit 0;;

	*)
        printf "\nInvalid option used.\n";
		ShowUsage "$@"
		exit 1 ;;
	esac

    shift
done

if [ ! -z "${ReadInstallDir}" ]; then
	CheckInstallDir "`ExpandDirPath "${ReadInstallDir}"`";
	if [ $? -ne 0 ]; then
        printf "\nExiting installation...\n";
		ShowUsage "$@"
		exit 1
	fi
fi

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

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

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



Version=9.5.4

#
# Get the filenames:
#

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


#
# Validate the configurations:
#

NumConfigs=0

if [ "$ReadTar" ] ; then
  if [ "$ReadSparcSolarisTar" ] ; then
    NumConfigs=`expr $NumConfigs + 1`
    ReadSparcSolarisNum=$NumConfigs
  fi
  if [ "$ReadIntelSolarisTar" ] ; then
    NumConfigs=`expr $NumConfigs + 1`
    ReadIntelSolarisNum=$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
  printf "\n%s\n" "ERROR: No configurations found ... aborting"
  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"
            ;;
          i386)
            ReadDefaultNum="$ReadIntelSolarisNum"
            DefaultName="Intel/Solaris"
            ;;
          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
  printf "%s\n" "WARNING: The chosen configuration will not run on your current platform (%s/%s)" "$OSname" "$OSrelease"
  if [ -z "$ReadInstallDir" ]; then
      if yesno "Continue installation?  [n] " 1 ; then
        :
      else
        exit 1
      fi
  fi
fi


#
# If multiple confurations, get the default configuration
#

if [ -z "$SingleConfig" ] ; then
  if [ "$ReadDefaultNum" ] ; then
      if [ -n "$ReadInstallDir" ]; then
        DefaultConfig="$ReadDefaultNum"
      elif 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
    #

    printf "\n%s\n" "The following configurations are available for installation:"

    if [ "$AcroSparcSolarisNum" ] ; then
      printf "%d. Acrobat %s for SPARC/Solaris 2.x.\n" "$AcroSparcSolarisNum" "${Version}"
    fi
    if [ "$AcroHppaHpuxNum" ] ; then
      printf "%d. Acrobat %s for HP-UX.\n" "$AcroHppaHpuxNum" "${Version}"
    fi
    if [ "$AcroRS6000AIXNum" ] ; then
      printf "%d. Acrobat %s for AIX.\n" "$AcroRS6000AIXNum" "${Version}"
    fi
    if [ "$AcroIntelLinuxNum" ] ; then
      printf "%d. Acrobat %s for Intel/Linux.\n" "$AcroIntelLinuxNum" "${Version}"
    fi
    if [ "$ReadSparcSolarisNum" ] ; then
      printf "%d. Acrobat Reader %s for SPARC/Solaris 2.x.\n" "$ReadSparcSolarisNum" "${Version}"
    fi
    if [ "$ReadHppaHpuxNum" ] ; then
      printf "%d. Acrobat Reader %s for HP-UX.\n" "$ReadHppaHpuxNum" "${Version}"
    fi
    if [ "$ReadRS6000AIXNum" ] ; then
      printf "%d. Acrobat Reader %s for AIX.\n" "$ReadRS6000AIXNum" "${Version}"
    fi
    if [ "$ReadIntelLinuxNum" ] ; then
      printf "%d. Adobe Reader %s for Intel/Linux.\n" "$ReadIntelLinuxNum" "${Version}"
    fi
    if [ "$ReadIntelSolarisNum" ] ; then
      printf "%d. Adobe Reader %s for Intel/Solaris.\n" "$ReadIntelSolarisNum" "${Version}"
    fi
    if [ "$ExitNum" ] ; then
        printf "%d. Exit Adobe Reader installation.\n" "${ExitNum}"
    fi

    printf "\n%s" "Enter the configuration number to install: "
    read ConfigNum

  fi


  #
  # Install the configuration
  #

  if [ -z "$ConfigNum" ] ; then
    printf "\n%s\n" "ERROR: Invalid response ... try again"
    continue
  elif [ "$ConfigNum" = "$ReadSparcSolarisNum" ] ; then
    InstallReader sparcsolaris "$ReadSparcSolarisTar" \
      "$SearchSparcSolarisTar" "$CustomSparcSolarisTar"
    ReadSparcSolarisNum=""
  elif [ "$ConfigNum" = "$ReadIntelSolarisNum" ] ; then
    InstallReader intelsolaris "$ReadIntelSolarisTar" \
      "$SearchIntelSolarisTar" "$CustomIntelSolarisTar"
    ReadIntelSolarisNum=""
  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
    printf "%s\n" "ERROR: Invalid response ... try again"
    continue
  fi

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

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

  NumConfigs=`expr $NumConfigs - 1`

done

printf "\n"
exit 0
