#!/bin/ksh 
#
# @14 06/22/07 LC  Add -s option for silent mode
# @14 03/08/05 LCL Add ITMinstall. to JREEXE invocations
# @13 03/02/05 LCL Add call to CandleDBConfig for or/oy agents.
# @12 01/18/05 LCL Add -g option to display parms
# @11 06/17/02 KLK get rid of -r for "really UD"
# @10 08/14/01 LCL -r for "really UD"
# @09 07/16/01 LCL Cat/atr files now handled via -S -u -t
# @08 05/03/01 LCL -p for hidden parm file input
# @07 03/09/01 LCL -o needed for tx/ps/sa agents
# @06 08/02/00 LCL Add safe mode
# @05 07/10/00 LCL Capture cmd line
# @04 04/21/00 LCL Dynarch gets binarch settings for -a
# @03 04/11/00 KLK Remove -s option for alternate network name
# @02 10/14/99 LCL Remove debug write
# @01 09/30/99 LCL Add -s option for alternate network name
#
# This script is a shell wrapper for CandleConfigAgent.class and 
# CandleConfigCMS.class. It checks command line arguments and passes them 
# to CandleConfigAgent and CandleConfigCMS classes for further processing.


Err_report() {
   case $1 in
      #$NLS-E$  "$PROGNAME warning: $2,continuing ..."
	  warn) print -u2 "$PROGNAME `nls_replace 'KCI0300I'`: $2,`nls_replace 'KCI0301I'` ..."
            sleep 2
            return
            ;;
	  #$NLS-E$ "$PROGNAME failure: $2."		
      fail) print -u2 "$PROGNAME `nls_replace 'KCI0302I'`: $2."
            exit 2
            ;;
      info) print -u2 "$PROGNAME        : $2."
            return
            ;;
	  #$NLS-E$ "$PROGNAME error: $2."			
      error) print -u2 "$PROGNAME `nls_replace 'KCI0303I'`: $2."
            exit $3
            ;;
   esac
}

# @01 add option
Use_report() {
    print -u2 "$PROGNAME -S [ -h install_dir ] [ -a arch ] -t tems_name"
    print -u2 "$PROGNAME -S [ -h install_dir ] -u -t tems_name pc"
    print -u2 "$PROGNAME -S [ -h install_dir ] [-g] -t tems_name"
    print -u2 "$PROGNAME -A [ -h install_dir ] [ -a arch ] [ -t agent_host_name ] pc"
    print -u2 "$PROGNAME -A [ -h install_dir ] [ -a arch ] [ -o domain_name ] pc"
    print -u2 "$PROGNAME -A [ -h install_dir ] [-g] pc"
	#$NLS-E$ "Use -g option to display configuration settings"
    print -u2 "`nls_replace 'KCI0304I' -g`"
    #$NLS-E$ "For Oracle Agent configuration use:"
	print -u2 "`nls_replace 'KCI0305I'`"
    print -u2 "$PROGNAME -A [ -h install_dir ] [ -o \"servername,userid,pwd\" ] or"
    exit 2
}

dbConfig() {
    typeset dbServer="" dbLogin="" dbPasswd=""
    # check options for necessary inputs (DB server, login, passwd)
    parms=$Domain
    dbServer=$(print $parms | cut -d"," -f1)
	#$NLS-E$ "Missing database server name"
    [ -n "$dbServer" ] || Err_report error "`nls_replace 'KCI0306E'`" 13
    parms=${parms#$dbServer}
    dbLogin=$(print $parms | cut -d"," -f2)
	#$NLS-E$ "Missing database login ID"
    [ -n "$dbLogin" ] || Err_report error "`nls_replace 'KCI0307E'`" 14
    parms=${parms#$dbLogin}
    dbPasswd=$(print $parms | cut -d"," -f3)
	#$NLS-E$ "Missing database login password"
    [ -n "$dbPasswd" ] || Err_report error "`nls_replace 'KCI0308E'`" 15

	#$NLS-E$ "Configuring for database server \"$dbServer\"..."
    nls_replace 'KCI0309I' \"$dbServer\"
    $CANDLEHOME/bin/CandleDBConfig -h $CANDLEHOME -s $dbServer -i $dbLogin -p $dbPasswd $product
    err_cc=$?
	#$NLS-E$ "Database server \"$dbServer\" not found"
    [ $err_cc = 99 ] && Err_report error "`nls_replace 'KCI0310E' \\"$dbServer\\"`" 99
	#$NLS-E$ "Database configuration is incomplete"
    [ $err_cc = 12 ] && Err_report error "`nls_replace 'KCI0311E'`" 12
}

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

AGENT=""
SERVER=""
AOPTION=""
HOPTION=""
GOPTION=""
OOPTION=""
POPTION=""
TOPTION=""
UOPTION=""
SOPTION=""
NOPTION=""
binArch=""
product=""
SAFE_MODE=""
err_cc=0

# @01 add SOPTION
while getopts ":SAa:h:uo:p:t:gns" OPTS
do case $OPTS in
     A) AGENT="ON"
        ;;
     S) SERVER="ON"
        ;;
     a) Arch=$OPTARG
        AOPTION="ON"
        ;;
     h) CANDLEHOME=$OPTARG
        HOPTION="ON"
        ;;
     g) GOPTION="ON"
        ;;
     p) ParmFile=$OPTARG
        POPTION="ON"
        ;;
     o) Domain=$OPTARG
        OOPTION="ON"
        ;;
     t) HostName=$OPTARG
        TOPTION="ON"
        ;;
     u) UOPTION="ON"
        ;;
     n) NOPTION="ON"
        ;;  
     s) SOPTION="ON"
        ;;       
     ?) Use_report
        ;;
   esac
done

