#! /bin/sh
#
# SyAM Install Script
#   Chris Chisholm
#  October 20, 2005
#

#product version 3.22 install

#########################################
####Menus

#---------------
#Language/Locale Selection

mnuLocale()
{
  insGen_mnuSel=-1
  while [ "$insGen_mnuSel" != "$fc" ]
  do

  clear
  echo -e "\033[1;44;33m================================================================="
  echo -e "|\033[1;36m Please Choose a Language                                       "
  echo -e "\033[1;33m=================================================================\033[0m"
  echo ""
  echo ""
  if [ "$insGen_locale" = "en" ]; then echo -n "*"; else echo -n " "; fi
  echo "[10]English (US)"
  
  if [ "$insGen_locale" = "en_GB" ]; then echo -n "*"; else echo -n " "; fi
  echo "[11]English (UK)"

  if [ "$insGen_locale" = "fr" ]; then echo -n "*"; else echo -n " "; fi
  echo "[12]French"
  
  if [ "$insGen_locale" = "fr_CA" ]; then echo -n "*"; else echo -n " "; fi
  echo "[13]French (Canada)"  

  if [ "$insGen_locale" = "de" ]; then echo -n "*"; else echo -n " "; fi
  echo "[14]German"

  if [ "$insGen_locale" = "pt_BR" ]; then echo -n "*"; else echo -n " "; fi
  echo "[15]Portuguese (Brazil)"

  if [ "$insGen_locale" = "pt" ]; then echo -n "*"; else echo -n " "; fi
  echo "[16]Portuguese (Portugal)"

  if [ "$insGen_locale" = "es" ]; then echo -n "*"; else echo -n " "; fi
  echo "[17]Spanish (Traditional)"

  if [ "$insGen_locale" = "es_AR" ]; then echo -n "*"; else echo -n " "; fi
  echo "[18]Spanish (Mexico)"

  if [ "$insGen_locale" = "ru" ]; then echo -n "*"; else echo -n " "; fi
  echo "[19]Russian"

  if [ "$insGen_locale" = "jp" ]; then echo -n "*"; else echo -n " "; fi
  echo "[20]Japanese"
  
  echo ""
  echo "                                     ${insMnu_trans[23]}"
  echo ""
  echo " [$fc]next"
  echo ""
  echo -n " enter an option: "

  read insGen_mnuSel

  #handle input

  case $insGen_mnuSel in
   "$fc")
     load_locale $insGen_locale
     ;;
   "10")
     insGen_locale="en"
     ;;
   "11")
     insGen_locale="en_GB"
     ;;
   "12")
     insGen_locale="fr"
     ;;
   "13")
     insGen_locale="fr_CA"
     ;;
   "14")
     insGen_locale="de"
     ;;
   "15")
     insGen_locale="pt_BR"
     ;;
   "16")
     insGen_locale="pt"
     ;;
   "17")
     insGen_locale="es"
     ;;
   "18")
     insGen_locale="es_AR"
     ;;
   "19")
     insGen_locale="ru"
     ;;
   "20")
     insGen_locale="jp"
     ;;

  esac

  done
}


#---------------
#Welcome Menu

