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

#Used to specify the start directory for finding acroread

set_lang_KOR_utf()
{
    ST_IBP_INFO_12="브라우저에서 설치 프로그램이 작동하지 않으면 다음 파일을 브라우저의 플러그인 폴더로 복사하십시오: %s."
    ST_IBP_ERR_06="%s 디렉토리를 만들 수 없습니다."
    ST_IBP_INFO_26="종료"
    ST_IBP_INFO_01="acroread용 브라우저 플러그인을 설치합니다."
    ST_IBP_INFO_23="%s이(가) 추가되었습니다."
    ST_IBP_INFO_04="선택 사항을 입력하십시오."
    ST_IBP_ERR_03="브라우저 폴더 %s을(를) 찾을 수 없습니다."
    ST_SMALL_N="n"
    ST_IBP_ERR_01="브라우저 플러그인 파일 %s을(를) 찾을 수 없습니다."
    ST_IBP_INFO_28="플러그인이 현재 사용자에게만 설치됩니다."
    ST_IBP_INFO_07="브라우저 설치 디렉토리를 입력하십시오."
    ST_IBP_INFO_25="설치 작업이 실패했습니다."
    ST_IBP_INFO_17="어떤 작업을 수행하시겠습니까? "
    ST_IBP_INFO_11="이제 Mozilla, Firefox 및 Netscape의 플러그인을 사용할 수 있습니다. "
    ST_IBP_INFO_09="설치가 취소되었습니다."
    ST_CAPS_N="N"
    ST_IBP_ERR_11="내부 오류. 일치하는 OS 이름이 없습니다."
    ST_IBP_ERR_05="플러그인 파일 %s을(를) %s에 복사할 수 없습니다."
    ST_IBP_INFO_03="사용자별 설치 수행"
    ST_IBP_ERR_08="%s이(가) 이미 특수 파일로 존재하므로 덮어쓸 수 없습니다."
    ST_IBP_INFO_24="%s 브라우저용 플러그인을 설치합니다."
    ST_IBP_INFO_16="%s에 플러그인 설치 중"
    ST_IBP_INFO_15="새 플러그인 추가에 문제가 있다면 브라우저와 함께 제공된 설명서를 참조하십시오."
    ST_IBP_INFO_27="Mozilla/Firefox 중 찾을 수 있는 브라우저의 플러그인이 설치됩니다."
    ST_IBP_ERR_10="설치 폴더 %s을(를) 찾을 수 없습니다."
    ST_IBP_INFO_06="Adobe Reader 8.1[%s] 설치 디렉토리를 입력하십시오."
    ST_CAPS_Y="Y"
    ST_IBP_INFO_18="자동 설치가 완료되었습니다."
    ST_IBP_ERR_09="해당 환경에서 HOME 변수를 쿼리할 수 없습니다. 홈 디렉토리를 찾을 수 없습니다."
    ST_IBP_ERR_02="브라우저 플러그인 폴더 %s을(를) 찾을 수 없습니다."
    ST_IBP_INFO_08="플러그인이 이미 설치되었습니다. 덮어쓰시겠습니까? [%s/%s] "
    ST_SMALL_Y="y"
    ST_IBP_INFO_10="성공적으로 설치되었습니다."
    ST_IBP_ERR_04="%s에서 이전 버전의 플러그인을 제거할 수 없습니다."
    ST_IBP_INFO_02="전체 설치(루트로)"
}