if [ "$AGENT" = "ON" ]
then
   if [ $# -lt 2 ]
   then
      Use_report
   fi

#  For Agents, the agent/product name is always the last argument
   eval product=\${$#}

   rParms=""
#   if [ "$product" = "ud" ]  #kludge to configure um instead of ud
#   then
#      product="um"
#      rParms="-r"
#   fi

#  Check for invalid agent option combinations:
   if [ "$SERVER" = "ON" ]
   then
      Use_report
   fi

#  -A is not used to configure the CMS
   if [ "$product" = ms ]
   then
     #$NLS-E$ "Use \"-S -t TEMS_name\" to configure the Tivoli Enterprise Monitoring Server"
     Err_report fail "`nls_replace 'KCI0312E' \\"-S -t TEMS_name\\"`"
   fi

fi 

if [ "$SERVER" = "ON" ]
then
  #$NLS-E$ "-S option also requires -t TEMS_name" 	
  [ "$TOPTION" = "" ] && Err_report fail "`nls_replace 'KCI0313E' '-S' '-t TEMS_name'`" 

  if [ "$UOPTION" = "ON" ]
  then
    eval product=\${$#}
    CMSName=$HostName
  else
    product="ms"
  fi

   if [ $# -lt 3 ]
   then
      Use_report
   fi
fi

if [ "$SERVER" = "" -a "$AGENT" = "" ]
then
   Use_report
fi

[ -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 'KCI0314E' CANDLEHOME`"
fi

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

# Just for cleanliness, clean up the plugin temp dir
if [ -z "$INST_COMP_CALLPOINT_LOCATION" ]
then
rm -rf /tmp/plugin_tmp
fi

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

#d45802 some compoenents are not configurable, their configuration should not be attempted
if [ "$product" = "dd" ] ||  [ "$product" = "ax" ] ||  [ "$product" = "ui" ]
then
	#$NLS-E$ This is not a configurable product.
	Err_report error "`nls_replace 'KCI0316E'`"
fi
#Defect 17185/17898, the dynarch.shl source intermittently caused a
# shell-init error when this script is run from agent deploy. The cd 
# was added to workaround what's apparently a ksh bug. 
cd $PWD
. $DynShl $ArchTbl

setJavaEnv $cArch

if [ "$UOPTION" = "" ] && [ "$AOPTION" = "" ]
then
   setBinLibArchs $cArch $product
fi

# For "-a" option, we let dynarch find the binarch from the .ver files @04
if [ "$AOPTION" = "ON" ]
then
   cArch=$Arch
   setBinLibArchs $cArch $product
fi

gParms=""
[ "$GOPTION" = "ON" ] && gParms="-g"

# If the "-n" option is specified, don't do the stty command
# the stty command breaks when called from Java.
if [ "$NOPTION" != "ON" ]
then
	# save tty settings - we do this because Utilities.getConsolePW() will turn off the ECHO setting.
	# The method will turn it back on after it reads a password but if the user cancels out, the Java
	# program may not have the opportunity to reset the settings.  We'll make sure we do it here so
	# the settings will be the restored.
	ttySettings=$(stty -g)

	# the EXIT trap will restore the settings regardless of how the script or java program terminate.
	# hopefully, they Java program will have already reset them but we do it here to be sure they're
	# restored.
	trap "stty $ttySettings" EXIT
fi
	
umask 022                               # tighten permissions in general
[ "$product" = "ud" ] && umask 000      # ud makes files needing r/w access

if [ "$UOPTION" = "ON" ]
then
   $JREEXE -classpath $CLASSPATH $SAFE_MODE -DGlobalDbgLevel="$TRACE_LEVEL" -DInstallRASConfig="$CANDLEHOME/config/ITMConfigRAS.properties" ITMinstall.CandleConfigAgent -ch $CANDLEHOME -u $CMSName $product
elif [ "$AGENT" = "ON" ]
then
  tParms=""
  oParms=""
  pParms=""
  sParms=""
  [ "$TOPTION" = "ON" ] && tParms="-t $HostName"
  [ "$OOPTION" = "ON" ] && oParms="-o $Domain"
  [ "$POPTION" = "ON" ] && pParms="-p $ParmFile"
  [ "$SOPTION" = "ON" ] && sParms="-s"

  $JREEXE -classpath $CLASSPATH $SAFE_MODE -DGlobalDbgLevel="$TRACE_LEVEL" -DInstallRASConfig="$CANDLEHOME/config/ITMConfigRAS.properties" ITMinstall.CandleConfigAgent -ch $CANDLEHOME -a $binArch $tParms $oParms $pParms $rParms $gParms $sParms $product 

  # OR/OY also needs additional config if added parms were provided.
  # if no "-o" option, we will continue CandleDBConfig
  if [[ "$product" = "or" || "$product" = "oy" ]]
  then
    if [ "$OOPTION" = "ON" ] 
    then
        dbConfig
    elif [ "$POPTION" != "ON" ]
    then
    	$CANDLEHOME/bin/CandleDBConfig $product
    fi
  fi
elif [ "$SERVER" = "ON" ]
then
  pParms=""
  [ "$POPTION" = "ON" ] && pParms="-p $ParmFile"
   $JREEXE -classpath $CLASSPATH $SAFE_MODE -DGlobalDbgLevel="$TRACE_LEVEL" -DInstallRASConfig="$CANDLEHOME/config/ITMConfigRAS.properties" ITMinstall.CandleConfigCMS -ch $CANDLEHOME -a $cArch $pParms $gParms -t $HostName
else
   Use_report
fi

# Just for cleanliness, clean up the plugin temp dir
if [ -z "$INST_COMP_CALLPOINT_LOCATION" ]
then
rm -rf /tmp/plugin_tmp
fi

exit 0
