#!/bin/ksh

# @(#) runGSkit - install GS kit. It required root permission

# @02 04/14/05 LCL Always install 32-bit GSkit; #3357
# @01 04/12/05 LCL Fix keyfile.kdb detection exit

Err_report()
{
  case $1 in
          #$NLS-E$ "$PROGNAME warning:  $2, continuing ..."
    warn) print -u2 "$PROGNAME `nls_replace 'KCI0535I'`:  $2, `nls_replace 'KCI0536I'` ..."
          sleep 2
          return
          ;;
          #$NLS-E$ "$PROGNAME failure:  $2."
    fail) print -u2 "$PROGNAME `nls_replace 'KCI0537I'`:  $2."
          exit 2
          ;;
    info) [ -n "$VERBOSE" ] && print -u2 "$PROGNAME        :  $2."
          return
          ;;
          #$NLS-E$ "$PROGNAME error: $2."
   error) print -u2 "$PROGNAME `nls_replace 'KCI0737I'`: $2."
          exit $3
          ;;
  esac
}

gskitVersionAIX()
{
   print $(lslpp -f $1|grep $1|head -1|awk '{print $2}')
}

gskitVersionHPUX()
{
   print $(swlist -l file $1 2>&1|grep $1.$1|head -1|awk '{print $3}')
}

gskitVersionLinux()
{
   print $(rpm -qi $1|grep '^Version'|tail -1|awk '{print $3}').$(rpm -qi $1|grep '^Release'|tail -1|awk '{print $3}')
}

gskitVersionSolaris()
{
   print $(pkginfo -l $1 2>&1 |grep 'VERSION:'|head -1|awk '{print $2}')
}

checkGskitVersion()
{
  gskVersion=$1
  retString=""
  tstV=$(print $version|cut -d. -f1)
  tstR=$(print $version|cut -d. -f2)
  tstM=$(print $version|cut -d. -f3)
  tstF=$(print $version|cut -d. -f4)
  if [[ -n "$gskVersion" ]]
  then
    gskV=$(print $gskVersion|cut -d. -f1)
    gskR=$(print $gskVersion|cut -d. -f2)
    gskM=$(print $gskVersion|cut -d. -f3)
    gskF=$(print $gskVersion|cut -d. -f4)

    if (( gskV == tstV ))
    then
      if (( gskR > tstR ))
      then
        retString=greater
      elif (( gskR < tstR ))
      then
        retString=less
      elif (( gskR == tstR ))
      then
        if (( gskM > tstM ))
        then
          retString=greater
        elif (( gskM < tstM ))
        then
          retString=less
        elif (( gskM == tstM ))
        then
          if (( gskF > tstF ))
          then
            retString=greater
          elif (( gskF < tstF ))
          then
            retString=less
          elif (( gskF == tstF ))
          then
            retString=match
          fi
        fi
      fi
    else
      retString=nomatch
    fi
  else
    retString=missing
  fi

  print $retString
}

# gskInstallDir() determines the location of an installed GSkit package

