#!/bin/ksh

# @02 03/08/05 LCL Add ITMinstall. to JREEXE invocations
# @01 04/11/02 LCL Add safe-mode option

###################################################################
#             (C) COPYRIGHT Candle Corporation 1999               #
#                      All Rights Reserved                        #
#                                                                 #
#       Licensed Materials - Property of Candle Corporation       #
#         Duplication or disclosure prohibited without the        #
#        expressed written consent of the Candle Corporation.     #
#                                                                 #
###################################################################

# @(#) CandleAudit script - provides a shell wrapper for invoking
# @(#) the CandleAudit class from the command line.


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

Use_report()
{
  print -u2 "$PROGNAME [ -h install_dir ] [ -c ] -l age [ LOGDAYS ]"
  print -u2 "$PROGNAME [ -h install_dir ] [ -c ] -l size [ LOGSIZE ]"
  print -u2 "$PROGNAME [ -h install_dir ] [ -c ] -l both [ LOGDAYS LOGSIZE ]"
  #$NLS-E$ "  Specify -c to print diagnostic messages to the console."
  print -u2 "  `nls_replace 'KCI0273I' -c`"
  exit 2
}

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

export PATH=/usr/bin:$PATH
if [[ -z "$ITMCMD" ]]
then
    PROGNAME=$(basename $0)
else
    PROGNAME="$ITMCMD"
fi


# Set the defaults.

UsrCmd="$0 $*"
SAFE_MODE=""
DEBUG_CON=""
EDITTYPE=""
LOGDAYS=30
LOGSIZE=1024000

while getopts :h:l:c opt
do case $opt in
  h) CANDLEHOME=$OPTARG
     CANDLEHOME=$(eval print $CANDLEHOME)  # Process ~ if it was entered.
     ;;
  l) EDITTYPE=$OPTARG
     ;;
  c) DEBUG_CON="-debugcon"
     ;;
  ?) Use_report
     ;;
  :) Use_report
     ;;
  esac
done
shift OPTIND-1

[ -n "$EDITTYPE" ] || Use_report

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

#$NLS-E$ "\"$CANDLEHOME\" is not a directory"
[ -d "$CANDLEHOME" ] || Err_report fail "`nls_replace 'KCI0274E' \\"$CANDLEHOME\\"`"

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

cd $CANDLEHOME > /dev/null 2>&1
#$NLS-E$ "could not cd to \"$CANDLEHOME\" directory"
[ $? -eq 0 ] || Err_report fail "`nls_replace 'KCI0276I' \\"$CANDLEHOME\\"`"

# Specify the arguments for CandleAudit.

case $EDITTYPE in
  age) [ $# -eq 0 ] || LOGDAYS=$1
       args="-lg age $LOGDAYS"
       ;;
  size) [ $# -eq 0 ] || LOGSIZE=$1
        args="-lg size $LOGSIZE"
        ;;
  both) if [ $# -eq 0 ]  # Use the default values.
        then
          args="-lg both $LOGDAYS $LOGSIZE"
        elif [ $# -eq 2 ]  # Use the values specified on the command line.
        then
          args="-lg both $1 $2"
        else
          #$NLS-E$ "ERROR - option \"both\" requires no args or two args"
		  Err_report info "`nls_replace 'KCI0277E' \\"both\\"`"
          Use_report
        fi
        ;;
  *) Use_report
     ;;
esac

# Add the debug option.
args="$args $DEBUG_CON"

# Ensure that required files exist.

dynShl=$CANDLEHOME/bin/dynarch.shl
#$NLS-E$ "cannot execute \"$dynShl\""
[ -x "$dynShl" ] || Err_report fail "`nls_replace 'KCI0278E'` \\"$dynShl\\""

archTbl=$CANDLEHOME/registry/archdsc.tbl
#$NLS-E$ "cannot read \"$archTbl\""
[ -r "$archTbl" ] || Err_report fail "`nls_replace 'KCI0279E'` \\"$archTbl\\""

# Determine the machine's architecture.

. $dynShl $archTbl  # dynarch.shl will determine a value for cArch.
#$NLS-E$ "could not determine the machine's architecture"
[ -n "$cArch" ] || Err_report fail "`nls_replace 'KCI0280E'`"
#$NLS-E$ "computed architecture = \"$cArch\""
Err_report info "`nls_replace 'KCI0281I' \\"$cArch\\"`"

# Determine which JRE to use (The JRE name is stored in env var JREEXE.), and set the CLASSPATH.
# Function setJavaEnv is defined in dynarch.shl.

setJavaEnv $cArch

$JREEXE -classpath $CLASSPATH $SAFE_MODE -DGlobalDbgLevel="$TRACE_LEVEL" -DInstallRASConfig="$CANDLEHOME/config/ITMConfigRAS.properties" ITMinstall.CandleAudit -ch $CANDLEHOME $args  # Run the command.

# Restore the library path.  It was saved in dynarch.shl.  The restoration must be done after
# CandleAudit has completed.

if [ -n "$saveLibPath" ]
then
  [[ "$1" = aix+([0-9]) ]] && export LIBPATH="$saveLibPath"
  [[ "$1" = hp+([0-9]) ]] && export SHLIB_PATH="$saveLibPath"
  [[ "$1" = hpi+([0-9]) ]] && export SHLIB_PATH="$saveLibPath"
  [[ "$1" = sol+([0-9]) ]] && export LD_LIBRARY_PATH="$saveLibPath"
  [[ "$1" = tsf+([0-9]) ]] && export LD_LIBRARY_PATH="$saveLibPath"
fi