mnuWelcome()
{

  insGen_mnuSel=-1
  while [ "$insGen_mnuSel" != "$fc" ] && [ "$insGen_mnuSel" != "$bc" ]
  do

  clear
  echo -e "\033[1;44;33m================================================================="
  echo -en "|\033[1;36m ${insMnu_welcome[0]}"
  if [ ${#insMnu_welcome[@]} -lt 62 ]; then
    for (( i = ${#insMnu_welcome[0]}; i <= 62; i++ ))
    do
      echo -n " "
    done
  fi
  echo ""
  echo -e "\033[1;33m=================================================================\033[0m"
  echo ${insMnu_welcome[1]}
  echo ${insMnu_welcome[2]}
  echo ${insMnu_welcome[3]}
  echo ${insMnu_welcome[4]}
  echo ${insMnu_welcome[5]}
  echo ${insMnu_welcome[6]}
  echo ${insMnu_welcome[7]}
  echo ${insMnu_welcome[8]}
  echo ${insMnu_welcome[9]}
  echo ${insMnu_welcome[10]}
  echo ${insMnu_welcome[11]}
  echo ${insMnu_welcome[12]}
  echo ${insMnu_welcome[13]}
  echo ${insMnu_welcome[14]}
  echo ${insMnu_welcome[15]}
  echo ${insMnu_welcome[16]}
  echo ${insMnu_welcome[17]}
  echo ${insMnu_welcome[18]}
  echo -n ${insMnu_welcome[19]}

  read insGen_mnuSel

  done
}

#---------------
#License Agreement Menu

mnuLicAgree()
{
  insGen_mnuSel=-1
  while [ "$insGen_mnuSel" != "$fc" ] && [ "$insGen_mnuSel" != "$bc" ]
  do

  clear
  echo -e "\033[1;44;33m================================================================="
  echo -en "|\033[1;36m ${insMnu_license[0]}"
  if [ ${#insMnu_license[@]} -lt 62 ]; then
    for (( i = ${#insMnu_license[0]}; i <= 62; i++ ))
    do
      echo -n " "
    done
  fi
  echo ""
  echo -e "\033[1;33m=================================================================\033[0m"
  echo ${insMnu_license[1]}
  echo ${insMnu_license[2]}
  echo ${insMnu_license[3]}
  echo ${insMnu_license[4]}
  echo ${insMnu_license[5]}
  echo ${insMnu_license[6]}
  echo ${insMnu_license[7]}
  echo ${insMnu_license[8]}
  echo ${insMnu_license[9]}
  echo ${insMnu_license[10]}
  echo ${insMnu_license[11]}
  echo ${insMnu_license[12]}
  echo ${insMnu_license[13]}
  echo ${insMnu_license[14]}
  echo ${insMnu_license[15]}
  if [ "$insLicAgree_accepted" = "true" ]; then
    echo ${insMnu_license[16]}
  else
    echo ${insMnu_license[20]}
  fi
  echo ${insMnu_license[17]}
  echo ${insMnu_license[18]}
  echo -n ${insMnu_license[19]}

  read insGen_mnuSel

  #handle user options
  case "$insGen_mnuSel" in
    "$fc") #only allow to continue if agreement is accepted
	if [ "$insLicAgree_accepted" != "true" ]; then
	  insGen_mnuSel=-1 #they didn't agree, so keep them in this menu
 	fi
	;;
    "1") #display the agreement for the user and return them to the menu when they're done
	cat $insGen_license | more
	;;
    "2") #change the 'accepted' variable to true and continue on
	insLicAgree_accepted="true"
	insGen_mnuSel="$fc"
	;;
    "3") #user declines the agreement, abort this script
	exit
	;;
  esac

  done
}

#---------------
#Choose Install Path  Menu

mnuChoosePath()
{
  insGen_mnuSel=-1
  while [ "$insGen_mnuSel" != "$fc" ] && [ "$insGen_mnuSel" != "$bc" ]
  do

  clear
  echo -e "\033[1;44;33m================================================================="
  echo -en "|\033[1;36m ${insMnu_installpath[0]}"
  if [ ${#insMnu_installpath[@]} -lt 62 ]; then
    for (( i = ${#insMnu_installpath[0]}; i <= 62; i++ ))
    do
      echo -n " "
    done
  fi
  echo ""
  echo -e "\033[1;33m=================================================================\033[0m"
  echo ${insMnu_installpath[1]}
  echo ${insMnu_installpath[2]}
  echo ${insMnu_installpath[3]}
  echo ${insMnu_installpath[4]}
  echo ${insMnu_installpath[5]}
  echo ${insMnu_installpath[6]}
  echo ${insMnu_installpath[7]}
  echo "${insMnu_installpath[8]} $insGen_installPath"
  echo ${insMnu_installpath[9]}
  echo ${insMnu_installpath[10]}
  echo ${insMnu_installpath[11]}
  echo ${insMnu_installpath[12]}
  echo ${insMnu_installpath[13]}
  echo ${insMnu_installpath[14]}
  echo ${insMnu_installpath[15]}
  echo ${insMnu_installpath[16]}
  echo ${insMnu_installpath[17]}
  echo ${insMnu_installpath[18]}
  echo -n ${insMnu_installpath[19]}

  read insGen_mnuSel

  #handle user options
  okay="false"
  
  if [ "$insGen_mnuSel" = "1" ]; then
    while [ "$okay" != "true" ]
    do
      old="$insGen_installPath"
      echo -n "${insMnu_trans[26]} " #enter an install path:
      read insGen_installPath
      
      #check for spaces in the install path
      testdir=`echo "$insGen_installPath" | cut -f -1 -d ' '`
      if [ "$testdir" != "$insGen_installPath" ]; then    #there are spaces in the install path
        echo "${insMnu_trans[24]}"  #there can not be spaces in the install path
      elif [ -d "$insGen_installPath" ]; then   #check for existance of install path
        echo -n "$insGen_installPath ${insMnu_trans[25]} [y/n]: " #$insGen_installPath already exists. Overwrite?
        read opt
        if [ "$opt" = "y" ] || [ "$opt" = "Y" ]; then
          okay="true"
        fi
      else
        okay="true"
      fi
    done
  fi

  done

}

#---------------
#Choose Install Type Menu

mnuChooseType()
{
  insGen_mnuSel=-1
  while [ "$insGen_mnuSel" != "$fc" ] && [ "$insGen_mnuSel" != "$bc" ]
  do

  clear
  echo -e "\033[1;44;33m================================================================="
  echo -en "|\033[1;36m ${insMnu_installtype[0]}"
  if [ ${#insMnu_installtype[@]} -lt 62 ]; then
    for (( i = ${#insMnu_installtype[0]}; i <= 62; i++ ))
    do
      echo -n " "
    done
  fi
  echo ""
  echo -e "\033[1;33m=================================================================\033[0m"
  echo ${insMnu_installtype[1]}
  echo ${insMnu_installtype[2]}
  echo ${insMnu_installtype[3]}
  echo ${insMnu_installtype[4]}
  echo ${insMnu_installtype[5]}
  echo ${insMnu_installtype[6]}
  
  if [ "$insGen_modeSuffix" = "c" ]; then    
    insGen_productType="central"    
    echo " [X] ${insMnu_installtype[8]}"
    echo ""
    echo ""
  else
    case "$insGen_modeSuffix" in
      "l")
      echo " [X] ${insMnu_installtype[7]}"
      echo " [ ] ${insMnu_installtype[9]}"
      echo ""
      ;;
      "a")
      echo " [ ] ${insMnu_installtype[7]}"
      echo " [X] ${insMnu_installtype[9]}"
      echo ""
      ;;
    esac
  fi 

  echo ${insMnu_installtype[10]}
  echo ${insMnu_installtype[11]}
  echo ${insMnu_installtype[12]}
  
  if [ "$insGen_modeSuffix" = "c" ]; then
    echo ""
    echo ""
    echo ""
  else
    echo ${insMnu_installtype[13]}
    echo ${insMnu_installtype[14]}
    echo ${insMnu_installtype[15]}
  fi
    
  echo ${insMnu_installtype[16]}
  echo ${insMnu_installtype[17]}
  echo ${insMnu_installtype[18]}
  echo -n ${insMnu_installtype[19]}

  read insGen_mnuSel

  #handle user options
  if [ "$insGen_modeSuffix" != "c" ]; then
    case "$insGen_mnuSel" in
      "1")
        insGen_productType="local"
        insGen_modeSuffix="l"
        ;;
      "2")
  	    insGen_productType="agent"
  	    insGen_modeSuffix="a"
        ;;
    esac
  fi


  done

}

#---------------
#Choose RMI Port Menu

mnuChooseRMI()
{
  insGen_mnuSel=-1
  while [ "$insGen_mnuSel" != "$fc" ] && [ "$insGen_mnuSel" != "$bc" ]
  do

  clear
  echo -e "\033[1;44;33m================================================================="
  echo -en "|\033[1;36m ${insMnu_rmiport[0]}"
  if [ ${#insMnu_rmiport[@]} -lt 62 ]; then
    for (( i = ${#insMnu_rmiport[0]}; i <= 62; i++ ))
    do
      echo -n " "
    done
  fi
  echo ""
  echo -e "\033[1;33m=================================================================\033[0m"
  echo ${insMnu_rmiport[1]}
  echo ${insMnu_rmiport[2]}
  echo ${insMnu_rmiport[3]}
  echo ${insMnu_rmiport[4]}
  echo ${insMnu_rmiport[5]}
  echo "${insMnu_rmiport[6]} $insGen_RMIport"
  echo ${insMnu_rmiport[7]}
  echo ${insMnu_rmiport[8]}
  echo ${insMnu_rmiport[9]}
  echo ${insMnu_rmiport[10]}
  echo ${insMnu_rmiport[11]}
  echo ${insMnu_rmiport[12]}
  echo ${insMnu_rmiport[13]}
  echo ${insMnu_rmiport[14]}
  echo ${insMnu_rmiport[15]}
  echo ${insMnu_rmiport[16]}
  echo ${insMnu_rmiport[17]}
  echo ${insMnu_rmiport[18]}
  echo -n ${insMnu_rmiport[19]}

  read insGen_mnuSel

  #handle user options
  if [ "$insGen_mnuSel" = "1" ]; then
    echo -n "${insMnu_trans[8]} " #enter a new port number: 
    read insGen_RMIport
  fi


  done

}

#---------------
#Choose SSL Menu

mnuChooseSSL()
{
  insGen_mnuSel=-1
  while [ "$insGen_mnuSel" != "$fc" ] && [ "$insGen_mnuSel" != "$bc" ]
  do

  clear
  echo -e "\033[1;44;33m================================================================="
  echo -en "|\033[1;36m ${insMnu_ssl[0]}"
  if [ ${#insMnu_ssl[@]} -lt 62 ]; then
    for (( i = ${#insMnu_ssl[0]}; i <= 62; i++ ))
    do
      echo -n " "
    done
  fi
  echo ""
  echo -e "\033[1;33m=================================================================\033[0m"
  echo ${insMnu_ssl[1]}
  echo ${insMnu_ssl[2]}
  echo ${insMnu_ssl[3]}
  echo ${insMnu_ssl[4]}
  echo ${insMnu_ssl[5]}
  echo ${insMnu_ssl[6]}
  if [ "$insGen_SSL" = "false" ]; then
    echo "   [ ]${insMnu_ssl[7]}"
    echo "   [X]${insMnu_ssl[8]}"
  else
    echo "   [X]${insMnu_ssl[7]}"
    echo "   [ ]${insMnu_ssl[8]}"
  fi
  echo ${insMnu_ssl[9]}
  echo ${insMnu_ssl[10]}
  echo ${insMnu_ssl[11]}
  echo ${insMnu_ssl[12]}
  echo ${insMnu_ssl[13]}
  echo ${insMnu_ssl[14]}
  echo ${insMnu_ssl[15]}
  echo ${insMnu_ssl[16]}
  echo ${insMnu_ssl[17]}
  echo ${insMnu_ssl[18]}
  echo -n ${insMnu_ssl[19]}

  read insGen_mnuSel

  #handle user input
  if [ "$insGen_mnuSel" = "1" ]; then
    insGen_SSL="true"
  fi
  if [ "$insGen_mnuSel" = "2" ]; then
    insGen_SSL="false"
  fi

  done

}

#---------------
#License Agreement Menu

mnuSummary()
{
  insGen_mnuSel=-1
  while [ "$insGen_mnuSel" != "$fc" ] && [ "$insGen_mnuSel" != "$bc" ]
  do

  clear
  echo -e "\033[1;44;33m================================================================="
  echo -en "|\033[1;36m ${insMnu_summary[0]}"
  if [ ${#insMnu_summary[@]} -lt 62 ]; then
    for (( i = ${#insMnu_summary[0]}; i <= 62; i++ ))
    do
      echo -n " "
    done
  fi
  echo ""
  echo -e "\033[1;33m=================================================================\033[0m"
  echo ${insMnu_summary[1]}
  echo ${insMnu_summary[2]}
  echo ${insMnu_summary[3]}
  echo ${insMnu_summary[4]}
  echo ${insMnu_summary[5]}
  echo ${insMnu_summary[6]}
  echo ${insMnu_summary[7]}
  echo ${insMnu_summary[8]}
  echo ${insMnu_summary[9]}
  echo ${insMnu_summary[10]}
  echo ${insMnu_summary[11]}
  echo ${insMnu_summary[12]}
  echo ${insMnu_summary[13]}
  echo ${insMnu_summary[14]}
  echo ${insMnu_summary[15]}
  echo ${insMnu_summary[16]}
  echo ${insMnu_summary[17]}
  echo ${insMnu_summary[18]}
  echo -n ${insMnu_summary[19]}

  read insGen_mnuSel

  done

}

#---------------


#########################################
####General Functions

#---------------
#Load Locale
#
#populates the arrays used to display the menus in the selected
#locale.  the menu text is stored in the 'language' folder.  one
#folder exists for each menu, and each contains the various menu
#texts in their respective languages.
#
# $1 is the locale string (i.e., 'en' 'pt_BR' 'de', etc)
#

load_locale()
{
  #load license agreement for chosen language
  insGen_license="$insGen_tempdir/lic/license-$1.txt"
  	
  #misc installer translations
    x=0
    file="$insGen_tempdir/language/misc_install/$1.mnu"
    
    exec 3<&0
    exec 0<$file
    while read line
    do
      insMnu_trans[x]="$line"    
      x=`expr $x + 1`
    done
    exec 0<&3 3<&-
  
  #welcome menu
    x=0
    file="$insGen_tempdir/language/mnu_welcome/$1.mnu"
  
    #replace tokens in language files    
    replace_tok "@@@prod_name@@@" "$insGen_prodName" $file
    
    exec 3<&0     #save stdin to filedescriptor '3'
    exec 0<$file  #$file becomes stdin
    while read line
    do
      insMnu_welcome[x]="$line"
      x=`expr $x + 1`
    done
    exec 0<&3 3<&-  #return to normal stdin and free filedescriptor '3'
    
  #license agreement menu
    x=0
    file="$insGen_tempdir/language/mnu_license/$1.mnu"
    
    exec 3<&0
    exec 0<$file
    while read line
    do
      insMnu_license[x]="$line"    
      x=`expr $x + 1`
    done
    exec 0<&3 3<&-
        
  #choose install path menu
    x=0
    file="$insGen_tempdir/language/mnu_installpath/$1.mnu"
    
    exec 3<&0
    exec 0<$file
    while read line
    do
      insMnu_installpath[x]="$line"     
      x=`expr $x + 1`
    done
    exec 0<&3 3<&- 
    
   #choose install type menu
    x=0
    file="$insGen_tempdir/language/mnu_installtype/$1.mnu"
    
    exec 3<&0
    exec 0<$file
    while read line
    do
      insMnu_installtype[x]="$line"      
      x=`expr $x + 1`
    done
    exec 0<&3 3<&-
        
   #choose RMI port menu
    x=0
    file="$insGen_tempdir/language/mnu_rmiport/$1.mnu"
    
    exec 3<&0
    exec 0<$file
    while read line
    do
      insMnu_rmiport[x]="$line"      
      x=`expr $x + 1`
    done
    exec 0<&3 3<&-
    
   #SSL menu
    x=0
    file="$insGen_tempdir/language/mnu_ssl/$1.mnu"
    
    exec 3<&0
    exec 0<$file
    while read line
    do
      insMnu_ssl[x]="$line"      
      x=`expr $x + 1`
    done
    exec 0<&3 3<&-
    
   #summary menu
    x=0
    file="$insGen_tempdir/language/mnu_summary/$1.mnu"
    
    exec 3<&0
    exec 0<$file
    while read line
    do
      insMnu_summary[x]="$line"      
      x=`expr $x + 1`
    done
    exec 0<&3 3<&-
}


#---------------
#Menu Flow Control
#
#this menu controls the flow of menus.  it is passed two variables,
#one being the name of the menu that was just visited, the other
#being the nav option (back or next) the user selected at that menu.
#it sets global variable 'insGen_nextFunc' to the name of the next
#menu that should get displayed (called from the main routine)
#

menu_flow_ctrl()
{
  insGen_mnuPrev=$1  #menu this was the previous menu
  insGen_mnuSel=$2   #the menu choice of that menu

  #echo $insGen_mnuPrev
  #echo $insGen_mnuSel

  case $insGen_mnuPrev in
    "start")
        insGen_nextFunc="mnuLocale"
        ;;
    "mnuLocale")
        if [ "$insGen_mnuSel" = "$fc" ]; then
          insGen_nextFunc="mnuWelcome"
        fi
        ;;
    "mnuWelcome")
        if [ "$insGen_mnuSel" = "$bc" ]; then
          insGen_nextFunc="mnuLocale"
        elif [ "$insGen_mnuSel" = "$fc" ]; then
          insGen_nextFunc="mnuLicAgree"
        fi
        ;;
    "mnuLicAgree")
        if [ "$insGen_mnuSel" = "$bc" ]; then
          insGen_nextFunc="mnuWelcome"
        elif [ "$insGen_mnuSel" = "$fc" ]; then
          insGen_nextFunc="mnuChoosePath"
        fi
        ;;
    "mnuChoosePath")
        if [ "$insGen_mnuSel" = "$bc" ]
        then
          insGen_nextFunc="mnuLicAgree"
        fi
        if [ "$insGen_mnuSel" = "$fc" ]
        then
          insGen_nextFunc="mnuChooseType"
        fi
        ;;
    "mnuChooseType")
        if [ "$insGen_mnuSel" = "$bc" ]
        then
          insGen_nextFunc="mnuChoosePath"
        fi
        if [ "$insGen_mnuSel" = "$fc" ]
        then
    	  if [ "$insGen_productType" = "central" ]; then  #the next menu is 'Choose RMI' only if this is a 'central' install
                insGen_nextFunc="mnuChooseRMI"
    	  else
    	  	if [ "$insgen_modeSuffix" = "a" ]; then	#if this is an agent install don't ask about SSL
    	  		insGen_SSL="false"
    	    	insGen_nextFunc="mnuSummary"
    	    else
    	    	insGen_nextFunc="mnuChooseSSL"
    	    fi
     	  fi
        fi
        ;;
    "mnuChooseRMI")
        if [ "$insGen_mnuSel" = "$bc" ]
        then
          insGen_nextFunc="mnuChooseType"
        fi
        if [ "$insGen_mnuSel" = "$fc" ]
        then
          insGen_nextFunc="mnuChooseSSL"
        fi
        ;;
    "mnuCollectInfo")
        if [ "$insGen_mnuSel" = "$bc" ]
        then
    	  if [ "$insGen_productType" = "central" ]; then  #return to 'Choose RMI' only if this is a 'central' install
                insGen_nextFunc="mnuChooseRMI"
    	  else
    	    insGen_nextFunc="mnuChooseType"
    	  fi
        fi
        if [ "$insGen_mnuSel" = "$fc" ]
        then
          insGen_nextFunc="mnuChooseSSL"
        fi
        ;;
    "mnuChooseSSL")
        if [ "$insGen_mnuSel" = "$bc" ]
        then
          insGen_nextFunc="mnuChooseRMI"
        fi
        if [ "$insGen_mnuSel" = "$fc" ]
        then
          insGen_nextFunc="mnuSummary"
        fi
        ;;
    "mnuSummary")
        if [ "$insGen_mnuSel" = "$bc" ]
        then
          insGen_nextFunc="mnuChooseSSL"
        fi
      	if [ "$insGen_mnuSel" = "$fc" ]
      	then
      	  insGen_nextFunc="install"
      	fi
        ;;
  esac

}

#---------------
#Error Exit Function
#
#this function exits with a specified error.  is it used to standardize
#the way the script exits when a required command was not successful
#

errexit()
{
  echo -e "\033[1;31m[${insTrans_def[2]}]\033[0m" #error
  exit
}


#---------------
#Probe for File/Folder
#
#checks for the existance of a file or folder
#

probe()
{
  if [ -e "$1" ]; then
    insGen_probe="true"
  else
    insGen_probe="false"
  fi
}

#---------------
#Replace Token
#
#replaces a token in a file with a variable.  this is used
#mostly for adding the install directory into config files
#

replace_tok()
{
	tok=$1	#token to replace
	var=$2	#value to replace it with
	file=$3	#file to do it to	
	
	rm -f tempqpazf > /dev/null 2> /dev/null
	sed s%"$tok"%"$var"% $file > /dev/null  >> templqpazf && mv -f templqpazf $file

}

#---------------
#unixEOL
#
#converts a DOS file with CR/LF EOL characters to UNIX
#EOL characters
#

unix_EOL()
{

  #convert from the DOS CR/LF file that sed apparently makes by default
  sed s%.$%% $1 > /dev/null >> templqpazf && mv -f templqpazf $1

}

#---------------
#Install Java
#
#installs java to the system
#

install_java()
{
  #install java to 'installpath/java'
  echo -n "${insMnu_trans[9]} "  #installing java (will not affect your system java)... 
  mv -f $insGen_tempdir/java $insGen_installPath
  echo -e "\033[1;32m${insMnu_trans[0]}\033[0m"

  #update java.security with correct .login file location
  replace_tok "@@@java_home@@@" "$insGen_installPath/java" "$insGen_installPath/java/lib/security/java.security"

  chmod 777 $insGen_installPath/java/bin/*

  #echo -e "\033[1;34m"
  #echo "--- ${insMnu_trans[10]} ---" #--- java installed and set up ---
  #echo -e "\033[0m"

}

#---------------
#Add Service
#
#adds a service to the system's default runlevels
#

add_service()
{
	serv=$1
	addsrv_err=0
	
	#chkconfig is the redhat/suse way of adding runlevels
	chkconfig --add $serv > /dev/null 2> /dev/null
  if [ "$?" != "0" ]; then #simply calling the command didn't work...
  	if [ -x "/sbin/chkconfig" ]; then	#...check for non-path existance of chkconfig
	    /sbin/chkconfig --add $serv > /dev/null 2> /dev/null
	    if [ "$?" != "0" ]; then
				addsrv_err=1
	    fi
	  else
	  	addsrv_err=1
	  fi
	fi
	
	#if chkconfig worked, break out of this function
	if [ $addsrv_err == 0 ]; then
		return
	fi
	addsrv_err=0	#reset error check
	
	#rc-update is the gentoo way of adding runlevels  
	rc-update add $serv default > /dev/null 2> /dev/null
	if [ "$?" != "0" ]; then
		if [ -x "/sbin/rc-update" ]; then #check for rc-update if it wasn't in the system path
			/sbin/rc-update add $serv default > /dev/null 2> /dev/null
			if [ "$?" != "0" ]; then
				addsrv_err=1
	    fi
	  else
	  	addsrv_err=1
	  fi
	fi
	
	#if rc-update worked, break out of this function
	if [ $addsrv_err == 0 ]; then
		return
	fi
	addsrv_err=0	#reset error check

	#update-rc.d is the debian/ubuntu way of adding runlevels
	update-rc.d -f $serv defaults  > /dev/null 2> /dev/null
	if [ "$?" != "0" ]; then
		echo "initial problem"
		if [ -x "/sbin/update-rc.d" ]; then #check for rc-update if it wasn't in the system path
			/sbin/update-rc.d -f $serv defaults  > /dev/null 2> /dev/null
			if [ "$?" != "0" ]; then
				addsrv_err=1
	    fi
	  else
	  	addsrv_err=1
	  fi
	fi
	    
	#if all methods were tried and none worked, exit with failure
	if [ $addsrv_err == 1 ]; then
		errexit
	fi

}

#---------------
#Install Agent
#
#installs the agent to the system
#

install_agent()
{
  #install the agent
  echo -n "${insMnu_trans[11]} " #installing the agent...
  cp -rf $insGen_tempdir/system_monitor $insGen_installPath
  cp -f $insGen_tempdir/SupportDir/etc/init.d/smad $insGen_installPath
  cp -f $insGen_tempdir/SupportDir/etc/init.d/smad_remote_console $insGen_initd
  cp -f $insGen_tempdir/SupportDir/$insGen_locale/* $insGen_installPath/system_monitor/remote_console/class
  cp -f $insGen_tempdir/SupportDir/vnc/scripts/vncpasswd $insGen_installPath/system_monitor/remote_console
  cp -rf $insGen_tempdir/SupportDir/vnc/bin/* $insGen_installPath/system_monitor/remote_console
  echo -e "\033[1;32m${insMnu_trans[0]}\033[0m"

  #if this is not a central install, remove SMC files from system_monitor
  if [ "$1" = "basic" ]; then
  #  rm -f $insGen_installPath/system_monitor/alerts.properties
    rm -f $insGen_installPath/system_monitor/event_log.log
  #  rm -f $insGen_installPath/system_monitor/lib/syam.jar
    rm -rf $insGen_installPath/system_monitor/conf
    rm -rf $insGen_installPath/system_monitor/bin
  fi
  
  #edit agent.properties
  replace_tok "locale=en" "locale=$insGen_locale" "$insGen_installPath/system_monitor/agent/agent.properties"

  #add install path to smad script
  replace_tok "INSTALLDIR=" "INSTALLDIR=\"$insGen_installPath/system_monitor/agent\"" "$insGen_installPath/smad"
  #unix_EOL "$insGen_installPath/smad"
  
  #set the correct product mode in smad
  replace_tok "MODE=SMA" "MODE=$insGen_modePrefix$insGen_modeSuffix" "$insGen_installPath/smad"
  
  replace_tok "@@@install_path@@@" "\"$insGen_installPath\";" "$insGen_installPath/system_monitor/remote_console/vncserver"
  replace_tok "@@@install_path@@@" "$insGen_installPath" "$insGen_initd/smad_remote_console"

  #change file permissions
  chmod 777 $insGen_installPath/smad
  chmod 777 $insGen_installPath/system_monitor/agent/smad    
  chmod 777 $insGen_initd/smad_remote_console
  chmod 777 $insGen_installPath/system_monitor/remote_console/*
  chmod 600 $insGen_installPath/system_monitor/remote_console/.vnc/passwd
  chmod 777 $insGen_installPath/system_monitor/agent/aaccli > /dev/null 2> /dev/null
  
  chmod 755 $insGen_installPath/system_monitor/agent/*.so* > /dev/null 2> /dev/null
  
  chmod 777 $insGen_installPath/system_monitor/remote_console/.vnc/* > /dev/null 2> /dev/null
  chmod 600 $insGen_installPath/system_monitor/remote_console/.vnc/passwd > /dev/null 2> /dev/null
  
  #create symbolic links in init.d for smad
  ln -fs $insGen_installPath/smad $insGen_initd

  #register the service with the system
  echo -n "${insMnu_trans[12]} " #registering agent smad with the system...

  add_service "smad"
  add_service "smad_remote_console"
  
  echo -e "\033[1;32m${insMnu_trans[0]}\033[0m"

  #start the agent
  $insGen_initd/smad start

  #echo -e "\033[1;34m"
  #echo "--- ${insMnu_trans[13]} ---" #--- agent installed and set up ---
  #echo -e "\033[0m"

}

#---------------
#Install Jetty
#
#installs jetty to the system
#

install_jetty()
{
  #install jetty web server
  echo -n "${insMnu_trans[14]} " #installing the web server...

  cp -rf $insGen_tempdir/jetty $insGen_installPath
  cp -f $insGen_tempdir/SupportDir/etc/pam.d/syam $insGen_pamd  #used for login

  echo -e "\033[1;32m${insMnu_trans[0]}\033[0m"

  #convert DOS EOLs to UNIX EOLs
  #unix_EOL $insGen_pamd/syam
  #unix_EOL $insGen_installPath/jetty/bin/findPamLib
  #unix_EOL $insGen_installPath/jetty/bin/smwd
  
  #determine if the 32 or 64 bit syamlogin should be used, and remove the other
  probe "/lib64"
  if [ "$insGen_probe" = "true" ]; then
    mv "$insGen_installPath/jetty/bin/syamlogin64" "$insGen_installPath/jetty/bin/syamlogin" > /dev/null 2> /dev/null
    rm -rf "$insGen_installPath/jetty/bin/syamlogin32"
  fi
  probe "/usr/lib64"
  if [ "$insGen_probe" = "true" ]; then
    mv "$insGen_installPath/jetty/bin/syamlogin64" "$insGen_installPath/jetty/bin/syamlogin" > /dev/null 2> /dev/null
    rm -rf "$insGen_installPath/jetty/bin/syamlogin32"
  fi
  mv "$insGen_installPath/jetty/bin/syamlogin32" "$insGen_installPath/jetty/bin/syamlogin" > /dev/null 2> /dev/null
  rm -rf "$insGen_installPath/jetty/bin/syamlogin64"


  #remove smc_fr.jsp (leave only sml_fr.jsp)
  rm -f $insGen_installPath/jetty/syam/webapps/root/smc_fr.jsp

  #change file permissions
  chmod 777 $insGen_installPath/jetty/wrapper > /dev/null 2> /dev/null
  #chmod 777 $insGen_installPath/jetty/bin/findPamLib
  chmod 777 $insGen_installPath/jetty/bin/smwd > /dev/null 2> /dev/null
  chmod 777 $insGen_installPath/jetty/bin/syamlogin > /dev/null 2> /dev/null
  chmod +x $insGen_installPath/jetty/bin/syamimr > /dev/null 2> /dev/null

  #use findPamLib to find the correct PAM library and update smwd
  #$insGen_installPath/jetty/bin/findPamLib $insGen_installPath/jetty/bin/smwd

  #edit syam.properties
  replace_tok "mode=sml" "mode=$insGen_modePrefix$insGen_modeSuffix" "$insGen_installPath/jetty/syam/webapps/root/WEB-INF/syam.properties"
  replace_tok "locale=en" "locale=$insGen_locale" "$insGen_installPath/jetty/syam/webapps/root/WEB-INF/syam.properties"
  
  #enable SSL if selected
  if [ "$insGen_SSL" = "true" ]; then
    #generate keystore
    replace_tok "@@@install_path@@@" "$insGen_installPath" "$insGen_installPath/jetty/bin/ssl_config"
    chmod 777 $insGen_installPath/jetty/bin/ssl_config
    $insGen_installPath/jetty/bin/ssl_config localhost 123456 123456
    
    #modify syam.xml to comment the http listener and uncomment the SSL listener
    replace_tok "<!--@@@http\ -->" "<!--@@@http" "$insGen_installPath/jetty/etc/syam.xml"
    replace_tok "<!--\ @@@http\ -->" "@@@http\ -->" "$insGen_installPath/jetty/etc/syam.xml"
    replace_tok "@@@ssl\ -->" "<!--\ @@@ssl\ -->" "$insGen_installPath/jetty/etc/syam.xml"
    replace_tok "<!--@@@ssl" "<!--@@@ssl\ -->" "$insGen_installPath/jetty/etc/syam.xml"
      
  fi
  
  #symlink smwd to the system init.d
  ln -fs $insGen_installPath/jetty/bin/smwd $insGen_initd

  #register smwd with the system and start it
  echo -n "${insMnu_trans[15]} " #registering webserver smwd with the system...

	add_service "smwd"

  echo -e "\033[1;32m${insMnu_trans[0]}\033[0m"

  #start jetty
  $insGen_initd/smwd start
  echo ""
  
  #echo -e "\033[1;34m"
  #echo "--- ${insMnu_trans[16]} ---" #--- web server installed and set up ---
  #echo -e "\033[0m"

}

#---------------
#Install SMC
#
#installs the SMC to the system.  since most files are installed along
#with the agent, this really just adds smcd as a service and starts it
#

install_SMC()
{
  echo -n "${insMnu_trans[17]} " #installing smc...
  echo -e "\033[1;32m${insMnu_trans[0]}\033[0m"

  #replace DOS EOLs with UNIX EOLs
  #unix_EOL "$insGen_installPath/system_monitor/smcd"

  #remove sml_fr.jsp and move smc_fr.jsp to the correct folder
  rm -f $insGen_installPath/jetty/syam/webapps/root/sml_fr.jsp
  cp -f $insGen_tempdir/jetty/syam/webapps/root/smc_fr.jsp $insGen_installPath/jetty/syam/webapps/root

  #edit syam.properties  
  replace_tok "rmi_port=1099" "rmi_port=$insGen_RMIport" "$insGen_installPath/jetty/syam/webapps/root/WEB-INF/syam.properties"     
  
  #change file permissions
  chmod 777 $insGen_installPath/system_monitor/smcd

  #symlink smcd to the system init.d
  ln -fs $insGen_installPath/system_monitor/smcd $insGen_initd

  #register smcd with the system and start it
  echo -n "${insMnu_trans[18]} " #registering SMC smcd with the system...

  add_service "smcd"

  echo -e "\033[1;32m${insMnu_trans[0]}\033[0m"

  #start SMC
  $insGen_initd/smcd start  

  #echo -e "\033[1;34m"
  #echo "--- ${insMnu_trans[19]} ---" #--- SMC installed and set up ---
  #echo -e "\033[0m"

}

#---------------
#Pre-Install Routine
#
#handles some basic things like temp directory setup,
#some directory probing and package exctraction
#

preinstall()
{
  ##probe for system folders (i.e., which folder acts as /etc/init.d)
  #probe for 'init.d' (check some known possibilities)
  probe "/etc/init.d"
  if [ "$insGen_probe" = "true" ]; then
    insGen_initd="/etc/init.d"
  else
    errexit
  fi

  #probe for 'pam.d'
  probe "/etc/pam.d"
  if [ "$insGen_probe" = "true" ]; then
    insGen_pamd="/etc/pam.d"
  else
    errexit
  fi

	
  #check to see if the product exists... if so, check for upgrade possibility
  ans=0
  insGen_installerr="false"

  echo "${insTrans_def[1]}" #checking for previous versions...

  if [ -e "$insGen_initd/smad" ]; then	
   	
  	#find the path of the current install
  	ret=`ls -l "$insGen_initd/smad" | awk '{print $11}'` #find out where the init.d/smad symlink references
  	
  	if [ "$?" = "0" ]; then
    	insGen_currInstall=`echo $ret | sed -e 's%/smad$%%'` #cut off the "/smad" part of the path to get the dir the product is currently in
    	
    	#we now know where the current install is located
    	
    	#verify that the install type is the same.. e.g., make sure they're both central, desktop, etc
    	currProd=`cat $insGen_initd/smad | grep MODE=`
    	currType=`expr substr $currProd 6 2`
    	currMode=`expr substr $currProd 8 1`
    	
    	if [ "$insGen_modePrefix" != "$currType" ]; then
				echo "${insTrans_def[13]}"
				errexit
    	elif [ "$insGen_modeSuffix" = "c" ]; then
    		if [ "$currMode" != "c" ]; then
					echo "${insTrans_def[14]}"
					errexit
				fi
    	elif [ "$insGen_modeSuffix" != "c" ]; then
    		if [ "$currMode" = "c" ]; then
					echo "${insTrans_def[15]}"
					errexit
				fi
    	fi
    	
    	#determine which version it is  		
  		if [ -e "$insGen_currInstall/version" ]; then
  			ver=`cat $insGen_currInstall/version | grep ".[.].."` > /dev/null 2> /dev/null
  			
  			#remove the '.' character from version numbers for integer comparison
  			verThis=${insGen_thisVersion/./}
  			verCurr=${ver/./}
  			
  			if [ $verThis -le $verCurr ]; then #if this version is less than or equal to the one currently installed, we can't upgrade
  				echo "${insTrans_def[7]}" #a previous installation was detected.  please uninstall before reinstalling.
  				exit
  			elif [ $verCurr -ge 310 ]; then #only upgrade from version at or bove 3.10 (see bug 1346)
  				echo "${insTrans_def[8]}" #A previous installation was detected.  Would you like to upgrade [1:yes 2:no]?
	    		read ans
	    		if [ "$ans" != "1" ]; then
	    			echo "${insTrans_def[10]}"	#install aborted
						exit
					fi
				else
					echo "${insTrans_def[7]}" #a previous installation was detected.  please uninstall before reinstalling.
  				exit
  			fi
  			
  		else #version file not there... this is likely a 3.00 version
  		
  			echo "${insTrans_def[7]}" #a previous installation was detected.  please uninstall before reinstalling.
  			exit
				
  		fi
  		
    else
    	echo "${insTrans_def[11]}"	#warning: previous installation detected but the folder was not found
    	echo "${insTrans_def[12]}"	#continuing with full install..."
  	fi
  fi
  
  #establish a temporary directory
  rm -rf $insGen_tempdir  #remove temp folder if it's there
  mkdir -p $insGen_tempdir
  if [ "$?" != "0" ]; then
    echo $insErr_tempdir
    errexit
  fi

  #extract (untar) install files to temp folder
  echo -n "${insTrans_def[6]} "  #extracting files to temp folder (please be patient)...
  tar -xf $insGen_insPackage -C $insGen_tempdir > /dev/null 2> /dev/null
  if [ "$?" != "0" ]; then
    echo $insErr_untar
    errexit
  fi
  echo -e "\033[1;32m${insMnu_trans[0]}\033[0m"
  
  #do we perform upgrade actions?
  if [ "$ans" = "1" ]; then
  	insGen_upgrade="true"
  	upgrade_install #if yes launch upgrade_install
  fi
}


#---------------
#Upgrade Install Routine
#
#performs the 'upgrade' part of the update install.  after saving some 
#files and performing other upgrade-related tasts, the regular install 
#continues as normal.
#
upgrade_install()
{			
	#find out about the current install
	upgrade_find_options
	
	#load_locale
	load_locale $insGen_locale
	
	#move files that need to be saved to the temp folder
	upgrade_backup
	
	#uninstall current install
	echo 1 | "$insGen_currInstall/uninstall" > /dev/null 2> /dev/null
	
	#install the product without the menu system
	install
}

#---------------
#Upgrade- Backup
#
#this backs up all the files that need to be backed up.  it places
#all the files that need to be moved over into the temp folder. 
#they are then automatically moved from the temp folder to the install
#folder when the install occursd
#this way the install naturally restores them to the correct spot.
#
upgrade_backup()
{	
	#backup /system_monitor files
	cp -f $insGen_currInstall/system_monitor/alerts.properties $insGen_tempdir/system_monitor > /dev/null 2> /dev/null 
	cp -f $insGen_currInstall/system_monitor/*.log $insGen_tempdir/system_monitor > /dev/null 2> /dev/null
	cp -f $insGen_currInstall/system_monitor/managed_systems.dat $insGen_tempdir/system_monitor > /dev/null 2> /dev/null
	
	#backup /system_monitor/agent files
	cp -f $insGen_currInstall/system_monitor/agent/.ezl20ck $insGen_tempdir/system_monitor/agent > /dev/null 2> /dev/null
	cp -f $insGen_currInstall/system_monitor/agent/si.dat $insGen_tempdir/system_monitor/agent > /dev/null 2> /dev/null
	cp -f $insGen_currInstall/system_monitor/agent/rcd.dat $insGen_tempdir/system_monitor/agent > /dev/null 2> /dev/null
	cp -f $insGen_currInstall/system_monitor/agent/ntarg.dat $insGen_tempdir/system_monitor/agent > /dev/null 2> /dev/null
	cp -f $insGen_currInstall/system_monitor/agent/ctx.dat $insGen_tempdir/system_monitor/agent > /dev/null 2> /dev/null
	cp -f $insGen_currInstall/system_monitor/agent/asset.dat $insGen_tempdir/system_monitor/agent > /dev/null 2> /dev/null
	cp -f $insGen_currInstall/system_monitor/agent/agt.dat $insGen_tempdir/system_monitor/agent > /dev/null 2> /dev/null
	cp -f $insGen_currInstall/system_monitor/agent/*.log $insGen_tempdir/system_monitor/agent > /dev/null 2> /dev/null

	#backup jetty/logs	
	cp -f $insGen_currInstall/jetty/logs/*.log $insGen_tempdir/jetty/logs > /dev/null 2> /dev/null
}

#---------------
#Upgrade- Find Menu Option
#
#the normal installation gets many options from the user. this function
#looks at the current install to populate those values.  
#
upgrade_find_options()
{
	
	#get the locale
	insGen_locale=`cat "$insGen_currInstall/system_monitor/agent/agent.properties" | sed -e 's%locale=%%'`
	
	if [ -e "$insGen_currInstall/system_monitor/agent/libezlicenser20.so" ]; then
		insGen_productType="central"
		insGen_modeSuffix="c"
		
		#get the current RMI port info
		insGen_RMIport=`cat "$insGen_currInstall/jetty/syam/webapps/root/WEB-INF/syam.properties" | sed -e '1d' -e '2d' -e'4d' -e 's%rmi_port=%%'`
		
		#get SSL information
		ssl=`cat "$insGen_currInstall/jetty/etc/syam.xml" | sed -n '/<!--@@@ssl -->/p'` > /dev/null 2> /dev/null
		if [ ${#ssl} -gt 1 ]; then #this means that SSL was enabled
			insGen_SSL="true"
		else #SSL was not enabled
			insGen_SSL="false"
		fi
		
	else
		if [ -d "$insGen_currInstall/jetty" ]; then
		
			insGen_productType="local"
			insGen_modeSuffix="l"
			
			#get SSL information
			ssl=`cat "$insGen_currInstall/jetty/etc/syam.xml" | sed -n '/<!--@@@ssl -->/p'` > /dev/null 2> /dev/null
			if [ ${#ssl} -gt 1 ]; then #this means that SSL was enabled
				insGen_SSL="true"
			else #SSL was not enabled
				insGen_SSL="false"
			fi
			
		else
			insGen_productType="agent"
			insGen_modeSuffix="a"			
		fi
	fi
	
	#set the install path to the current install path
	insGen_installPath="$insGen_currInstall"
}

#---------------
#Install Routine
#
#this function performs the installation.  it relies on other functions
#to perform the many actions required by the install.
#

install()
{
  clear

  #create the install directory
  rm -rf "$insGen_installPath" > /dev/null 2> /dev/null
  mkdir -p "$insGen_installPath" > /dev/null 2> /dev/null
  if [ "$?" != "0" ]; then	#couldn't make the directory
    echo $insErr_installPath
    errexit
  fi

  #install components
  case "$insGen_productType" in
    "local")
	install_java
	install_agent "basic"
	install_jetty
    ;;
    "central")
	install_java
	install_agent "central"
	install_jetty
	install_SMC
    ;;
    "agent")
	install_java
	install_agent "basic"
    ;;
  esac
  
  #install documentation
  cp -rf $insGen_tempdir/docs $insGen_installPath
  
  #install license
  cp $insGen_tempdir/lic/license-$insGen_locale.txt $insGen_installPath/license

  #create a version file
  echo "$insGen_thisVersion" > "$insGen_installPath/version"
  echo "prod=$insGen_modePrefix$insGen_modeSuffix" >> "$insGen_installPath/version"
  echo "locale=$insGen_locale" >> "$insGen_installPath/version"
  echo "rmi=$insGen_RMIport" >> "$insGen_installPath/version"
  echo "ssl=$insGen_SSL" >> "$insGen_installPath/version"

  #prepare the uninstaller
  echo -n "${insMnu_trans[20]} " #Preparing the uninstaller... 
  cp $insGen_tempdir/uninstall $insGen_installPath
  #unix_EOL "$insGen_installPath/uninstall"
  replace_tok "@@@install_path@@@" "$insGen_installPath" $insGen_installPath/uninstall
  replace_tok "@@@locale@@@" "$insGen_locale" $insGen_installPath/uninstall
  chmod 777 $insGen_installPath/uninstall
  echo -e "\033[1;32m${insMnu_trans[0]}\033[0m"

  #delete temp folder
  echo -n "${insMnu_trans[21]} " #Cleaning up temp files...
  rm -rf $insGen_tempdir
  echo -e "\033[1;32m${insMnu_trans[0]}\033[0m"

  echo -e "\033[1;34m"
  echo "--- ${insMnu_trans[22]} ---" #--- installation complete ---
  echo -e "\033[0m"

}

#---------------

#########################################
#####Variables

#---------------
#error messages

insErr_mustUninstall="${insTrans_def[0]}" #You must uninstall the product before you can install it again
insErr_installPath="${insTrans_def[5]} "  #Could not create the install path... make sure you have full permissions 
insErr_tempdir="${insTrans_def[3]}"		  #Could not create temp folder... make sure you have full permissions to $insGen_tempdir
insErr_untar="${insTrans_def[4]}"		  #Could not untar the install package

#---------------
#general / global variables

fc="9"
bc="8"

insGen_locale="en"

insGen_insPackage="install_package.tar"
insGen_tempdir="./temp"
insGen_startdir=`pwd`

insGen_mnuSel=6
insGen_prodName="SyAM Server Monitor 3.22"
insGen_nextFunc="start"
insGen_license="$insGen_tempdir/lic/license-$insGen_locale.txt"	#load license agreement for default language

insGen_initd=""
insGen_pamd=""

insGen_thisVersion="3.22"
insGen_installPath="/syam"
insGen_productType="local" #this gets changed later, but must be kept local as default
insGen_modePrefix="sm"
insGen_modeSuffix="c"
insGen_RMIport="1099"
insGen_SSL="false"

insGen_probe="false"

#---------------
#menu text arrays

insMnu_welcome[0]=""
insMnu_license[0]=""
insMnu_installpath[0]=""
insMnu_installtype[0]=""
insMnu_rmiport[0]=""
insMnu_ssl[0]=""
insMnu_summary[0]=""

#---------------
#installer translation arrays
insMnu_trans[0]=""  #special translations used in menus and other places (messages, etc)

#---------------
#translations for items that the user might see before the language files are extracted

insTrans_def[0]="" #depending on the default language, arrays below are loaded into this one for use

insTrans_en[0]="You must uninstall the product before you can install it again"
insTrans_en[1]="checking for previous versions..."
insTrans_en[2]="error"
insTrans_en[3]="Could not create temp folder... make sure you have full permissions to $insGen_tempdir"
insTrans_en[4]="Could not untar the install package"
insTrans_en[5]="Could not create the install path... make sure you have full permissions"
insTrans_en[6]="extracting files to temp folder (please be patient)..."
insTrans_en[7]="a previous installation was detected.  please uninstall before reinstalling."
insTrans_en[8]="A previous installation was detected.  Would you like to upgrade [1:yes 2:no]?"
insTrans_en[9]="All the files needed for the upgrade have been saved.  The product will now be uninstalled, then reinstalled with the saved files."
insTrans_en[10]="install aborted"
insTrans_en[11]="warning: previous installation detected but the folder was not found"
insTrans_en[12]="continuing with will installation"
insTrans_en[13]="your product types do not match!  you must upgrade a server to a server, desktop to a desktop, or notebook to a notebook"
insTrans_en[14]="your product modes do not match!  local or agent versions may not be upgraded to central versions"
insTrans_en[15]="your product modes do not match!  central versions may not be upgraded to local or agent-only versions"

insTrans_ru[0]="Вы должны удалить предыдущую ин?талл?цию перед повторной попыткой."
insTrans_ru[1]="идет пои?к предыдущих вер?ий продукта..."
insTrans_ru[2]="произошла ошибка"
insTrans_ru[3]="?е удало?ь ?оздать папку временных файлов... проверьте права вашего до?тупа."
insTrans_ru[4]="?е удало?ь ра?крыть tar архив ин?талл?ционного пакета."
insTrans_ru[5]="?е удало?ь ?оздать ин?талл?ционную папку... проверьте права вашего до?тупа."
insTrans_ru[6]="?оздают?? временные файлы (может зан?ть не?колько минут)..."
insTrans_ru[7]="найдена предыдуща? вер?и? продукта. пожалуй?та, деин?таллируйте ее перед у?тановкой новой вер?ии. "
insTrans_ru[8]="Обнаружена ин?талл?ци? предыдущей вер?ии. Продолжить обновление уже у?тановленной программы [1:да 2:нет]?"
insTrans_ru[9]="В?е файлы, необходимые дл? обновлени? программы ?охранены. Программа будет удалена, а, затем, ?нова у?тановлена ? добавлением ?охраненных файлов."
insTrans_ru[10]="ин?талл?ци? не удала?ь"
insTrans_ru[11]="Предупреждение: предыдуща? ин?талл?ци? обнаружена, но папка не найдена"
insTrans_ru[12]="пре?тупаем к ин?талл?ции"
insTrans_ru[13]="Кла??ы ин?талл?ционных пакетов не ?овпадают! Серверный пакет может обновить только ?ерверную ин?талл?цию, а де?ктоповый и ноутбуковый, ?оответ?твенно, только де?ктоповую и ?ерверную.  "
insTrans_ru[14]="У?тановленный пакет отличает?? от ин?талл?ционного! Ин?талл?ционный пакет центрального мониторинга не может обновить у?тановленную программу локального мониторинга или агента.  "
insTrans_ru[15]="У?тановленный пакет отличает?? от ин?талл?ционного! Ин?талл?ционный пакет локального мониторинга или агента не может обновить у?тановленную вер?ию центрального мониторинга."

insTrans_jp[0]="?インストール?る??アンインストールを実行??下??。"
insTrans_jp[1]="以???ージョンを?ェック中…"
insTrans_jp[2]="エラー"
insTrans_jp[3]="tempフォルダーを作??????ん。権?等を確???下??。"
insTrans_jp[4]="インストール パッケージを確??????ん。"
insTrans_jp[5]="インストールパスを作??????ん。権?等を確???下??。"
insTrans_jp[6]="tempフォルダー?ファイルを解?中??待?下??。…"
insTrans_jp[7]="以??インストレーションを検地????。?インストール?る??アンインストールを実行??下??。"
insTrans_jp[8]="以??インストールを発見????。上書??????。?　 [1:yes 2:no]"
insTrans_jp[9]="アップグレード????必??????ファイル??存?れ???。製??アンインストール?れ??????存?れ?ファイル?よ???設置?れ??。"
insTrans_jp[10]="インストール?中止?れ???。"
insTrans_jp[11]="注?：以??インストールを発見??????フォルダーを発見?????ん。"
insTrans_jp[12]="インストールを継続???。"
insTrans_jp[13]="プロダクトタイプ?一致???ん。サー?ー用?デスクトップ用?ノートブック用を確???アップグレード??下??。"
insTrans_jp[14]="プロダクトモード?一致???ん。ローカル?エージェント ?ージョン?セントラル ?ージョン?アップグレード?????ん。"
insTrans_jp[15]="プロダクトモード?一致???ん。セントラル ?ージョン? ローカル?エージェント ?ージョン?アップグレード?????ん。"

insTrans_en_GB[0]="You must uninstall the product before you can install it again"
insTrans_en_GB[1]="checking for previous versions..."
insTrans_en_GB[2]="error"
insTrans_en_GB[3]="Could not create temp folder... make sure you have full permissions to $insGen_tempdir"
insTrans_en_GB[4]="Could not untar the install package"
insTrans_en_GB[5]="Could not create the install path... make sure you have full permissions"
insTrans_en_GB[6]="extracting files to temp folder (please be patient)..."
insTrans_en_GB[7]="a previous installation was detected.  please uninstall before reinstalling."
insTrans_en_GB[8]="A previous installation was detected.  Would you like to upgrade [1:yes 2:no]?"
insTrans_en_GB[9]="All the files needed for the upgrade have been saved.  The product will now be uninstalled, then reinstalled with the saved files."
insTrans_en_GB[10]="install aborted"
insTrans_en_GB[11]="warning: previous installation detected but the folder was not found"
insTrans_en_GB[12]="continuing with will installation"
insTrans_en_GB[13]="your product types do not match!  you must upgrade a server to a server, desktop to a desktop, or notebook to a notebook"
insTrans_en_GB[14]="your product modes do not match!  local or agent versions may not be upgraded to central versions"
insTrans_en_GB[15]="your product modes do not match!  central versions may not be upgraded to local or agent-only versions"

insTrans_de[0]="Sie müssen das alte Produkt deinstallieren, bevor Sie mit der Installation beginnen können"
insTrans_de[1]="Frühere Versionen werden gesucht..."
insTrans_de[2]="fehler"
insTrans_de[3]="Der gewünschte Folder konnte nicht kreiert werden... Stellen Sie den Zugriff sicher $insGen_tempdir"
insTrans_de[4]="Konnte das Package nicht einwandfrei installieren"
insTrans_de[5]="Der gewünschte Pfad konnte nicht kreiert werden... Stellen Sie Ihre Zugriffsrechte sicher"
insTrans_de[6]="Dateien werden in Temp-Folder extrahiert (bitte haben Sie etwas Geduld)..."
insTrans_de[7]="Eine frühere Version wurde gefunden. Vor Neuinstallation bitte entfernen."
insTrans_de[8]="A previous installation was detected.  Would you like to upgrade [1:yes 2:no]?"
insTrans_de[9]="All the files needed for the upgrade have been saved.  The product will now be uninstalled, then reinstalled with the saved files."
insTrans_de[10]="install aborted"
insTrans_de[11]="warning: previous installation detected but the folder was not found"
insTrans_de[12]="continuing with will installation"
insTrans_de[13]="your product types do not match!  you must upgrade a server to a server, desktop to a desktop, or notebook to a notebook"
insTrans_de[14]="your product modes do not match!  local or agent versions may not be upgraded to central versions"
insTrans_de[15]="your product modes do not match!  central versions may not be upgraded to local or agent-only versions"

insTrans_fr[0]="You must uninstall the product before you can install it again"
insTrans_fr[1]="checking for previous versions..."
insTrans_fr[2]="error"
insTrans_fr[3]="Could not create temp folder... make sure you have full permissions to $insGen_tempdir"
insTrans_fr[4]="Could not untar the install package"
insTrans_fr[5]="Could not create the install path... make sure you have full permissions"
insTrans_fr[6]="extracting files to temp folder (please be patient)..."
insTrans_fr[7]="a previous installation was detected.  please uninstall before reinstalling."
insTrans_fr[8]="A previous installation was detected.  Would you like to upgrade [1:yes 2:no]?"
insTrans_fr[9]="All the files needed for the upgrade have been saved.  The product will now be uninstalled, then reinstalled with the saved files."
insTrans_fr[10]="install aborted"
insTrans_fr[11]="warning: previous installation detected but the folder was not found"
insTrans_fr[12]="continuing with will installation"
insTrans_fr[13]="your product types do not match!  you must upgrade a server to a server, desktop to a desktop, or notebook to a notebook"
insTrans_fr[14]="your product modes do not match!  local or agent versions may not be upgraded to central versions"
insTrans_fr[15]="your product modes do not match!  central versions may not be upgraded to local or agent-only versions"

insTrans_fr_CA[0]="You must uninstall the product before you can install it again"
insTrans_fr_CA[1]="checking for previous versions..."
insTrans_fr_CA[2]="error"
insTrans_fr_CA[3]="Could not create temp folder... make sure you have full permissions to $insGen_tempdir"
insTrans_fr_CA[4]="Could not untar the install package"
insTrans_fr_CA[5]="Could not create the install path... make sure you have full permissions"
insTrans_fr_CA[6]="extracting files to temp folder (please be patient)..."
insTrans_fr_CA[7]="a previous installation was detected.  please uninstall before reinstalling."
insTrans_fr_CA[8]="A previous installation was detected.  Would you like to upgrade [1:yes 2:no]?"
insTrans_fr_CA[9]="All the files needed for the upgrade have been saved.  The product will now be uninstalled, then reinstalled with the saved files."
insTrans_fr_CA[10]="install aborted"
insTrans_fr_CA[11]="warning: previous installation detected but the folder was not found"
insTrans_fr_CA[12]="continuing with will installation"
insTrans_fr_CA[13]="your product types do not match!  you must upgrade a server to a server, desktop to a desktop, or notebook to a notebook"
insTrans_fr_CA[14]="your product modes do not match!  local or agent versions may not be upgraded to central versions"
insTrans_fr_CA[15]="your product modes do not match!  central versions may not be upgraded to local or agent-only versions"

insTrans_es[0]="You must uninstall the product before you can install it again"
insTrans_es[1]="checking for previous versions..."
insTrans_es[2]="error"
insTrans_es[3]="Could not create temp folder... make sure you have full permissions to $insGen_tempdir"
insTrans_es[4]="Could not untar the install package"
insTrans_es[5]="Could not create the install path... make sure you have full permissions"
insTrans_es[6]="extracting files to temp folder (please be patient)..."
insTrans_es[7]="a previous installation was detected.  please uninstall before reinstalling."
insTrans_es[8]="A previous installation was detected.  Would you like to upgrade [1:yes 2:no]?"
insTrans_es[9]="All the files needed for the upgrade have been saved.  The product will now be uninstalled, then reinstalled with the saved files."
insTrans_es[10]="install aborted"
insTrans_es[11]="warning: previous installation detected but the folder was not found"
insTrans_es[12]="continuing with will installation"
insTrans_es[13]="your product types do not match!  you must upgrade a server to a server, desktop to a desktop, or notebook to a notebook"
insTrans_es[14]="your product modes do not match!  local or agent versions may not be upgraded to central versions"
insTrans_es[15]="your product modes do not match!  central versions may not be upgraded to local or agent-only versions"

insTrans_es_AR[0]="You must uninstall the product before you can install it again"
insTrans_es_AR[1]="checking for previous versions..."
insTrans_es_AR[2]="error"
insTrans_es_AR[3]="Could not create temp folder... make sure you have full permissions to $insGen_tempdir"
insTrans_es_AR[4]="Could not untar the install package"
insTrans_es_AR[5]="Could not create the install path... make sure you have full permissions"
insTrans_es_AR[6]="extracting files to temp folder (please be patient)..."
insTrans_es_AR[7]="a previous installation was detected.  please uninstall before reinstalling."
insTrans_es_AR[8]="A previous installation was detected.  Would you like to upgrade [1:yes 2:no]?"
insTrans_es_AR[9]="All the files needed for the upgrade have been saved.  The product will now be uninstalled, then reinstalled with the saved files."
insTrans_es_AR[10]="install aborted"
insTrans_es_AR[11]="warning: previous installation detected but the folder was not found"
insTrans_es_AR[12]="continuing with will installation"
insTrans_es_AR[13]="your product types do not match!  you must upgrade a server to a server, desktop to a desktop, or notebook to a notebook"
insTrans_es_AR[14]="your product modes do not match!  local or agent versions may not be upgraded to central versions"
insTrans_es_AR[15]="your product modes do not match!  central versions may not be upgraded to local or agent-only versions"

insTrans_pt[0]="Você precisa desinstalar o produto antes de reinstalar novamente"
insTrans_pt[1]="Verificando versões anteriores..."
insTrans_pt[2]="erro"
insTrans_pt[3]="Não foi possível criar uma pasta temporária... Verifique se sua senha possui acesso total ao sistema $insGen_tempdir"
insTrans_pt[4]="Não foi possível untar o pacote de instalação"
insTrans_pt[5]="Não foi possível criar o atalho para a instalação... Verifique se sua senha possui acesso total ao sistema"
insTrans_pt[6]="Extraindo arquivos para uma pasta temporária (por favor aguarde)..."
insTrans_pt[7]="Uma versão anterior foi encontrada. Por favor desinstale antes de continuar com a instalação"
insTrans_pt[8]="A previous installation was detected.  Would you like to upgrade [1:yes 2:no]?"
insTrans_pt[9]="All the files needed for the upgrade have been saved.  The product will now be uninstalled, then reinstalled with the saved files."
insTrans_pt[10]="install aborted"
insTrans_pt[11]="warning: previous installation detected but the folder was not found"
insTrans_pt[12]="continuing with will installation"
insTrans_pt[13]="your product types do not match!  you must upgrade a server to a server, desktop to a desktop, or notebook to a notebook"
insTrans_pt[14]="your product modes do not match!  local or agent versions may not be upgraded to central versions"
insTrans_pt[15]="your product modes do not match!  central versions may not be upgraded to local or agent-only versions"

insTrans_pt_BR[0]="Você precisa desinstalar o produto antes de reinstalar novamente"
insTrans_pt_BR[1]="Verificando versões anteriores..."
insTrans_pt_BR[2]="erro"
insTrans_pt_BR[3]="Não foi possível criar uma pasta temporária... Verifique se sua senha possui acesso total ao sistema $insGen_tempdir"
insTrans_pt_BR[4]="Não foi possível untar o pacote de instalação"
insTrans_pt_BR[5]="Não foi possível criar o atalho para a instalação... Verifique se sua senha possui acesso total ao sistema"
insTrans_pt_BR[6]="Extraindo arquivos para uma pasta temporária (por favor aguarde)..."
insTrans_pt_BR[7]="Uma versão anterior foi encontrada. Por favor desinstale antes de continuar com a instalação"
insTrans_pt_BR[8]="A previous installation was detected.  Would you like to upgrade [1:yes 2:no]?"
insTrans_pt_BR[9]="All the files needed for the upgrade have been saved.  The product will now be uninstalled, then reinstalled with the saved files."
insTrans_pt_BR[10]="install aborted"
insTrans_pt_BR[11]="warning: previous installation detected but the folder was not found"
insTrans_pt_BR[12]="continuing with will installation"
insTrans_pt_BR[13]="your product types do not match!  you must upgrade a server to a server, desktop to a desktop, or notebook to a notebook"
insTrans_pt_BR[14]="your product modes do not match!  local or agent versions may not be upgraded to central versions"
insTrans_pt_BR[15]="your product modes do not match!  central versions may not be upgraded to local or agent-only versions"

#---------------
#function copy default translations
copyDefTrans()
{
  case "$insGen_locale" in
    "en")
        for (( i = 0; i <= 15; i++ ))
        do
          insTrans_def[$i]=${insTrans_en[$i]}
        done
      ;;
    "en_GB")
        for (( i = 0; i <= 15; i++ ))
        do
          insTrans_def[$i]=${insTrans_en_GB[$i]}
        done
      ;;
    "de")
        for (( i = 0; i <= 15; i++ ))
        do
          insTrans_def[$i]=${insTrans_de[$i]}
        done
      ;;
    "fr")
        for (( i = 0; i <= 15; i++ ))
        do
          insTrans_def[$i]=${insTrans_fr[$i]}
        done
      ;;
    "fr_CA")
        for (( i = 0; i <= 15; i++ ))
        do
          insTrans_def[$i]=${insTrans_fr_CA[$i]}
        done
      ;;
    "es")
        for (( i = 0; i <= 15; i++ ))
        do
          insTrans_def[$i]=${insTrans_es[$i]}
        done
      ;;
    "es_AR")
        for (( i = 0; i <= 15; i++ ))
        do
          insTrans_def[$i]=${insTrans_es_AR[$i]}
        done
      ;;
    "pt")
        for (( i = 0; i <= 15; i++ ))
        do
          insTrans_def[$i]=${insTrans_pt[$i]}
        done
      ;;
    "pt_BR")
        for (( i = 0; i <= 15; i++ ))
        do
          insTrans_def[$i]=${insTrans_pt_BR[$i]}
        done
      ;;
    "jp")
        for (( i = 0; i <= 15; i++ ))
        do
          insTrans_def[$i]=${insTrans_jp[$i]}
        done
      ;;
    "ru")
        for (( i = 0; i <= 15; i++ ))
        do
          insTrans_def[$i]=${insTrans_ru[$i]}
        done
      ;;
  esac
    
}

#########################################
####Main Area (starts running here)

#set up some default translations for the installer
copyDefTrans

#run the pre-install routine
preinstall

#if this is not an upgrade install, run through the normal process
if [ "$insGen_upgrade" != "true" ]; then
	
	#menu system.. stay here until aborted or install begins
	while [ "$insGen_nextFunc" != "install" ]
	do
	
	  #decide what the next menu should be
	  menu_flow_ctrl "$insGen_nextFunc" "$insGen_mnuSel"
	
	  #call the menu that was determined to be next
	  case $insGen_nextFunc in
	    "mnuLocale")
	      mnuLocale
	      ;;
	    "mnuWelcome")
	    	mnuWelcome
	    	;;
	    "mnuLicAgree")
	    	mnuLicAgree
	    	;;
	    "mnuChoosePath")
	    	mnuChoosePath
	    	;;
	    "mnuChooseType")
	    	mnuChooseType
	    	;;
	    "mnuChooseRMI")
	    	mnuChooseRMI
	    	;;
	    "mnuCollectInfo")
	    	mnuCollectInfo
	    	;;
	    "mnuChooseSSL")
	    	mnuChooseSSL
	    	;;
	    "mnuSummary")
	    	mnuSummary
	    	;;
	    "install")
	    	install #the last menu has finished, run the install routine
	    	;;
	  esac
	
	done

fi

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




