gskInstallDir()
{

  # Update the line below to force installation of a new version of GSKit
  # but on if a new CANDLEHOME w/ this file is installed
  RHEL21=false
  version=7.0.3.18
  RPMver=$(print $version | awk -F. '{print $1 "." $2 "-" $3 "." $4}')

  # if any GSKit is already installed, then gskitVersion will contain a string

  case $os in
    ai)
      pkgName=gskta.rte
      pkgName_64=gsksa.rte
      gskitVersion=$(gskitVersionAIX $pkgName)
      gskitVersion_64=$(gskitVersionAIX $pkgName_64)
      gskitDirs=$(lslpp -f $pkgName|grep copyright|head -1)
      gskitDirs_64=$(lslpp -f $pkgName_64 2>/dev/null |grep copyright|head -1)
      ;;
    hp)
      [[ $(uname -m) != "ia64" ]] && version=7.0.3.25
      pkgName=gsk7bas
      pkgName_64=gsk7bas64
      [[ $(uname -m) = "ia64" ]] && pkgName=gsk7bas32
      gskitVersion=$(gskitVersionHPUX $pkgName)
      gskitVersion_64=$(gskitVersionHPUX $pkgName_64)
      gskitDirs=$(swlist -l file $pkgName 2>&1|grep copyright|head -1)
      gskitDirs_64=$(swlist -l file $pkgName_64 2>&1|grep copyright|head -1)
      ;;
    li|ls|lp|lx)
      # Linux's are almost all the same, don't duplicate so much code.
      pkgName=gsk7bas
      pkgName_64=gsk7bas64
      ## Is this system Red Hat 2.1?
      if [ "$os" = li ]
      then
        cat /etc/*release |grep "Red Hat" |grep "2.1"  > /dev/null 2>&1
        [ $? -eq 0 ] && RHEL21=true
        ## If this is Red Hat 2.1, use a slightly different package name
        $RHEL21 && pkgName=gsk7bas_295
      fi
      gskitVersion=$(gskitVersionLinux $pkgName)
      gskitVersion_64=$(gskitVersionLinux $pkgName_64)
      gskitDirs=$(rpm -ql $pkgName|grep copyright|head -1)
      gskitDirs_64=$(rpm -ql $pkgName_64|grep copyright|head -1)
      ;;
    so)
      [ $(uname -m) = i86pc ] && version=7.0.3.19
      pkgName=gsk7bas
      pkgName_64=gsk7bas64
      gskitVersion=$(gskitVersionSolaris $pkgName)
      gskitVersion_64=$(gskitVersionSolaris $pkgName_64)
      gskitDirs=$(pkgchk -v $pkgName 2>&1 |grep copyright|head -1)
      gskitDirs_64=$(pkgchk -v $pkgName_64 2>&1 |grep copyright|head -1)
      # we are still assuming that the GSKit version is 7.0.3.18 or 19
      # 7.0.3.18 and 19 do not support alternate paths
      SOL_PKG_PARM=""
      [[ $(uname -r) = 5.10* && $(zonename) = global ]] && SOL_PKG_PARM="-G"
      ;;
  esac
  # we only want the part up to /copyright, ie /usr/local/ibm/gsk7
  gskitDirs=${gskitDirs%/*}
  gskitDirs=$(print $gskitDirs)
  gskitDirs_64=${gskitDirs_64%/*}
  gskitDirs_64=$(print $gskitDirs_64)
}

# ------------------------
# recognizes solaris 10
# return 0: (success) is Solaris 10
# return 1: (failure) no
# ------------------------

is_solaris_10() {
  # check for Solaris 10
  [[ "$(uname -r)" = 5.10* ]] && return 0 || return 1
}

# ----------------------------------
# check if directory is read-only
# $1 - directory to check
# return 0: is read only (success)
#        1: not (no success)
# ----------------------------------

is_read_only() {
  typeset TMPF=/tmp/aa$$
  typeset DIR=$1
  typeset A
  typeset RES=1
  if mount | grep ^$DIR* > $TMPF; then
    read A < $TMPF
    if expr "$A" : .*read\ only*. >/dev/null; then
      RES=0
    fi
  fi
  rm $TMPF
  return $RES
}


# ------------------------------
# creates response param
# SOLARIS_TEMP_RESPONSE_PARAM
# ------------------------------
create_response_param() {
 if [ -z "$SOLARIS_TEMP_RESPONSE_PARAM" ]; then
   SOLARIS_TEMP_RESPONSE_PARAM=/tmp/tempitmresponse$$
 fi
}

# ----------------------------------
# removes reponse param if created
# ----------------------------------

remove_response_param() {
 if [ -n "$SOLARIS_TEMP_RESPONSE_PARAM" ]; then
   rm $SOLARIS_TEMP_RESPONSE_PARAM
 fi
 unset SOLARIS_TEMP_RESPONSE_PARAM
}

# -------------------------------
# add param to response file
# $1 - param name
# $2 - param value
# -------------------------------

add_param_to_response_file() {
  create_response_param
  typeset PARAM1=$1
  typeset PARAM2=$2
  print "$PARAM1=$PARAM2" >> $SOLARIS_TEMP_RESPONSE_PARAM
}

# --------------------------------
# adds reponse param to file
# $1 - file to add
# --------------------------------

add_response_to_file() {
 if [ -n "$SOLARIS_TEMP_RESPONSE_PARAM" ]; then
   cat $SOLARIS_TEMP_RESPONSE_PARAM >> $1
 fi
}

# -----------------------------
# ask for yes
# return 1: user confirm no
# return 0: user answered yes
# -----------------------------

confirm_yes_answer() {
  typeset ANSWWER
  while true; do
    unset ANSWER
    #$NLS-E$ "Enter 1-yes or 2-no [default is 1]:"
    print `nls_replace 'KCI1229I' '1' '2' '\"1\"'`
    read ANSWER
    if [ -z "$ANSWER" ]; then ANSWER=yes; fi
    case $ANSWER in
      1|yes) return 0;;
      2|no) return 1;;
    esac
  done
}

# --------------------------------------------------------------
# ask for directory
# $1 - default directory
# return 0 (success): user entered directory TEMP_RESPONSE_DIRECTORY
# return 1: user resigned
# --------------------------------------------------------------

get_directory_from_user() {
  unset TEMP_RESPONSE_DIRECTORY
  while true; do
    #$NLS-E$ "Default directory is: $1"
    print `nls_replace 'KCI1216I' $1`
    #$NLS-E$ "Enter directory (should start with /) or 'q' if you resign"
    print `nls_replace 'KCI1217I' '\"q\"'`
    read TEMP_RESPONSE_DIRECTORY
    if [ -z "$TEMP_RESPONSE_DIRECTORY" ] ; then
      TEMP_RESPONSE_DIRECTORY=$1
    fi
    if [ -n "$TEMP_RESPONSE_DIRECTORY" ] ; then
      if [ "$TEMP_RESPONSE_DIRECTORY" = "q" ] ; then
         return 1
      fi
      if is_read_only $TEMP_RESPONSE_DIRECTORY; then
        #$NLS-E$ "Directory you have entered is read-only file system."
        print `nls_replace 'KCI1218I'`
      else
        return 0
      fi
    fi
  done
}

# -----------------------------
# set new GSKit directories
# $1 - base directory
# $2 - 1: /usr : 2/ opt
# GSKIT_DIRECTORY1
# GSKIR_DIRECTORY2
# -----------------------------

set_newgskit_directories() {
  typeset DIR=$1
  typeset WHAT=$2
  unset GSKIT_DIRECTORY1
  unset GSKIT_DIRECTORY2
  case $WHAT in
  1)
    GSKIT_DIRECTORY1=$DIR/usr/bin
    GSKIT_DIRECTORY2=$DIR/usr/lib
    ;;
  2)
    GSKIT_DIRECTORY1=$DIR/ibm/gsk7
    ;;
  esac
}

# -----------------------------------
# generate info text
# $1 - base directory
# $2 - directory type
# return GSKIT_INFO_MESSAGE
# ------------------------------------
generate_info_tekst() {
  set_newgskit_directories $1 $2
  if [ -z "$GSKIT_DIRECTORY2" ]; then
    #$NLS-E$ " $GSKIT_DIRECTORY1 directory."
    GSKIT_INFO_MESSAGE=`nls_replace 'KCI1219I' $GSKIT_DIRECTORY1`
  else
    #$NLS-E$ "$GSKIT_DIRECTORY1 and $GSKIT_DIRECTORY2 directories."
    GSKIT_INFO_MESSAGE=`nls_replace 'KCI1220I' $GSKIT_DIRECTORY1 $GSKIT_DIRECTORY2`
  fi
}

# ----------------------------------
# check for read-only and get new directory from user
# $1 - directory to check
# $2 - 1: /usr directory :2 /opt directory
# $3 - default directory
# TEMP_RESPONSE_DIRECTORY
# return 2: not read only
#        0: read only, TEMP_RESPONSE_DIRECTORY new value
#        1: failure, user resign
# ----------------------------------

check_and_get_directory_from_user() {
  typeset DIR=$1
  typeset WHAT=$2
  typeset DEFA=$3
  if ! is_read_only $DIR; then return 2; fi

  if [ "$silentMode" = "yes" ]; then
    TEMP_RESPONSE_DIRECTORY=$DEFA
    return 0
  fi
  #$NLS-E$ "Directory $DIR is read-only file system."
  print `nls_replace 'KCI1221I' $DIR`
  #$NLS-E$  "It is not possible to install GSKit into this directory."
  print `nls_replace 'KCI1222I'`
  #$NLS-E$  "You have to enter new directory for GSKit installation."
  print `nls_replace 'KCI1223I'`
  #$NLS-E$  "Example:"
  print `nls_replace 'KCI1224I'`
  #$NLS-E$  "If you enter /tmp directory GSKit files will be placed in"
  print `nls_replace 'KCI1225I'`
  generate_info_tekst /tmp $WHAT
  print $GSKIT_INFO_MESSAGE
  while true; do
     if ! get_directory_from_user $DEFA; then return 1; fi
     generate_info_tekst $TEMP_RESPONSE_DIRECTORY $WHAT
     #$NLS-E$ "GSkit files will be placed in $GSKIT_INFO_MESSAGE"
     print `nls_replace 'KCI1226I' "$GSKIT_INFO_MESSAGE"`
     #$NLS-E$  "Please confirm that."
     print `nls_replace 'KCI1227I'`
     #$NLS-E$ "If you enter no you will be able to re-enter directory name."
     print `nls_replace 'KCI1228I' '\"2\"'`
     if confirm_yes_answer; then return 0; fi
  done
}

# ----------------------------------------------
# SOLARIS_PACKAGE_PARAM
# Recognize and define -G param for Solaris
# return 0: success
#        1: failure, cannot continue
# ----------------------------------------------
check_solaris_package_param() {
  # check for Solaris 10
  unset SOLARIS_PACKAGE_PARAM
  if is_solaris_10 ; then SOLARIS_PACKAGE_PARAM=-G; fi
  check_and_get_directory_from_user /usr 1 $CANDLEHOME/gsklinks
  typeset RES=$?
  if [ "$RES" -eq 1 ]; then return 1; fi
  if [ "$RES" -eq 0 ]; then
    set_newgskit_directories $TEMP_RESPONSE_DIRECTORY 1
    add_param_to_response_file LNKBINDIR $GSKIT_DIRECTORY1
    add_param_to_response_file LNKLIBDIR $GSKIT_DIRECTORY2
  fi
  check_and_get_directory_from_user /opt 2 ""
  typeset RES=$?
  if [ "$RES" -eq 1 ]; then return 1; fi
  if [ "$RES" -eq 0 ]; then
    add_param_to_response_file BASEDIR $GSKIT_DIRECTORY1
  fi
  return 0
}

###################################################################
#                          Main                                   #
###################################################################
bindir=`pwd`/$(dirname $0)
[ "$(print $0 | grep '\./')" = "" ] && [ "$(print $0 | grep '^/.*')" = "" ] && [ `pwd` = '/' ] && bindir=`pwd`$(dirname $0)
[ "$(print $0 | grep '^/.*')" != "" ] && bindir=$(dirname $0)

export TEXTDOMAINDIR=$bindir/../config/nls/msg
export PATH=$bindir:$PATH

PROGNAME=$(basename $0)
[[ "$CDNAME" = @(m3v4*|hlv*|exv*|3zv*) ]] && exit 0
[ -z "$(whoami 2>/dev/null)" ] && alias whoami='id | cut -d"(" -f2 | cut -d")" -f1'

# The caller will pass in -mode gui|cmd|silent and we'll default to cmd
# install.sh will pass in -mode gui|cmd|silent
# installGSkit will pass in -mode cmd|silent

mode=cmd
[ "X$1" = X-mode ] && mode=$2
[ $mode = silent ] && silentMode=yes

# Install GSkit according to the running platform

isSol=no
isAix=no    #44386
isHP=no     #44386
isLinux=no  #44386
os=$(print $cArch|cut -c1-2)
hw=$(print $cArch|cut -c2-3)


PATH=$PATH:/usr/sbin                    #defect 19814 for solaris
export PATH

# Extract the bitMode (32/64) of the platform
len=0
len=$(print $cArch|wc -c)
len=$(($len-1))
bitNum=$(print $cArch|cut -c$len)
[ -z "$bitNum" ] && bitNum=3

# EJF (#19197) if bitNum is not 6 make it 3
[ $bitNum = 6 ] || bitNum=3


# See if GSkit is already installed.
# Skip the install if this version already installed.
# Set a bunch of common variables.
gskInstallDir


loop=yes
loop_64=yes
[[ $(checkGskitVersion "$gskitVersion") = @(greater|match) ]] && loop=no
[[ $(checkGskitVersion "$gskitVersion_64") = @(greater|match) ]] && loop_64=no
[ $bitNum = 3 ] && loop_64=no


#The line below looks odd, but the package for Linux Itanium (lia266)
#is actually gsk7bas, not gsk7bas64
[[ "$os" = li && "$hw" = ia ]] && loop_64=no


if [ -n "$GSK_BAILOUT" ]
then
  loop=no
  loop_64=no
fi


case $os in
  ai)
    if [ $bitNum = 6 ]
    then
      gskArch=aix526
      gskDir_64=$CANDLEHOME/gskit/$gskArch
      [ -d $gskDir_64 ] || mkdir $gskDir_64
      cmd_64="installp -acgqw -d $gskDir_64 gsksa.rte gskjs.rte"
    fi
    # For 64 bit system, we will install both
    gskArch=aix523
    gskDir=$CANDLEHOME/gskit/$gskArch
    [ -d $gskDir ] || mkdir $gskDir
    cmd="installp -acgqw -d $gskDir gskta.rte gskjt.rte"
    isAix=yes    #44386
    ;;
  hp)
    if [ $bitNum = 6 ]
    then
      [ "$hw" = "pi" ] && gskArch=hpi116 || gskArch=hp116
      gskDir_64=$CANDLEHOME/gskit/$gskArch
      [ -d $gskDir_64 ] || mkdir $gskDir_64
      cd $gskDir_64; zcat $pkgName_64.tar.Z |tar -xf -
      cd - >/dev/null
      cmd_64="swinstall -x mount_all_filesystems=false -s $gskDir_64/$pkgName_64 $pkgName_64"
    fi
    [ "$hw" = "pi" ] && gskArch=hpi113 || gskArch=hp11
    gskDir=$CANDLEHOME/gskit/$gskArch
    [ -d $gskDir ] || mkdir $gskDir
    cd $gskDir; zcat $pkgName.tar.Z |tar -xf -
    cd - >/dev/null
    cmd="swinstall -x mount_all_filesystems=false -s $gskDir/$pkgName $pkgName"
    isHP=yes    #44386
    ;;
  li)
    #See if this is Itanium first, otherwise process
    #as Linux Intel
    #Note: Itanium's gskit package is called gsk7bas, NOT gsk7bas64
    if [ $hw = ia ]
    then
      gskArch=lia266
      RPMplat=ia64
      #rpm -Uv will upgrade or install as appropriate, so no need to
      #check if this is a fresh install or not
      gskDir=$CANDLEHOME/gskit/$gskArch
      [ -d $gskDir ] || mkdir $gskDir
      cmd="rpm -Uv $gskDir/$pkgName-$RPMver.$RPMplat.rpm --nodeps"
    else
      #Deal with Intel 32 bit Linux
      gskArch=li6243
      ## Find red Hat 2.1?
      $RHEL21 && gskArch=li6242
      RPMplat=i386
      gskDir=$CANDLEHOME/gskit/$gskArch
      [ -d $gskDir ] || mkdir $gskDir
      [ -n "$gskitDirs" ] && RPMmode="Uv" || RPMmode="iv"
      cmd="rpm -$RPMmode $gskDir/$pkgName-$RPMver.$RPMplat.rpm"
    fi
    isLinux=yes  #44386
    ;;
  lp|ls|lx)
    if [ $bitNum = 6 ]
    then
      [ $os = lp ] && gskArch=lpp266 && RPMplat=ppc64
      [ $os = ls ] && gskArch=ls3266 && RPMplat=s390x
      [ $os = lx ] && gskArch=lx8266 && RPMplat=x86_64
      gskDir_64=$CANDLEHOME/gskit/$gskArch
      [ -d $gskDir ] || mkdir $gskDir_64
      [ -n "$gskitDirs_64" ] && RPMmode="Uv" || RPMmode="iv"
      cmd_64="rpm -$RPMmode $gskDir_64/$pkgName_64-$RPMver.$RPMplat.rpm"
    fi

    [ $os = lp ] && gskArch=lpp263 && RPMplat=ppc32
    [ $os = ls ] && gskArch=ls3263 && RPMplat=s390
    [ $os = lx ] && gskArch=lx8266 && RPMplat=i386     # not an error
    gskDir=$CANDLEHOME/gskit/$gskArch
    [ -d $gskDir ] || mkdir $gskDir
    [ -n "$gskitDirs" ] && RPMmode="Uv" || RPMmode="iv"
    cmd="rpm -$RPMmode $gskDir/$pkgName-$RPMver.$RPMplat.rpm"
    isLinux=yes  #44386
    ;;
  so)
    if [[ $loop = "yes"  ||  $loop_64 = "yes" ]]; then
      if ! check_solaris_package_param; then
        #$NLS-E$ "User response, cannot continue installation."
        Err_report error "`nls_replace 'KCI1230I'`" 10
      fi
    fi
    typeset SOL_PKG_ADD="pkgadd $SOLARIS_PACKAGE_PARAM"

    if [ $bitNum = 6 ]
    then
      [ $(uname -m) = i86pc ] && gskArch=sol606 || gskArch=sol286
      gskDir_64=$CANDLEHOME/gskit/$gskArch
      gskTmp=/tmp/$pkgName_64
      [ -d $gskDir_64 ] || mkdir $gskDir_64
      cd $gskDir_64; zcat $pkgName_64.tar.Z |tar -xf -
      print "instance=overwrite" > admin.txt
      print "action=nocheck" >>admin.txt
      print "GSK_UPGRADE_ONLY=1" > resp.txt
      add_response_to_file resp.txt
      cmd_64="$SOL_PKG_ADD -a $gskDir_64/admin.txt -r $gskDir_64/resp.txt -d $gskDir_64 $pkgName_64"

      # version 7.0.3.18 has a bug that causes it to only be installable
      # from /tmp on 32 bit solaris
      typeset -i osver=0
      osver=$(uname -a | grep 5.8 | wc -l )
      if [ osver -ne 0 ]
      then
        [ -d $gskTmp ] && rm -rf $gskTmp
        mkdir $gskTmp; chmod 777 $gskTmp
        cp -R $pkgName_64 $gskTmp
        if [ $? -ne 0 ]
        then
          #$NLS-E$ "Copy error, insufficient disk space or permissions problems on /tmp"
          nls_replace 'KCI0738E' /tmp
          exit 10
        fi
        print "instance=overwrite" > $gskTmp/admin.txt
        print "action=nocheck" >> $gskTmp/admin.txt
        print "GSK_UPGRADE_ONLY=1" > $gskTmp/resp.txt
        add_response_to_file $gskTmp/resp.txt
        cmd_64="$SOL_PKG_ADD -a $gskTmp/admin.txt -r $gskTmp/resp.txt -d $gskTmp $pkgName_64"
      fi
      cd - >/dev/null
    fi

    [ $(uname -m) = i86pc ] && gskArch=sol603 || gskArch=sol283
    gskDir=$CANDLEHOME/gskit/$gskArch
    gskTmp=/tmp/$pkgName
    [ -d $gskDir ] || mkdir $gskDir
    cd $gskDir; zcat $pkgName.tar.Z |tar -xf -
    print "instance=overwrite" > admin.txt
    print "action=nocheck" >> admin.txt
    print "GSK_UPGRADE_ONLY=1" > resp.txt
    add_response_to_file resp.txt
    cmd="$SOL_PKG_ADD -a $gskDir/admin.txt -r $gskDir/resp.txt -d $gskDir $pkgName"

    # version 7.0.3.18 has a bug that causes it to only be installable
    # from /tmp on 32 bit solaris
    typeset -i osver=0
    osver=$(uname -a | grep 5.8 | wc -l )
    if [ osver -ne 0 ]
    then
      [ -d $gskTmp ] && rm -rf $gskTmp
      mkdir $gskTmp; chmod 777 $gskTmp
      cp -R $pkgName $gskTmp
      if [ $? -ne 0 ]
      then
        #$NLS-E$ "Copy error, insufficient disk space or permissions problems on /tmp"
        nls_replace 'KCI0738E' /tmp
        exit 10
      fi
      print "instance=overwrite" > $gskTmp/admin.txt
      print "action=nocheck" >> $gskTmp/admin.txt
      print "GSK_UPGRADE_ONLY=1" > $gskTmp/resp.txt
      add_response_to_file $gskTmp/resp.txt
      cmd="$SOL_PKG_ADD -a $gskTmp/admin.txt -r $gskTmp/resp.txt -d $gskTmp $pkgName"
    fi
    cd - >/dev/null
    isSol=yes
    ;;
esac


while [[ $loop = "yes" || $loop_64 = "yes" ]]
do
  #$NLS-E$ "Preparing to install the IBM Global Security Kit (GSkit)"
  nls_replace 'KCI0739I'

  # If we're already root, there's no need to su
  if [ "$(whoami)" = "root" ]
  then

    if [ $loop = "yes" ]
    then
      if [[ "$isSol" = "yes" && "$silentMode" = "yes" ]]
      then
        print y | $cmd
      else
        $cmd
      fi
      rc=$?                   # defect 4990

      # 12261: we need to process >0 as an error or warning for all platforms,
      # not just linux
      # for interactive install, we want the install to terminate (10)
      # for silent install, we want to continue with warning (9) to avoid
      # regressing remote deploy and DM upgrade
      [[ "$isSol" = "yes" && $rc -eq 4 ]] && rc=0  # 44018 platform-specific, allow pass
      [[ "$isLinux" = "yes" && $rc -eq 2 ]] && rc=0  # 44386 platform-specific, allow pass
      [[ $rc -gt 0 && "$silentMode" = "yes" ]] && exit 9
      [[ $rc -gt 0 && "$silentMode" != "yes" ]] && exit 10
    fi



    if [ $loop_64 = "yes" ]
    then
      if [[ "$isSol" = "yes" && "$silentMode" = "yes" ]]
      then
        print y | $cmd_64
      else
        $cmd_64
      fi
      rc=$?                 # defect 4990

      # 12261: we need to process >0 as an error or warning for all platforms,
      # not just linux
      # for interactive install, we want the install to terminate (10)
      # for silent install, we want to continue with warning (9) to avoid
      # regressing remote deploy and DM upgrade
      [[ "$isSol" = "yes" && $rc -eq 4 ]] && rc=0  # 44018 platform-specific, allow pass
      [[ "$isLinux" = "yes" && $rc -eq 2 ]] && rc=0  # 44386 platform-specific, allow pass
      [[ $rc -gt 0 && "$silentMode" = "yes" ]] && exit 9
      [[ $rc -gt 0 && "$silentMode" != "yes" ]] && exit 10
    fi



    loop=no
    loop_64=no

    # install the auto start agent features
    if [ -f $CANDLEHOME/registry/AutoStart ]
    then
      autoInstall=
    else
      if [ "$silentMode" != "yes" ]
      then
        #$NLS-E$ " Will enable automatic agent initiation after reboot.\n"
        nls_replace 'KCI0740I'
        print
      fi
      $CANDLEHOME/bin/AutoRunAgents.sh
    fi





  else
    # install the auto start agent features
    if [ -f $CANDLEHOME/registry/AutoStart ]
    then
      #$NLS-E$ "Auto start of agents already enabled"
      Err_report info "`nls_replace 'KCI0741I'`"
      autoInstall=
    else
      #$NLS-E$ "Will enable automatic agent initiation after reboot"
      Err_report info "`nls_replace 'KCI0742I'`"
      autoInstall=$CANDLEHOME/bin/AutoRunAgents.sh
    fi

    # If we're not root and we're in silent mode bail out now with a cc=9
    [ "$silentMode" = "yes" ] && exit 9
    if [ $loop = "yes" ]
    then
      if [ $loop_64 = "yes" ]              # install two gskit
      then
        #$NLS-E$ "\nroot user access is required to install IBM GSkit prerequisite."
        print
        nls_replace 'KCI0743I'
        su root -c "$cmd; $cmd_64; $autoInstall"  # The name "root" is required, or the command will fail on Solaris 2.5.
      else
        #$NLS-E$ "\nroot user access is required to install IBM GSkit prerequisite."
        print
        nls_replace 'KCI0743I'
        su root -c "$cmd; $autoInstall"  # The name "root" is required, or the command will fail on Solaris 2.5.
      fi
    else
      #loop is no, which means 32 bit already installed - now we just check 64 and we go from there
      if [ $loop_64 = "yes" ]
      then
        # this means that we want to install 64 bit
        #$NLS-E$ "\nroot user access is required to install IBM GSkit prerequisite."
        print
        nls_replace 'KCI0743I'
        su root -c "$cmd_64; $autoInstall"  # The name "root" is required, or the command will fail on Solaris 2.5.
      fi
    fi
  fi
  rc=$?
  [[ "$isSol" = "yes" && $rc -eq 4 ]] && rc=0  # 44018 platform-specific, allow pass
  [[ "$isLinux" = "yes" && $rc -eq 2 ]] && rc=0  # 44386 platform-specific, allow pass
  if [ $rc -eq 0 ]  # su worked
  then
    loop=no
    loop_64=no
  elif [ $rc -eq 74 ]  # su failed.
  then
    #$NLS-E$ "\nPlease acquire root access, and run \"installGSkit\" from the command line to complete the installation."
    print
    print "`nls_replace 'KCI0744I' \\"installGSkit\\"`"
    sleep 5
    exit 74
  elif [ $rc -eq 4 ]     # GSKit is probably already installed, just keep going
  then
    break
  else # All others just wait for  response. For su fail sun and linux return 1, aix returns 113, hp returns 9
    # 12261: need to process 1 as su or install failure.  since this is
    # interactive, give the user a chance to remedy and retry.  if they
    # choose not to retry, exit 10 (which will terminate the overall install)
    #$NLS-E$ "\nIgnore [ i ] or Try again [ y , n; \"n\" is default ]?  \c"
    print "\n`nls_replace 'KCI0745I'` [ 1 ] `nls_replace 'KCI0746I'` [ 2-`nls_replace 'KCI0197I'`, 3-`nls_replace 'KCI0198I'`; \"3\" `nls_replace 'KCI0199I'` ]?  \c"
    read
    case $REPLY in
      y*|Y*|2)
        continue
        ;;
      i*|I*|1)
        exit 9                 # su or install failed: in this case, we allow the install to proceed with a warning
        ;;
      *)
        exit 10                # su or install failed: install.sh processes 10 as error to end the interactive install
        ;;
    esac
  fi
done

# Getting here means GSkit is installed, now do any post processing like setting up keyfiles, etc

#remove /tmp/gsk7bas* on Solaris platform
if [ "$os" = "so" ]
then
  rm -Rf /tmp/gsk7bas* 2>/dev/null
fi

# Get the GSkit install location for setting ICCRTE_DIR
if [[ $cArch = @(lx8266|lpp266) ]]
then
  [ -z "$gskitDirs_64" ] && gskInstallDir
  gskitLoc=$gskDir_64/gsKit.info
  [ -n "$gskitDirs_64" ] && print "GSkitInstallDir=$gskitDirs" > $gskitLoc
else
  [ -z "$gskitDirs" ] && gskInstallDir
  gskitLoc=$gskDir/gsKit.info
  [ -n "$gskitDirs" ] && print "GSkitInstallDir=$gskitDirs" > $gskitLoc
fi

# Build the keyfile directory
keyDir=$CANDLEHOME/keyfiles
[ -d $keyDir ] || mkdir $keyDir

# Skip the keyfile and certificate creation if we already have a keyfile, otherwise rewriting the
# encryption key means all previously encrypted data will be undecipherable
if [ -f "$keyDir/keyfile.kdb" ]
then
  #$NLS-E$ "Keyfile.kdb already exists, skipping keyfile and certificate creation"
  Err_report info "`nls_replace 'KCI0747I' Keyfile.kdb`"
  exit
fi

# Define keyfile and certificate parameters
CertificateCommonName="IBM Tivoli Monitoring Self-Signed Certificate"
CertificateOrgUnit="IBM Tivoli"
CertificateOrg="IBM"
CertificateCountry="US"
CertificateExpirationTime=3650
DatabaseName="$keyDir/keyfile.kdb"
DatabasePassword="IBM61TIV"
PasswordExpiration=0
KeyLabel="IBM_Tivoli_Monitoring_Certificate"
DistinguishedName="CN=$CertificateCommonName,O=$CertificateOrg,C=$CertificateCountry"

# for 64-bit zLinux, we will use the 64-bit commands
# to create the keyfile and certificate.  All other
# platforms use the 32-bit commands, regardless of
# whether the platform is 32-bit or 64-bit.

if [[ $cArch = @(ls3266|lpp266|lx8266|lia246|lia266) ]]
then
  useGskitDirs=$gskitDirs_64
  gsk7cmd=gsk7cmd_64
else
  useGskitDirs=$gskitDirs
  # d11371.1 abstract      CVTR:createnode fails for RedHat2.1 OS Agent
  # RHEL2.1 GSKit installs uses gsk7cmd_gcc295, not gsk7cmd
  # d20177 abstract      SVT: REGR: GSKit 408 error in log files
  # gsk7cmd_gcc295 generates a database error after the upgrade to
  # gsk7bas_295-7.0-3.18
  if $RHEL21
  then
    gsk7cmd=gsk7capicmd_gcc295
  elif [[ $cArch = hpi11* ]]
  then
    gsk7cmd=gsk7cmd_32
  else
    gsk7cmd=gsk7cmd
  fi
fi

# Setup GSkit command paths
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=$useGskitDirs/classes/cfwk.zip:$useGskitDirs/classes/gsk7cls.jar:$CLASSPATH
logFile=$CANDLEHOME/logs/candle_installation.log

# Create the keyfile and certificate
#$NLS-E$ "creating security files"
Err_report info "`nls_replace 'KCI0748I'`"
Err_report info "...creating GSkit security keyfile and certificate" >> $logFile
Err_report info "...GSkit version info" >> $logFile
$useGskitDirs/bin/$gsk7cmd -version -help >> $logFile 2>&1

# Create the keyfile
#$NLS-E$ "create keyfile"
Err_report info "`nls_replace 'KCI0749I'`"
Err_report info "...create GSkit security keyfile" >> $logFile
$useGskitDirs/bin/$gsk7cmd -keydb -create -db $DatabaseName -pw $DatabasePassword -type cms -stash >> $logFile 2>&1

# Create the certificate (self-signed)
#$NLS-E$ "create certificate"
Err_report info "`nls_replace 'KCI0750I'`"
Err_report info "...create GSkit security certificate" >> $logFile
$useGskitDirs/bin/$gsk7cmd -cert -create -db $DatabaseName -pw $DatabasePassword -size 1024 -dn "$DistinguishedName" -label $KeyLabel -default_cert yes -expire $CertificateExpirationTime >> $logFile 2>&1

# d20177 abstract      SVT: REGR: GSKit 408 error in log files
# on RHEL21, insure the keyfiles/ have the same permissions when using gsk7capicmd_gcc295
if $RHEL21
then
  files="keyfile.crl keyfile.kdb keyfile.rdb keyfile.sth"
  for filename in $files; do
    if [ -f $keyDir/$filename ]
    then
      chmod 666 $keyDir/$filename >> $logFile 2>&1
    fi
  done
fi


# Run the CryptoSetKey program

# Start real code
propFile=$CANDLEHOME/tmp/property.txt
print "kjr.crypto.keyfilesURL=file://$CANDLEHOME/keyfiles" > $propFile

# Get encryption key: prompting now takes place in gskSetkey Java program

defaultKey="IBMTivoliOMEGAMONEncryptionKey61"

if [ -n "$silentMode" ]
then
  if [ -f "$PARM_FILE" ]
  then
    silentKey=$(grep "^INSTALL_ENCRYPTION_KEY" $PARM_FILE | head -1 )
    silentKey=${silentKey#*=}
    key=$silentKey
    len=$(print $key|wc -c)
    len=$(($len-1))
  else
    key=$defaultKey
  fi

  # EJF (#12383) disallow certain characters
  print $key | grep "[\$=|]" > /dev/null 2>&1
  #$NLS-E$ "INSTALL_ENCRYPTION_KEY cannot contain the following characters \"\$=|\""
  [[ $? = 0 ]] && Err_report error "`nls_replace 'KCI0751E' INSTALL_ENCRYPTION_KEY` \"\$=|\"" 10
fi

#$NLS-E$ "setting encryption key"
Err_report info "`nls_replace 'KCI0752I'`"
$JREEXE -classpath $CLASSPATH -DGlobalDbgLevel="$TRACE_LEVEL" -DInstallRASConfig="$CANDLEHOME/config/ITMConfigRAS.properties" ITMinstall/gskSetkey -ch $CANDLEHOME -k "$key" -mode $mode
cc=$?

rm -f $propFile

# double check for the auto start code
if [ -f $CANDLEHOME/registry/AutoStart ]
then
  autoInstall=
else
  if [ "$(whoami)" = "root" ]
  then
    $CANDLEHOME/bin/AutoRunAgents.sh
  else
    if [ "$silentMode" != "yes" ]
    then
      #$NLS-E$ "\nAgents are not currently enabled to run automatically after reboot."
      Err_report info "`nls_replace 'KCI0753I' '\n'`"
      #$NLS-E$ "\nEnter root password to configure automatic restart of agents."
      print "`nls_replace 'KCI0754I' '\n'`"
      autoInstall=$CANDLEHOME/bin/AutoRunAgents.sh

      dloop=yes
      while [ $dloop = "yes" ]
      do
        su root -c "$autoInstall"  # The name "root" is required, or the command will fail on Solaris 2.5.
        if [ $? -ne 0 ]
        then
          #$NLS-E$ "\nIgnore [ i ] or Try again [ y , n; \"n\" is default ]?  \c"
          print "\n`nls_replace 'KCI0745I'` [ 1 ] `nls_replace 'KCI0746I'` [ 2-`nls_replace 'KCI0197I'`, 3-`nls_replace 'KCI0198I'`; \"3\" `nls_replace 'KCI0199I'` ]?  \c"
          read
          case $REPLY in
            y*|Y*|2)
              continue
              ;;
            i*|I*|1)
              #$NLS-E$ "Please acquire root access, and run \"AutoRunAgents.sh\" from the command line to configure automatic restart of agents."
              Err_report warn "`nls_replace 'KCI0755I' \\"AutoRunAgents.sh\\"`"
              break
            ;;
            *)
              #$NLS-E$ "Please acquire root access, and run \"AutoRunAgents.sh\" from the command line to configure automatic restart of agents."
              Err_report warn "`nls_replace 'KCI0755I' \\"AutoRunAgents.sh\\"`"
            ;;
          esac
        fi
        break
      done
    fi
  fi
fi

exit $cc
