#!/bin/ksh
######################################################################
#                                                                    #
#             (C) COPYRIGHT Candle Corporation 1996                  #
#                      All Rights Reserved                           #
#                                                                    #
#        Licensed Materials - Property of Candle Corporation         #
#         Duplication or disclosure prohibited without the           #
#        expressed written consent of the Candle Corporation.        #
#                                                                    #
#                                                                    #
######################################################################
# @(#) $Header$
#
#
# This script is used to start DB agents and collectors on a given machine.
# Calling sequence is:
#
#  CandleDBConfig [-d ] [-l] [-V] [ -h CANDLEHOME] [ -s SERVER -i ID -p PW ] [ pc ] 
#
#  -h directory overrides CANDLEHOME
#  -l = New Log
#  -L = No  Log
#  -V = No Verify
#  -d = Enables debug tracing
#
#  -s = SERVER = database server
#  -i = connect-id
#  -p = password for the connecgt-id
#
#  pc = or, oy, ox,  Default=whichever single product is installed


Use_report() {
 print -u2 "$PROGNAME [-d] [-l|-L] [-V] [ -h install_dir ] [ -s SERVER -i ID -p PW ] [ pc ]"
 exit 1
}

Err_report() {
   typeset what=$1; shift
   case $what in
		#$NLS-E$ "$PROGNAME warning: $*, continuing ..."
     (warn) print -u2 "$PROGNAME `nls_replace 'KCI0380I'`: $*, `nls_replace 'KCI0381I'` ..."
            sleep 1
		#$NLS-E$ "$PROGNAME failure: $*."	
  ;; (fail) print -u2 "$PROGNAME `nls_replace 'KCI0382I'`: $*."
            exit 2
  ;; (info) print -u2 "$PROGNAME        : $*."
   esac
}                           # Err_report type comment


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

BATCH=0

while getopts ":dh:li:p:s:LV" OPT
do case $OPT in
      (h) CANDLEHOME=$OPTARG                 # reset CANDLEHOME
   ;; (d | l | L | V ) KDDOPT=$KDDOPT" -"$OPT    # pass to kddfind.sh
   ;; (x) KDDOPT=$KDDOPT" -"$OPT                 # pass to kddfind.sh
   ;; (s | i | p ) KDDOPT=$KDDOPT" -"$OPT        # pass to kddfind.sh
                   KDDOPT=$KDDOPT" "$OPTARG      # pass to kddfind.sh
                   BATCH=1
   esac
done

#$NLS-E$ 'print -u2 "  (press enter)"; read'
[[ $BATCH = 0 ]] && trap 'print -u2 "  (`nls_replace KCI0383I`)"; read' EXIT

shift OPTIND-1

PROD=$1

[[ "$PROD" = "" ]] && Use_report

if [[ "$PROD" = "ud" ]]
then
	 #$NLS-E$ "$PROD not a valid product code, use 'itmcmd config -A ud' for $PROD"
     Err_report fail "`nls_replace 'KCI0384E' $PROD 'itmcmd config -A ud'`"
fi
if [[ "$PROD" != "or" ]] && [[ "$PROD" != "oy" ]]
then
	 #$NLS-E$ "$PROD not a valid product code, valid product codes are 'or' and 'oy'"
     Err_report fail "`nls_replace 'KCI0385E' $PROD \\'or\\' \\'oy\\'`"
fi

# Locate CANDLEHOME
  [[ -d ${CANDLEHOME:=$(x=$(whence $0); cd ${x%/*}/..; pwd)} ]] ||
	 #$NLS-E$ "$CANDLEHOME not a directory"
     Err_report fail "`nls_replace 'KCI0386E' $CANDLEHOME`"
  export CANDLEHOME

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

###################################################################
#                Set common environment variables                  #
###################################################################

set -a          # auto-export the following:
  CANDLELOG=$CANDLEHOME/logs
  CANDLETBL=$CANDLEHOME/tables
  CANDLECFG=$CANDLEHOME/config
  CANDLEMISC=$CANDLEHOME/misc
  CANDLETMP=$CANDLEHOME/tmp
set +a
unset CDPATH

###################################################################
#                  Determine architecture                         #
###################################################################

# source dynarch.shl in a subshell so it does not modify my environment
cArch=`. $CANDLEHOME/bin/dynarch.shl $CANDLEHOME/registry/archdsc.tbl ; echo $cArch`

#$NLS-E$ no registry directory  
cd $CANDLEHOME/registry || Err_report fail "`nls_replace 'KCI0393E'`"

grep -l "runArch" ${PROD}*.ver 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]
then
   #$NLS-E$ Database monitoring product "'""$PROD""'" not installed	
   Err_report fail "`nls_replace 'KCI0388E' \\'$PROD\\'`"
   
fi

# temp compromise: assume all dd products runarch the same arch
  grep -l "^runArch *= *$cArch" ${PROD}*.ver | IFS=. read pack x 
  rc1=$? 
  grep -l "^runArch *= *open" ${PROD}*.ver | IFS=. read pack x 
  rc2=$? 
  if [ $rc1 -ne 0 ] && [ $rc2 -ne 0 ]
  then
	 #$NLS-E$ unsupported runtime environment: $cArch
     Err_report fail "`nls_replace 'KCI0389E' $cArch`"
  fi

#  defect 9984, not accepting 'open'
#  grep -l "^runArch *= *$cArch" ${PROD:-o[ryx]}*.ver | IFS=. read pack x ||
#      Err_report fail unsupported runtime environment: $cArch

# Defect 11188
# The line below is not returning the correct architecture that the
# product team is expecting.
#  CANDLEARCH=${pack#??}
# Hence return the correct architecture as specified by the name of
# the ver file.  e.g. oysol283.ver should result in a CANDLEARCH
# of sol283, even if this is running on a sol293 machine.

if [ $rc1 -eq 0 ]
then
   verFile=$(grep -l "^runArch *= *$cArch" ${PROD}*.ver)
else
   verFile=$(grep -l "^runArch *= *open" ${PROD}*.ver)
fi

CANDLEARCH=${verFile%%.ver}
CANDLEARCH=${CANDLEARCH##$PROD}
# Defect 11188 - End

cd $OLDPWD

id | IFS="()" read x USER x

archdir=$CANDLEHOME/$CANDLEARCH
#$NLS-E$ Directory $archdir not found
[[ -d $archdir ]] || Err_report fail "`nls_replace 'KCI0390E' $archdir`"

unset cArch pack  x archdir

export CANDLEARCH HOST=$(uname -n) USER PROD

###################################################################
#                  Set Stuff                                      #
###################################################################

# Set environment variables using YUMI config files

  Config_File=$CANDLECFG/$PROD.config

  #$NLS-E$ config file $Config_File not found, run CandleConfig -A
  if [[ -f $Config_File ]]
  then . $Config_File
  else Err_report fail "`nls_replace 'KCI0391E' $Config_File 'CandleConfig -A'`"
  fi

whence kddfind.sh 
#$NLS-E$ "kddexec.sh not executable or missing"
export PATH=$bindir:$PATH
whence kddfind.sh >/dev/null || Err_report fail "`nls_replace 'KCI0392E' kddexec.sh`"
Config_File=$CANDLECFG/$PROD.config
kddfind.sh ${KDDOPT:+$KDDOPT} $PROD
return $?
# no trailing exec so EXIT trap works
