#!/bin/sh
#
# Copyright (c) 1996-2007, Adobe Systems Incorporated
# All Rights Reserved
#

#Used to specify the start directory for finding acroread
INSTALL_PATH=

#Contains the install directory path for acroread
ACROREAD_DIR=

#Contains the complete path of the browser plugin
NPPDF_FILE=

#Maintains whether we need to continue with the installation and repeat it for more browsers
INSTALL_MORE="y"


#This function records the location where we copy nppdf.so so that it can be removed during uninstall.
record_config()
{
    config_file="${ACROREAD_DIR}/Adobe/Reader9/Reader/GlobalPrefs/.config"
    echo "$@" >> "$config_file"
}


#This function gets the complete path of Adobe Reader according to this script.
GetInstallPath()
{
	CURRENT_PATH=`pwd`
	INSTALL_PATH=`dirname "$0"`
	#This would give us <actual_install_path>/Adobe/Reader9/Browser.
	#We need to strip it of Adobe/Reader9/Browser.
	COMPLETE_PATH="${INSTALL_PATH}/../../.."
	cd "$COMPLETE_PATH"
	INSTALL_PATH=`pwd`
	cd "$CURRENT_PATH"
}


#This function installs the plugin for a specific installation of the browser.
#If the installation is successful, then whenever the browser (specified in the browser install path) is run, for any user, it will have the plugin enabled.
#The way to achieve this is to look for the 'plugins' folder in the browser install path (specified by the user), and copy our plugin file to this location.
#If the plugin already exists, we prompt the user whether to overwrite or not, and proceed accordingly.
#This mechanism has been tested for Mozilla and Firefox. In both these browsers, the app searches for extensions/plugins in the 'plugins' folder.
InstallBrowserGlobal()
{
    if [ -z "$NPPDF_FILE" ]
	then
	return 1
    fi

    #Fetch the browser location
    if [ $# -eq 0 ]
	then
	BROWSER_DIR=""
	while [ -z "$BROWSER_DIR" ]
	  do
	  printf "Enter the browser install directory - \n"
	  read BROWSER_DIR
	done
    else
	BROWSER_DIR=$1
    fi
	
    #Check existence of browser folder
    if [ ! -d "$BROWSER_DIR" ]
	then
	printf "Could not find the browser folder %s\n" "${BROWSER_DIR}"
    printf "%s\n" "Installation cancelled."
	return 1
    fi

    #Check existence of 'plugin' folder in the browser folder
    BROWSER_PLUGIN_DIR="${BROWSER_DIR}/plugins"
    if [ ! -d "$BROWSER_PLUGIN_DIR" ]
	then
	printf "Could not find the browser plugins folder %s\n" "${BROWSER_PLUGIN_DIR}"
	return 1
    fi

    #Check whether plugin already exists
    if [ "$OSNAME" = "HP-UX" ]
        then
	BROWSER_PLUGIN_FILE="${BROWSER_PLUGIN_DIR}/nppdf.sl"
    else
	BROWSER_PLUGIN_FILE="${BROWSER_PLUGIN_DIR}/nppdf.so"
    fi

    if [ -f "$BROWSER_PLUGIN_FILE" ] || [ -h "$BROWSER_PLUGIN_FILE" ]
	then
	while :
	  do
	  if [ "$install_silent" = 0 ]; then
		  printf "The plugin seems to be already installed. Are you sure you want to overwrite ? [%s/%s] " "y" "n"
		  read USER_CHOICE
	  else
		  USER_CHOICE="y";
	  fi
	  
	  if [ "$USER_CHOICE" = "y" ] || [ "$USER_CHOICE" = "Y" ]
	      then
	      rm -f "$BROWSER_PLUGIN_FILE"
	      if [ $? -ne 0 ]
		  then
		  printf "Could not remove previous version of the plugin in %s.\n" "${BROWSER_PLUGIN_DIR}"
		  return 1
	      fi
	      break
	  elif [ "$USER_CHOICE" = "n" ] || [ "$USER_CHOICE" = "N" ]
	      then
	      printf "%s\n" "Installation cancelled."
	      return 0
	      break
	  fi
	done
    elif [ -b "$BROWSER_PLUGIN_FILE" ] || [ -c "$BROWSER_PLUGIN_FILE" ] || [ -d "$BROWSER_PLUGIN_FILE" ]
	then
	printf "%s  already exists as a special file and cannot overwrite it.\n" "$BROWSER_PLUGIN_FILE"
	return 1
    fi
    
    #Copy the browser plugin file to the correct location
    cp -f "$NPPDF_FILE" "$BROWSER_PLUGIN_FILE"
    if [ $? -ne 0 ]
	then
	printf "Could not copy the plugin file  %s  to %s\n" "${NPPDF_FILE}" "${BROWSER_PLUGIN_FILE}"
	return 1
    else
    record_config remove "${BROWSER_PLUGIN_FILE}"
	printf "Installation successful.\n" "${BROWSER_PLUGIN_FILE}"
	return 0
    fi
}


#This function installs the plugin for the current user only.
#If the installation is successful, then whenever Mozilla or Firefox is run, from anywhere, it will have the plugin enabled.
#The way to achieve this is to copy our plugin file in the .mozilla/plugins folder in the users' home directory.
#If the folder does not exist, we create it, and if the plugin already exists, we prompt the user whether to overwrite or not, and proceed accordingly.
#This mechanism has been tested for Mozilla and Firefox. In both these browsers, the app searches for extensions/plugins in ~/.mozilla/plugins folder.
InstallBrowserUser()
{
    if [ -z "$NPPDF_FILE" ]
	then
	return 1
    fi
    
    if [ -d "$HOME" ]
	then
	USER_DIR="$HOME"
    else
	printf "%s\n" "Unable to query HOME variable from the environment. Cannot find your home directory."
	return 1
    fi

    CHOWN_USER="${USER}"
    if [ "${SUDO_USER+set}" = "set" -a -n "${SUDO_USER}" ]; then
        CHOWN_USER="${SUDO_USER}"
    fi

    OSNAME=`uname -s`
    if [ "$OSNAME" = "Linux" ]; then
        ID_COMMAND="`Which id`"
    elif [ "$OSNAME" = "SunOS" ]; then
        ID_COMMAND="/usr/xpg4/bin/id"
    else
        ID_COMMAND=""
    fi
    if [ -n "${ID_COMMAND}" ]; then
        CHOWN_GRP="`"${ID_COMMAND}" -gn "${CHOWN_USER}" >/dev/null 2>&1`"
    else
        CHOWN_GRP=""
    fi
    CHOWN_CMD="chown ${CHOWN_USER}"
    if [ -n "${CHOWN_GRP}" ]; then
        CHOWN_CMD="${CHOWN_CMD}:${CHOWN_GRP}"
    fi

    #Create the .mozilla & plugins folders if required
    BROWSER_DIR="$USER_DIR/.mozilla"
    if [ ! -d "$BROWSER_DIR" ]
	then
	mkdir "$BROWSER_DIR"
	if [ $? -ne 0 ]
	    then
	    printf "Could not create the directory %s.\n" "${BROWSER_DIR}"
	    return 1
    else
        eval "${CHOWN_CMD} ${BROWSER_DIR}" 2>/dev/null
	fi
    fi
    
    BROWSER_PLUGIN_DIR="${BROWSER_DIR}/plugins"
    if [ ! -d "$BROWSER_PLUGIN_DIR" ]
	then
	mkdir "$BROWSER_PLUGIN_DIR"
	if [ $? -ne 0 ]
	    then
	    printf "Could not create the directory %s.\n" "${BROWSER_PLUGIN_DIR}"
	    return 1
	fi
    fi
    
    #Check whether the plugin already exists
    if [ "$OSNAME" = "HP-UX" ]
        then
	BROWSER_PLUGIN_FILE="${BROWSER_PLUGIN_DIR}/nppdf.sl"
    else
	BROWSER_PLUGIN_FILE="${BROWSER_PLUGIN_DIR}/nppdf.so"
    fi

    if [ -f "$BROWSER_PLUGIN_FILE" ] || [ -h "$BROWSER_PLUGIN_FILE" ]
	then
	while :
	  do
	  if [ "$install_silent" = 0 ]; then
		  printf "The plugin seems to be already installed. Are you sure you want to overwrite ? [%s/%s] " "y" "n"
		  read USER_CHOICE
	  else
		  USER_CHOICE="y";
	  fi
	  
	  if [ "$USER_CHOICE" = "y" ] || [ "$USER_CHOICE" = "Y" ]
	      then
	      rm -f "$BROWSER_PLUGIN_FILE"
	      if [ $? -ne 0 ]
		  then
		  printf "Could not remove previous version of the plugin in %s.\n" "${BROWSER_PLUGIN_DIR}"
		  return 1
	      fi
	      break
	  elif [ "$USER_CHOICE" = "n" ] || [ "$USER_CHOICE" = "N" ]
	      then
	      printf "%s\n" "Installation cancelled."
	      return 0
	      break
	  fi
	done
    elif [ -b "$BROWSER_PLUGIN_FILE" ] || [ -c "$BROWSER_PLUGIN_FILE" ] || [ -d "$BROWSER_PLUGIN_FILE" ]
	then
	printf "%s  already exists as a special file and cannot overwrite it.\n" "$BROWSER_PLUGIN_FILE"
	return 1
    fi
    
    #Copy the browser plugin file to the correct location
    cp "$NPPDF_FILE" "$BROWSER_PLUGIN_FILE"
    if [ $? -ne 0 ]
	then
	printf "Could not copy the plugin file  %s  to %s\n" "${NPPDF_FILE}" "${BROWSER_PLUGIN_FILE}"
	return 1
	else
    eval "${CHOWN_CMD} ${BROWSER_PLUGIN_FILE}" 2>/dev/null
    record_config remove "${BROWSER_PLUGIN_FILE}"
    fi
    
    printf "%s\n" "Installation successful."
    printf "%s  has been added.\n" "${BROWSER_PLUGIN_FILE}"
    printf "%s\n" "This will enable the plugin for Mozilla, Firefox and Netscape."
}


#
# Check a given file in given path
# 1 imples file exists and 0 implies file does not exist
#
check_file_in_named_path()
{
    if [ -x "$2/$1" ]
	then
 	return 1
    fi
    
    return 0
}


#
# Check a given file in $PATH
# 1 imples file exists and 0 implies file does not exist
#
check_file_in_path()
{
    OLD_IFS=$IFS
    IFS=":"
    FOUND_PATH=""

    if [ -n "$PATH" ]
	then
 	for i in $PATH
	  do
	  if [ -d "$i" ]
	      then
	      check_file_in_named_path "$1" "$i"
	      if [ $? -eq 1 ]
		  then
		  FOUND_PATH="${i}/$1"
		  IFS=$OLD_IFS
		  return 1
	      fi
	  fi
 	done
    fi
    
    IFS=$OLD_IFS
    return 0
}


GetAbsoluteFileName()
{
    if [ -h "$1" ]
	then
	CURRENT_PATH=`pwd`
	cd `dirname $1`
	COMPLETE_PATH=`ls -l $1 | awk '{ print $NF }'`
	COMPLETE_PATH_DIR=`dirname $COMPLETE_PATH`
	COMPLETE_PATH_FILE=`basename $COMPLETE_PATH`
	cd $COMPLETE_PATH_DIR
	COMPLETE_PATH_DIR=`pwd`
	cd "$CURRENT_PATH"
	GetAbsoluteFileName ${COMPLETE_PATH_DIR}/${COMPLETE_PATH_FILE}
    else
	echo "$1"
	return 0
    fi
}

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

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
}

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
}