set_lang_KOR()
{
    ST_IBP_INFO_12=" ġ α׷ ۵     ÷  Ͻʽÿ: %s."
    ST_IBP_ERR_06="%s 丮   ϴ."
    ST_IBP_INFO_26=""
    ST_IBP_INFO_01="acroread  ÷ ġմϴ."
    ST_IBP_INFO_23="%s() ߰Ǿϴ."
    ST_IBP_INFO_04="  ԷϽʽÿ."
    ST_IBP_ERR_03="  %s() ã  ϴ."
    ST_SMALL_N="n"
    ST_IBP_ERR_01=" ÷  %s() ã  ϴ."
    ST_IBP_INFO_28="÷  ڿԸ ġ˴ϴ."
    ST_IBP_INFO_07=" ġ 丮 ԷϽʽÿ."
    ST_IBP_INFO_25="ġ ۾ ߽ϴ."
    ST_IBP_INFO_17=" ۾ Ͻðڽϱ? "
    ST_IBP_INFO_11=" Mozilla, Firefox  Netscape ÷   ֽϴ. "
    ST_IBP_INFO_09="ġ ҵǾϴ."
    ST_CAPS_N="N"
    ST_IBP_ERR_11=" . ġϴ OS ̸ ϴ."
    ST_IBP_ERR_05="÷  %s() %s   ϴ."
    ST_IBP_INFO_03="ں ġ "
    ST_IBP_ERR_08="%s() ̹ Ư Ϸ ϹǷ   ϴ."
    ST_IBP_INFO_24="%s  ÷ ġմϴ."
    ST_IBP_INFO_16="%s ÷ ġ "
    ST_IBP_INFO_15=" ÷ ߰  ִٸ  Բ   Ͻʽÿ."
    ST_IBP_INFO_27="Mozilla/Firefox  ã  ִ  ÷ ġ˴ϴ."
    ST_IBP_ERR_10="ġ  %s() ã  ϴ."
    ST_IBP_INFO_06="Adobe Reader 8.1[%s] ġ 丮 ԷϽʽÿ."
    ST_CAPS_Y="Y"
    ST_IBP_INFO_18="ڵ ġ ϷǾϴ."
    ST_IBP_ERR_09="ش ȯ濡 HOME    ϴ. Ȩ 丮 ã  ϴ."
    ST_IBP_ERR_02=" ÷  %s() ã  ϴ."
    ST_IBP_INFO_08="÷ ̹ ġǾϴ. ðڽϱ? [%s/%s] "
    ST_IBP_INFO_10=" ġǾϴ."
    ST_SMALL_Y="y"
    ST_IBP_ERR_04="%s   ÷   ϴ."
    ST_IBP_INFO_02="ü ġ(Ʈ)"
}

if echo $LANG | grep -i utf >/dev/null; then
    set_lang_KOR_utf
else
    set_lang_KOR
fi

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 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/Reader8/Browser.
	#We need to strip it of Adobe/Reader8/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 "$ST_IBP_INFO_07 - \n"
	  read BROWSER_DIR
	done
    else
	BROWSER_DIR=$1
    fi
	
    #Check existence of browser folder
    if [ ! -d "$BROWSER_DIR" ]
	then
	printf "$ST_IBP_ERR_03\n" "${BROWSER_DIR}"
    printf "%s\n" "$ST_IBP_INFO_09"
	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 "$ST_IBP_ERR_02\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
	  printf "$ST_IBP_INFO_08" "$ST_SMALL_Y" "$ST_SMALL_N"
	  read USER_CHOICE
	  
	  if [ "$USER_CHOICE" = "$ST_SMALL_Y" ] || [ "$USER_CHOICE" = "$ST_CAPS_Y" ]
	      then
	      rm -f "$BROWSER_PLUGIN_FILE"
	      if [ $? -ne 0 ]
		  then
		  printf "$ST_IBP_ERR_04\n" "${BROWSER_PLUGIN_DIR}"
		  return 1
	      fi
	      break
	  elif [ "$USER_CHOICE" = "$ST_SMALL_N" ] || [ "$USER_CHOICE" = "$ST_CAPS_N" ]
	      then
	      printf "%s\n" "$ST_IBP_INFO_09"
	      return 0
	      break
	  fi
	done
    elif [ -b "$BROWSER_PLUGIN_FILE" ] || [ -c "$BROWSER_PLUGIN_FILE" ] || [ -d "$BROWSER_PLUGIN_FILE" ]
	then
	printf "$ST_IBP_ERR_08\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 "$ST_IBP_ERR_05\n" "${NPPDF_FILE}" "${BROWSER_PLUGIN_FILE}"
	return 1
    else
	printf "$ST_IBP_INFO_10\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" "$ST_IBP_ERR_09"
	return 1
    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 "$ST_IBP_ERR_06\n" "${BROWSER_DIR}"
	    return 1
	fi
    fi
    
    BROWSER_PLUGIN_DIR="${BROWSER_DIR}/plugins"
    if [ ! -d "$BROWSER_PLUGIN_DIR" ]
	then
	mkdir "$BROWSER_PLUGIN_DIR"
	if [ $? -ne 0 ]
	    then
	    printf "$ST_IBP_ERR_06\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
	  printf "$ST_IBP_INFO_08" "$ST_SMALL_Y" "$ST_SMALL_N"
	  read USER_CHOICE
	  
	  if [ "$USER_CHOICE" = "$ST_SMALL_Y" ] || [ "$USER_CHOICE" = "$ST_CAPS_Y" ]
	      then
	      rm -f "$BROWSER_PLUGIN_FILE"
	      if [ $? -ne 0 ]
		  then
		  printf "$ST_IBP_ERR_04\n" "${BROWSER_PLUGIN_DIR}"
		  return 1
	      fi
	      break
	  elif [ "$USER_CHOICE" = "$ST_SMALL_N" ] || [ "$USER_CHOICE" = "$ST_CAPS_N" ]
	      then
	      printf "%s\n" "$ST_IBP_INFO_09"
	      return 0
	      break
	  fi
	done
    elif [ -b "$BROWSER_PLUGIN_FILE" ] || [ -c "$BROWSER_PLUGIN_FILE" ] || [ -d "$BROWSER_PLUGIN_FILE" ]
	then
	printf "$ST_IBP_ERR_08\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 "$ST_IBP_ERR_05\n" "${NPPDF_FILE}" "${BROWSER_PLUGIN_FILE}"
	return 1
    fi
    
    printf "%s\n" "$ST_IBP_INFO_10"
    printf "$ST_IBP_INFO_23\n" "${BROWSER_PLUGIN_FILE}"
    printf "%s\n" "$ST_IBP_INFO_11"
}


