#!/bin/ksh
#
# @09 03/08/05 LCL Add ITMinstall. to JREEXE invocations
# @08 01/25/05 LCL Repl line cmd w/ "head -1" for redhat linux.
# @07 06/20/03 LCL TZ handling for Linux
# @06 12/10/02 LCL Rm debug msg.
# @05 11/17/02 LCL Err msg for seed failures.
# @04 10/16/02 LCL TZ regenerated each time; 1-20932901
# @03 08/02/00 LCL Add safe mode
# @02 07/19/00 LCL Multi-product processing
# @01 07/10/00 LCL Capture cmd line
#
# This script is a shell wrapper for CandleSeed.class. It checks command 
# line arguments and passes them to CandleSeed.class for further processing.


Err_report() {
   case $1 in
	  #$NLS-E$ "$PROGNAME warning: $2,continuing ..."	
      warn) print -u2 "$PROGNAME `nls_replace 'KCI0340I'`: $2,`nls_replace 'KCI0341I'` ..."
            sleep 2
            return
            ;;
	  #$NLS-E$ "$PROGNAME failure: $2."		
      fail) print -u2 "$PROGNAME `nls_replace 'KCI0342I'`: $2."
            exit 2
            ;;
      info) print -u2 "$PROGNAME        : $2."
            return
            ;;
   esac
}

Use_report () {
    print -u2 "$PROGNAME [ -h install_dir ] [-m] -t tems_name pc"
	#$NLS-E$ "Use -m to skip installation of product provided situations and policies"
    print -u2 "`nls_replace 'KCI0343I' '-m'`"
    exit 2
}

###################################################################
#                         Main Routine                            #
###################################################################
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

export PATH=/usr/bin:$PATH
export AIXTHREAD_SCOPE=S

UsrCmd="$0 $*"
if [[ -z "$ITMCMD" ]]
then
    PROGNAME=$(basename $0)
else
    PROGNAME="$ITMCMD"
fi

HOPTION=""
SAFE_MODE=""

typeset -i hIdx=0 numPC=0 pcIdx=0 sIdx=0 tIdx=0
typeset idxList

while getopts ":h:t:m" OPTS
do case $OPTS in
     h) CANDLEHOME=$OPTARG
        HOPTION="ON"
        hIdx=$OPTIND
        ;;
     t) CMSNAME=$OPTARG
        tIdx=$OPTIND
        ;;
     m) SKIPKDSTSNS="-m"
        ;;
     ?) Use_report
        ;;
   esac
done

[ tIdx -gt 0 ] || Use_report 

# Find the largest option index.  This number will be the index of the first product code.

idxList="$hIdx\\n$sIdx\\n$tIdx"  # Add newline characters so the list can be sorted.
pcIdx=$(print $idxList | sort -nr 2> /dev/null | head -1)  # The sort is in descending order.

shift pcIdx-1  # Move to the first product code.
numPC=$#
[ numPC -ge 1 ] || Use_report
product=$*  # Collect the product codes.

# The following fix is required since HP incorrectly processes OPTIND when -s is the last option.

product=$(print $product | sed "s/\-s//")
#print ... product = $product

[ -n "$CANDLEHOME" ] || CANDLEHOME=$(cd $(dirname $0) ; dirname $(pwd) )
export CANDLEHOME

if [ ! -d "$CANDLEHOME" ]
then
   #$NLS-E$ "Cannot set CANDLEHOME environment variable"	
   Err_report fail "`nls_replace 'KCI0344E' CANDLEHOME`"
fi

if [ $(print "$CANDLEHOME" | wc -w) -ne 1 ]
then
  #$NLS-E$ "Blanks are not permitted in install_dir"
  Err_report fail "`nls_replace 'KCI0345E' install_dir`"
fi

CurRegPath=$CANDLEHOME/registry
ArchTbl=$CurRegPath/archdsc.tbl
DynShl=$CANDLEHOME/bin/dynarch.shl

. $DynShl $ArchTbl

setJavaEnv $cArch

# Justify/Shift/trim any user entered uppercase to lower in the product list
typeset -lL product; typeset +lL product

if [ numPC -ge 2 ]
then

#  Start multi-product processing

#  For user supplied multiple products, strip commas 
   prods=$(print $product | tr ',' ' ')

#  We now have a list of products to process, so we invoke CandleSeed script
#  recursively for each product.  

   let product_ct=0
   let product_fail=0
   for product in $prods
   do
	  #$NLS-E$ "Multi-product support installation: $product"	
      nls_replace 'KCI0346I' $product
      $CANDLEHOME/bin/CandleSeed          \
		-h $CANDLEHOME            \
		-t $CMSNAME               \
		$product
      lastcc=$?
      if [ $lastcc -ne 0 ]
      then
	 let product_fail=product_fail+1
      fi
      let product_ct=product_ct+1
   done
   if [ product_fail -gt 0 ]
   then
	  #$NLS-E$ "Multi-product support installation failed for $product_fail of $product_ct products"
      Err_report warn "`nls_replace 'KCI0347I' $product_fail $product_ct`"
   fi
   #$NLS-E$ "Multi-product support installation completed"
   nls_replace 'KCI0348I'
   exit 0
fi

# End of multi-product processing, the rest is single product processing

# We regenerate the TZ= variable each time because of platform differences @04
envConfig=$CANDLEHOME/config/env.config
envTmp=$envConfig.tmp
if [ -f $envConfig ]
then
  cat $envConfig|sed "/TZ=/d" > $envTmp
  # Feature 15381
  #mv $envTmp $envConfig
  cp $envTmp $envConfig
  rm $envTmp
  # Feature 15381 END  

fi

# Store the timezone.  Check for TIMEZONE and ZONE since they're used in 
# different Linux distributions. @07
[ -n "$TZ" ] && print "TZ='$TZ'" >> $envConfig
[[ -z "$TZ" && -n "$TIMEZONE" ]] && print "TZ='$TIMEZONE'" >> $envConfig
[[ -z "$TZ" && -n "$ZONE" ]] && print "TZ='$ZONE'" >> $envConfig

$JREEXE -classpath $CLASSPATH $SAFE_MODE -DGlobalDbgLevel="$TRACE_LEVEL" -DInstallRASConfig="$CANDLEHOME/config/ITMConfigRAS.properties" ITMinstall.CandleSeed -ch $CANDLEHOME -t $CMSNAME $product $SKIPKDSTSNS

if [ $? != 0 ] 
then
  #$NLS-E$ "Error encountered while installing support for $product"
  Err_report fail "`nls_replace 'KCI0349E' $product`"
  exit 1
else
  exit 0
fi