AutoInstallSystemPath()
{
    browser_dirs="$@"
    status=0

    if [ -d "${browser_dirs}" ]; then
        printf "\nInstalling plugin in %s\n" "${browser_dirs}"
        InstallBrowserGlobal "${browser_dirs}"
        status=`expr $status \| $?`
    fi

    return $status
}


AutoInstallBrowser()
{
    printf "Trying to install plugin for browser  -  %s\n" "$1"
    check_file_in_path $1
    if [ $? -eq 1 ]
	then
	FOUND_PATH="`eval GetAbsoluteFileName \"$FOUND_PATH\"`"
	printf "Installing plugin in %s\n" "$FOUND_PATH"
	InstallBrowserGlobal `dirname $FOUND_PATH`
	if [ $? -eq 0 ]
	    then
	    return 0
	else
	    printf "%s\n" "Installation failed"
	    return 1
	fi
    else
	printf "%s\n" "Installation failed"
	return 1
    fi
}


AutoInstall()
{
    AUTO_INSTALL_SUCCESS=0

    AutoInstallBrowser "firefox"
    if [ $? -eq 0 ]
	then
	   AUTO_INSTALL_SUCCESS=1
    fi

    USR_LIB_DIR="/usr/lib"

    filename="`MkTemp`"
    ls -1 ${USR_LIB_DIR} | grep '^firefox' > "${filename}"
    i=1
    l=`wc -l "${filename}" | awk -F' ' '{print $1}'`

    while [ ${i} -le ${l} ]
    do
        BROWSER_LIB_DIR=`head -${i} "${filename}" | tail -1`
        BROWSER_LIB_DIR="${USR_LIB_DIR}/${BROWSER_LIB_DIR}" ;

        i=`expr ${i} + 1`

        AutoInstallSystemPath "${BROWSER_LIB_DIR}" ;
    done


    if [ $? -eq 0 ]
    then
	   AUTO_INSTALL_SUCCESS=1
    fi

    rm -f "${filename}"

    AutoInstallBrowser "mozilla"
    if [ $? -eq 0 ]
	then
	   AUTO_INSTALL_SUCCESS=1
    fi

    filename="`MkTemp`"
    ls -1 ${USR_LIB_DIR} | grep '^mozilla' > "${filename}"
    i=1
    l=`wc -l "${filename}" | awk -F' ' '{print $1}'`

    while [ ${i} -le ${l} ]
    do
        BROWSER_LIB_DIR=`head -${i} "${filename}" | tail -1`
        BROWSER_LIB_DIR="${USR_LIB_DIR}/${BROWSER_LIB_DIR}" ;

        i=`expr ${i} + 1`

        AutoInstallSystemPath "${BROWSER_LIB_DIR}" ;
    done

    if [ $? -eq 0 ]
    then
	   AUTO_INSTALL_SUCCESS=1
    fi

    rm -f "${filename}"

    if [ $AUTO_INSTALL_SUCCESS -eq 1 ]
	then
	   printf "%s\n" "Finished with automatic install."
	   return 0
    else
	   return 1
    fi
}


