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

#Used to specify the start directory for finding acroread
script_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 this script.
GetScriptPath()
{
	script_file=`which $0`
	script_path=`dirname "$script_file"`
	current_path=`pwd`
	complete_path="$script_path"/../
	cd "$complete_path"
	script_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, Firefox, Netscape and Opera. In all these browsers, the app searches for extensions/plugins in the 'plugins' folder.
InstallBrowserGlobal()
{
    if [ -n "$nppdf_file" ]
	then
	while :
	  do
	  #Fetch and validate the browser location
	  echo -n "пJsw˥ؿ - "
	  read browser_dir
	  if [ -z "$browser_dir" ]
	      then
	      echo "zݭnJsؿHw˼WļҲաC"
	      break
	  fi
	  
	  browser_plugin_dir="$browser_dir/plugins"
	  if [ ! -d "$browser_plugin_dir" ]
	      then
	      echo "LksWļҲոƧ ${browser_plugin_dir}"
	      break
	  fi
	  
	  #Check whether the file already exists
	  browser_plugin_file="$browser_plugin_dir/nppdf.so"
	  install_err=0
	  if [ -e "$browser_plugin_file" ]
	      then
	      while :
		do
		echo -n "WļҲզGwgwˡCzTwnܡH [y/n] "
		read overwrite_choice
	      
		if [ "$overwrite_choice" = "y" ]
		    then
		    rm -f "$browser_plugin_file"
		    if [ $? -ne 0 ]
			then
			echo "LkHeWļҲ ${browser_plugin_dir}"
			install_err=1
		    fi
		    break
		elif [ "$overwrite_choice" = "n" ]
		    then
		    echo "w˨C"
		    install_err=1
		    break
		fi
	      done
	  fi

	  if [ $install_err -eq 1 ]
	      then
	      break
	  fi
	  
	  #Copy the browser plugin file to the correct location
	  cp "$nppdf_file" "$browser_plugin_file"
	  if [ $? -ne 0 ]
	      then
	      echo "LkƻsWļҲɮ ${nppdf_file}  ${browser_plugin_file}"
	      break
	  else
	      echo "w˦CsWɮ ${browser_plugin_file}"
	      break;
	  fi

	  break
	done
    fi
}


#This function installs the plugin for the current user only.
#If the installation is successful, then whenever Mozilla or Firefox or Netscape 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, Firefox and Netscape. In all these browsers, the app searches for extensions/plugins in ~/.mozilla/plugins folder.
InstallBrowserUser()
{
    if [ -n "$nppdf_file" ]
	then
	current_dir=`pwd`
	cd ~
	user_dir=`pwd`
	
	#Create the .mozilla & plugins folders if required
	browser_dir="$user_dir/.mozilla"
	if [ ! -d "$browser_dir" ]
	    then
	    mkdir "$browser_dir"
	    if [ $? -ne 0 ]
		then
		echo "LkإߥؿC ${browser_dir}"
		exit 1
	    fi
	fi
	
	browser_plugin_dir="$browser_dir/plugins"
	if [ ! -d "$browser_plugin_dir" ]
	    then
	    mkdir "$browser_plugin_dir"
	    if [ $? -ne 0 ]
		then
		echo "LkإߥؿC ${browser_plugin_dir}"
		exit 1
	    fi
	fi
	
        #Check whether the file already exists
	browser_plugin_file="$browser_plugin_dir/nppdf.so"
	if [ -e "$browser_plugin_file" ]
	    then
	    while :
	      do
	      echo -n "WļҲզGwgwˡCzTwnܡH [y/n] "
	      read overwrite_choice
	      
	      if [ "$overwrite_choice" = "y" ]
		  then
		  rm -f "$browser_plugin_file"
		  if [ $? -ne 0 ]
		      then
		      echo "LkHeWļҲ ${browser_plugin_dir}"
		      exit 1
		  fi
		  break
	      elif [ "$overwrite_choice" = "n" ]
		  then
		  echo "w˨C"
		  exit 1
		  break
	      fi
	    done
	fi
	
        #Copy the browser plugin file to the correct location
	cp "$nppdf_file" "$browser_plugin_file"
	if [ $? -ne 0 ]
	    then
	    echo "LkƻsWļҲɮ ${nppdf_file}  ${browser_plugin_file}"
	    exit 1
	fi
	
	echo "w˦CsWɮ ${browser_plugin_file}"
	echo "N?μWļҲ Mozilla/Firefox/Netscape"
	echo ""
	echo "䥦sAAӵ{ɨÿܡuwˡvA"
	echo "Ϊ̱zݭnʽƻsɮ ${nppdf_file}"
	echo "sWļҲոƧC"
	echo ""
	echo "pGxAаѾHs@_ѪsWWļҲդC"
	cd "$current_dir"
    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
	GetScriptPath
	echo -n "пJ  Adobe Reader 7.0.0 w˥ؿC [${script_path}] "
	read acroread_dir
	if [ -z "$acroread_dir" ]
	    then
	    acroread_dir="$script_path"
	fi
    else
	acroread_dir="$1"
    fi

    #Check for the plugin file
    nppdf_file="$acroread_dir/Browser/intellinux/nppdf.so"
    if [ ! -f "$nppdf_file" ]
	then
	echo "LksWļҲɮ ${nppdf_file}"
	exit 1
    fi
}


Init()
{
    echo "Nws acroread WļҲաC"
    echo ""
    GetAcroreadInfo "$1"

    while :
      do
      echo ""
      echo "1. w"
      echo "2. ϥΪ̫ww (Mozilla/Firefox/Netscape)"
      echo -n "пJz [1/2] "
      read install_choice
      
      if [ $install_choice -eq 1 ]
	  then
	  #Till the user explicitly asks to quit, we keep on installing, looking for more browsers.
	  while [ "$install_more" = "y" ]
	    do
	    echo ""
	    InstallBrowserGlobal

	    while :
	      do
	      echo ""
	      echo -n "zO_nw˥t@sH [y/n] "
	      read install_more
	      
	      if [ "$install_more" = "y" ] || [ "$install_more" = "n" ]
		  then
		  break
	      fi
	    done
	  done
	  break;
      elif [ $install_choice -eq 2 ]
	  then
	  echo ""
	  InstallBrowserUser
	  break
      fi
    done

    exit 0
}

Init "$1"
