#!/bin/ksh
###################################################################
#             (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.     #
#                                                                 #
###################################################################

# @(#) um_console - starts the UA console interface.

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) print -u2 "$PROGNAME        :  $2."
          return
          ;;
  esac
}

Use_report()
{
  print -u2 "$PROGNAME [ -h CANDLEHOME ]" 
  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
PROGNAME=$(basename $0)

# Default to the interactive version of this script
# if -c is specfied, run in non-interactive mode
# this passes anything specified in -c "command1 command2"
# to kumpcon
noninteractive=0

# binArch and libArch will be set by the function "setBinLibArchs" in dynarch.shl.
# These vars are given here simply for clarification.

binArch=""
libArch=""

while getopts :h:c: opt
do
  case $opt in
    h) CANDLEHOME=$OPTARG
       CANDLEHOME=$(eval print $CANDLEHOME)  # Process ~ if it was entered.
       ;;
    c) noninteractive=1
       command="$OPTARG"
       ;;
    ?) Use_report
       ;;
    :) Use_report
       ;;
  esac
done
shift OPTIND-1
[ $# -eq 0 ] || Use_report

#$NLS-E$ "CANDLEHOME directory not set or not specified"
[ -n "$CANDLEHOME" ] || Err_report fail "`nls_replace 'KCI0680E' CANDLEHOME`"

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

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

# Check for required scripts and text files.

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

regPath=$CANDLEHOME/registry
archTbl=$regPath/archdsc.tbl  # archTbl is needed by dynarch.shl
#$NLS-E$ "cannot read \"$archTbl\""
[ -r $archTbl ] || Err_report fail "`nls_replace 'KCI0941E' \\"$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 'KCI0942E'`"
#Err_report info "computed architecture = \"$cArch\""

# Set the bin and lib archs.  Function setBinLibArchs is defined in dynarch.shl.

setBinLibArchs $cArch um

export UAHOME=$CANDLEHOME/$binArch/um
export UALIB=$CANDLEHOME/$binArch/lib
export LIBPATH=$CANDLEHOME/$libArch/lib:$UALIB
export LD_LIBRARY_PATH=$CANDLEHOME/$libArch/lib:$UALIB
export SHLIB_PATH=$CANDLEHOME/$libArch/lib:$UALIB
export KUMP_CONSOLE_COMMAND_WAIT=5
export KUMP_RB_PATH=$UAHOME/rb
export KUM_WORK_PATH=$UAHOME/work
export KBB_RAS1=
export KUMP_META_PATH=$UAHOME/metafiles

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

kumpcon=$UAHOME/bin/kumpcon
#$NLS-E$ "cannot execute \"$kumpcon\""
[ -x $kumpcon ] || Err_report fail "`nls_replace 'KCI0944E' \\"$kumpcon\\"`"

# Execute the command non-interactively and exit with kumpcon return code
if [ $noninteractive -eq 1 ]
then
  $kumpcon $command
  exit $?
fi

# Execute the command interactively
$kumpcon

#$NLS-E$ "\nPress ENTER to exit\n"
print "\n`nls_replace 'KCI0945I' ENTER`\n"
read