#This function fetches the installation path if not specified and validates the existance of the plugin file.
GetAcroreadInfo()
{
    #Prompt the user for acroread's installation path if not specified on the command prompt
    if [ -z "$1" ]
	then
	GetInstallPath
	printf "Enter the install directory for Adobe Reader 9.1 [%s] " "${INSTALL_PATH}"
	read ACROREAD_DIR

	if [ -z "$ACROREAD_DIR" ]
	    then
	    ACROREAD_DIR="$INSTALL_PATH"
	fi
    else
	ACROREAD_DIR="$1"
    fi

    OSNAME=`uname -s`
    if [ "$OSNAME" = "Linux" ]
	then
	CONFIG_FOLDER="intellinux"
    elif [ "$OSNAME" = "SunOS" ]
	then
    ARCHTYPE=`uname -p`
    case "${ARCHTYPE}" in
        sparc) CONFIG_FOLDER=sparcsolaris;;
        i*86)  CONFIG_FOLDER=intelsolaris;;
    esac
    elif [ "$OSNAME" = "AIX" ]
        then
	CONFIG_FOLDER="rs6000aix"
    elif [ "$OSNAME" = "HP-UX" ]
        then
	CONFIG_FOLDER="hppahpux"
    else
	printf "%s\n" "INTERNAL ERROR ..... No matching OS name."
	return 1
    fi

    #Check for the plugin file
    if [ "$OSNAME" = "HP-UX" ]
        then
	NPPDF_FILE="${ACROREAD_DIR}/Adobe/Reader9/Browser/${CONFIG_FOLDER}/nppdf.sl"
    else
	NPPDF_FILE="${ACROREAD_DIR}/Adobe/Reader9/Browser/${CONFIG_FOLDER}/nppdf.so"
    fi

    if [ ! -f "$NPPDF_FILE" ]
	then
	printf "Could not find the browser plugin file  %s.\n" "${NPPDF_FILE}"
	return 1
    fi
}