#
# 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
}


GetAbsoluteFile()
{
    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"
	GetAbsoluteFile ${COMPLETE_PATH_DIR}/${COMPLETE_PATH_FILE}
    else
	echo "$1"
	return 0
    fi
}


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

    for i in $browser_dirs
    do
        if [ -d "$i" ]; then
            printf "\n$ST_IBP_INFO_16\n" "$i"
            InstallBrowserGlobal "$i"
            status=`expr $status \| $?`
        fi
    done

    return $status
}


AutoInstallBrowser()
{
    printf "$ST_IBP_INFO_24\n" "$1"
    check_file_in_path $1
    if [ $? -eq 1 ]
	then
	FOUND_PATH=`GetAbsoluteFile $FOUND_PATH`
	printf "$ST_IBP_INFO_16\n" "$FOUND_PATH"
	InstallBrowserGlobal `dirname $FOUND_PATH`
	if [ $? -eq 0 ]
	    then
	    return 0
	else
	    printf "%s\n" "$ST_IBP_INFO_25"
	    return 1
	fi
    else
	printf "%s\n" "$ST_IBP_INFO_25"
	return 1
    fi
}


AutoInstall()
{
    AUTO_INSTALL_SUCCESS=0

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

    AutoInstallSystemPath /usr/lib/firefox*
    if [ $? -eq 0 ]
    then
	   AUTO_INSTALL_SUCCESS=1
    fi

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

    AutoInstallSystemPath /usr/lib/mozilla*
    if [ $? -eq 0 ]
    then
	   AUTO_INSTALL_SUCCESS=1
    fi

    if [ $AUTO_INSTALL_SUCCESS -eq 1 ]
	then
	   printf "%s\n" "$ST_IBP_INFO_18"
	   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 "$ST_IBP_INFO_06" "${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
	CONFIG_FOLDER="sparcsolaris"
    elif [ "$OSNAME" = "AIX" ]
        then
	CONFIG_FOLDER="rs6000aix"
    elif [ "$OSNAME" = "HP-UX" ]
        then
	CONFIG_FOLDER="hppahpux"
    else
	printf "%s\n" "$ST_IBP_ERR_11"
	return 1
    fi

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

    if [ ! -f "$NPPDF_FILE" ]
	then
	printf "$ST_IBP_ERR_01\n" "${NPPDF_FILE}"
	return 1
    fi
}


Init()
{
    clear
    printf "%s\n\n" "$ST_IBP_INFO_01"
    GetAcroreadInfo "$1"
    if [ $? -ne 0 ]
	then
	return 1
    fi

    if [ ! -d "$ACROREAD_DIR" ]
	then
	printf "$ST_IBP_ERR_10\n" "$ACROREAD_DIR"
	return 1
    fi

    while :
    do
        printf "\n\n%s\n\n" "$ST_IBP_INFO_17"
        printf "%d. %s - %s\n" 1 "$ST_IBP_INFO_02" "$ST_IBP_INFO_27"
        printf "%d. %s - %s\n" 2 "$ST_IBP_INFO_03" "$ST_IBP_INFO_28"
        printf "%d. %s\n" 3 "$ST_IBP_INFO_26"
        printf "\n%s" "$ST_IBP_INFO_04"
        read answer

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

    return 0
}

EndingMessage()
{
    if [ -n "${NPPDF_FILE}" ]
	then
	printf "\n\n$ST_IBP_INFO_12\n\n" "${NPPDF_FILE}"
    fi

    printf "%s\n" "$ST_IBP_INFO_15"
}


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"
        yes "$ST_SMALL_Y" | head -10 | \
        (
            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

