#!/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="o"



#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 "Saisir le rpertoire d'installation du navigateur - "
	  read browser_dir
	  if [ -z "$browser_dir" ]
	      then
	      echo "L'installation du module externe requiert la spcification du rpertoire du navigateur."
	      break
	  fi
	  
	  browser_plugin_dir="$browser_dir/plugins"
	  if [ ! -d "$browser_plugin_dir" ]
	      then
	      echo "Dossier des modules externes de navigation introuvable ${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 "Le module externe est dj install. Voulez-vous vraiment le remplacer?  [o/n] "
		read overwrite_choice
	      
		if [ "$overwrite_choice" = "o" ]
		    then
		    rm -f "$browser_plugin_file"
		    if [ $? -ne 0 ]
			then
			echo "Impossible de supprimer la version prcdente du module externe de ${browser_plugin_dir}"
			install_err=1
		    fi
		    break
		elif [ "$overwrite_choice" = "n" ]
		    then
		    echo "Installation annule"
		    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 "Impossible de copier le fichier du module externe ${nppdf_file} vers ${browser_plugin_file}"
	      break
	  else
	      echo "Installation russie. Ajout du fichier ${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 "Impossible de crer le rpertoire ${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 "Impossible de crer le rpertoire ${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 "Le module externe est dj install. Voulez-vous vraiment le remplacer?  [o/n] "
	      read overwrite_choice
	      
	      if [ "$overwrite_choice" = "o" ]
		  then
		  rm -f "$browser_plugin_file"
		  if [ $? -ne 0 ]
		      then
		      echo "Impossible de supprimer la version prcdente du module externe de ${browser_plugin_dir}"
		      exit 1
		  fi
		  break
	      elif [ "$overwrite_choice" = "n" ]
		  then
		  echo "Installation annule"
		  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 "Impossible de copier le fichier du module externe ${nppdf_file} vers ${browser_plugin_file}"
	    exit 1
	fi
	
	echo "Installation russie. Ajout du fichier ${browser_plugin_file}"
	echo "Cela va activer le module externe pour Mozilla/Firefox/Netscape"
	echo ""
	echo "Pour les autres navigateurs, excutez  nouveau ce script et choisissez Effectuer l'installation globale"
	echo "ou copiez le fichier manuellement ${nppdf_file}"
	echo "dans le dossier du module externe du navigateur."
	echo ""
	echo "En cas de difficults, reportez-vous  la documentation accompagnant le navigateur pour des informations supplmentaires relatives aux nouveaux modules externes."
	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 "Saisir le rpertoire d'installation pour Adobe Reader [${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 "Fichier du module externe de navigation introuvable ${nppdf_file}"
	exit 1
    fi
}


Init()
{
    echo "L'installation du module externe de navigation pour acroread va s'effectuer."
    echo ""
    GetAcroreadInfo "$1"

    while :
      do
      echo ""
      echo "1. Effectuer l'installation globale"
      echo "2. Effectuer l'installation spcifique  l'utilisateur (Mozilla/Firefox/Netscape)"
      echo -n "Saisir la slection voulue [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" = "o" ]
	    do
	    echo ""
	    InstallBrowserGlobal

	    while :
	      do
	      echo ""
	      echo -n "Voulez-vous installer un autre navigateur ? [o/n] "
	      read install_more
	      
	      if [ "$install_more" = "o" ] || [ "$install_more" = "n" ]
		  then
		  break
	      fi
	    done
	  done
	  break;
      elif [ $install_choice -eq 2 ]
	  then
	  echo ""
	  InstallBrowserUser
	  break
      fi
    done

    exit 0
}

Init "$1"