Init()
{
    printf "%s\n\n" "This will install the browser plugin for acroread."
    GetAcroreadInfo "$1"
    if [ $? -ne 0 ]
	then
	return 1
    fi

    if [ ! -d "$ACROREAD_DIR" ]
	then
	printf "Could not find the installation folder  %s.\n" "$ACROREAD_DIR"
	return 1
    fi

    while :
    do
        printf "\n\n%s\n\n" "What do you want to perform ? "
        printf "%d. %s - %s\n" 1 "Global installation (as root)" "this will try to install the plugin for mozilla/firefox, whichever it is able to locate."
        printf "%d. %s - %s\n" 2 "Perform user-specific installation" "this will install the plugin for current user only."
        printf "%d. %s\n" 3 "Exit"
        printf "\n%s" "Enter your choice : "
        read answer

        case "$answer" in
            1)  AutoInstall || InstallBrowserGlobal ;;
            2)  InstallBrowserUser ;;
            3)  break ;;
        esac
    done

    return 0
}

EndingMessage()
{
    if [ -n "${NPPDF_FILE}" ]
	then
	printf "\n\nIf you are facing any problem in getting the installation to work for your browser, please copy the following file to the plugin folder of the browser : %s\n\n" "${NPPDF_FILE}"
    fi

    printf "%s\n" "In case of difficulties please refer to the documentation provided along with the browser for addition of new plugins."
}


ProcessCmdLineOpts()
{
    install_silent=0
    install_global=0
    install_user=0
    install_path=""

    for i in $@
    do
        case "$i" in
            "-silent") install_silent=1 ;;
            "-global") install_global=1 ;;
            "-user")   install_user=1 ;;
            *)         install_path="$i" ;;
        esac
    done
}


ProcessCmdLineOpts "$@"

if [ "$install_silent" = 1 ]
then
    if [ -n "$install_path" ]
    then
        GetAcroreadInfo "$install_path"
        (
            if [ "$install_global" = 1 ]; then
                AutoInstall
            fi

            if [ "$install_user" = 1 ]; then
                InstallBrowserUser
            fi
        ) >/dev/null 2>&1
    else
        exit 1
    fi
else
    trap 'EndingMessage; exit 0' 2
    if [ "$install_global" = 1 ]
    then
        GetAcroreadInfo
        AutoInstall || InstallBrowserGlobal
    fi

    if [ "$install_user" = 1 ]
    then
        GetAcroreadInfo
        InstallBrowserUser
    fi

    if [ "$install_user" = 0 -a "$install_global" = 0 ]
    then
        Init "$1"
    fi

    EndingMessage
fi

