#!/bin/ksh
#
# @03 03/08/05 LCL Add ITMinstall. to JREEXE invocations
# @02 06/03/04 LCL Add "keep" option to retain script.
# @01 05/28/04 LCL Initial release
#
# This script is a shell wrapper for CandleAgent.class and executes a desired 
# command using the product's environment settings, or (if sourced), loads
# the product's environment settings into the current shell.

# It checks command line arguments and passes them to CandleAgent.class for 
# further processing.


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

Use_report() {
    print -u2 "\n$PROGNAME [ -h install_dir ] [-k] pc [command]\n  -k (keep) suppresses deletion of the temp script\n"
    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
UsrCmd="$0 $*"
if [[ -z "$ITMCMD" ]]
then
    PROGNAME=$(basename $0)
else
    PROGNAME="$ITMCMD"
fi

SAFE_MODE=""
keep=""
arglist=$*
cmd=""

while getopts ":h:k" OPTS
do case $OPTS in
     h) CANDLEHOME=$OPTARG
        ;;
     k) keep=y
        ;;
     ?) Use_report
        ;;
   esac
done
shift $((OPTIND-1))
arglist=$*

[ $# -lt 1 ] && Use_report

product="$1";

# Add quotes in case the command or args conflict with CandleAgent args
cmd="\"$2\"";

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

#$NLS-E$ "Cannot set CANDLEHOME environment variable"
[ ! -d "$CANDLEHOME" ] && Err_report fail "`nls_replace 'KCI0403E' CANDLEHOME`"

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

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

. $DynShl $ArchTbl

setJavaEnv $cArch 2>/dev/null

setBinLibArchs $cArch $product 

$JREEXE -classpath $CLASSPATH $SAFE_MODE -DGlobalDbgLevel="$TRACE_LEVEL" -DInstallRASConfig="$CANDLEHOME/config/ITMConfigRAS.properties" ITMinstall.CandleAgent -ch $CANDLEHOME -a $cArch execute $product $cmd

#$NLS-E$ "Unable to run command - missing $CANDLEHOME/config/$product.sh"
[ -f $CANDLEHOME/config/$product.sh ] || Err_report fail "`nls_replace 'KCI0405E' $CANDLEHOME/config/$product.sh`"
#$NLS-E$ "Unable to run command - missing $CANDLEHOME/config/$product.env"
[ -f $CANDLEHOME/config/$product.env ] || Err_report fail "`nls_replace 'KCI0406E' $CANDLEHOME/config/$product.env`"

. $CANDLEHOME/config/$product.sh
ret=$?

# Option to keep the resulting product script
if [ "$keep" = "y" ]
then
  # Merge the shell script together with env stuff, discarding extra ksh line and "source"
  cat $CANDLEHOME/config/$product.sh | grep -v "^#!/bin/ksh" | grep -v "^\." >> $CANDLEHOME/config/$product.env
  rm -f $CANDLEHOME/config/$product.sh
  mv $CANDLEHOME/config/$product.env $CANDLEHOME/config/$product.shl
  chmod +x $CANDLEHOME/config/$product.shl
  #$NLS-E$ "Script created: $CANDLEHOME/config/$product.shl"
  Err_report info "`nls_replace 'KCI0407I' $CANDLEHOME/config/$product.shl`"
else
  rm -f $CANDLEHOME/config/$product.sh
  rm -f $CANDLEHOME/config/$product.env
fi
exit $ret
