#!/bin/ksh
#
# Script displays running processes, installed products, etc
#
#     07/18/05 TR  Replace lost functionality for below change
#     06/13/05 TR  hostname -s is NOT supported on Solaris, it changes the
#                  hostname to -s, very bad
# @10 01/12/05 LCL Add product inventory dump option
# @09 10/12/04 LCL Fix cinfo menu mode missing $CANDLEHOME.
# @08 04/27/04 LCL Display platform codes (no menu)
# @07 07/06/03 LCL Fix looping when RunInfo has line with nulls.
# @06 08/06/03 LCL Make tmp files unique
# @05 06/24/02 LCL Remove RunInfo missing err msg
# @04 06/03/02 LCL Remove debug write; add header date
# @03 11/20/01 LCL Handle Linux product inventories
# @02 11/18/01 LCL Support BigK
# @01 11/18/01 LCL Initial release

PROGNAME=$(basename $0)

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
            ;;
      fail) print -u2 "$PROGNAME `nls_replace 'KCI0537I'`: $2."
            exit 2
            ;;
      info) print -u2 "$PROGNAME        : $2."
            return
            ;;
   esac
}

Use_report() {
    print -u2 "$PROGNAME [-h candle_directory] [-c product] [-i] [-t][-t product] [-r] [-s product] [-R] [-v]" 
	#$NLS-E$ "\t-c <product> Displays configuration prompts and values"
    print -u2 "\t-c <product> `nls_replace 'KCI0700I'`"
	#$NLS-E$ "\t-i Displays an inventory of installed products"
    print -u2 "\t-i `nls_replace 'KCI0701I'`"
	#$NLS-E$ "\t-t or -t <product> Extends to displays one or all of inventory of installed products"
    print -u2 "\t-t or -t <product> `nls_replace 'KCI1246I'`"
	#$NLS-E$  "\t-r Shows running processes"
    print -u2 "\t-r `nls_replace 'KCI0702I'`"
	#$NLS-E$ "\t-s <product> Displays configuration parameters and settings"
    print -u2 "\t-s <product> `nls_replace 'KCI0703I'`"
	#$NLS-E$ "\t-R Shows running processes, after updating a tracking database"
    print -u2 "\t-R `nls_replace 'KCI0704I'`"
	#$NLS-E$ "\t-v Shows the installed CD versions in this CandleHome"
    print -u2 "\t-v `nls_replace 'KCI0705I' CandleHome`"
	#$NLS-E$ "\t-p <product> Shows associated platform codes for the specified product"
    print -u2 "\t-p <product> `nls_replace 'KCI0706I'`"
	#$NLS-E$ "\t-d Dumps an inventory of installed products"
    print -u2 "\t-d `nls_replace 'KCI0707I'`"
    exit 2
}

# Parse the given version file to get some information: 
# [platform] [version] [build] [installdate]
# $1: name of this version file 
# $2: product code
# $3: product name
Parse_ver_file() {
					
    typeset vFile=$1
    typeset pcL=$2
    typeset name=$3

    printf "%-5s%-58s" "$pcL" "$name"

    platform=${vFile#$pcL}
    platform=${platform%.ver}
    printf "%-8s" "$platform"
    
    version=$(grep "^VRMF =" $vFile|cut -f3 -d" ")
    if [ -z "$version" ]
    then
        version=$(grep "^ver =" $vFile|cut -f3 -d" ")
	release=$(grep "^rel =" $vFile|cut -f3 -d" ")
	printf "%-14s" "$version Rel: $release"
    else
        version1=$(print $version| cut -c 1,2)
        version2=$(print $version| cut -c 3,4)
        version3=$(print $version| cut -c 5,6)
        version4=$(print $version| cut -c 7,8)
        printf "%-14s" "$version1.$version2.$version3.$version4"
    fi
          
    build=$(grep "^build =" $vFile|cut -f3 -d" ")
    if [ -z "$build" ]
    then
        printf "%-16s" "-"
    else
        printf "%-16s" "$build"
    fi

    installDate=$(grep "^installDate =" $vFile|cut -f2 -d"|")
    if [ -n "$installDate" ]
    then
        installDateLen=${#installDate}
        let "installDateLen-=3"
        installDate=$(print $installDate|cut -c-$installDateLen)
        #installDate=$(date --date="1970-01-01 UTC $installDate sec" +"%Y%m%d %H%M")
        #printf "%-16s" "$installDate"
        
        # For the cross-platform reason (linux, AIX, HP, SunOS), 
        # we can not use the "date" command (or any other command) 
        # to translate a unix time to a local time on all of target platforms.
        # So we translate it by ourself. 
        typeset -i timeZone=`ComputeTimeZone`
        printf "%-16s" "`ConverteToDate $installDate $timeZone`"
    else
        printf "%-16s" "-"
    fi
    printf "\n"
}

# Judge the given year whether or not is a leap year.
# $1: the year you want to judge.
IsLeapYear() {
    typeset -i year=$1
    echo $((year%4==0 && year%100!=0 || year%400==0))
}

# Translate a number to a date, 
# the sum of days from Jan.1 to this day equals to this number.
DayToDate() {
    typeset -i sumDay=$1
    typeset -i year=$2

    typeset -i month=1
    typeset -i tempDay=31

    while [ $tempDay -lt $sumDay ]
    do
      sumDay=$(($sumDay - $tempDay))
      month=$month+1
      if [ $month -eq 3 ] || [ $month -eq 5 ] || [ $month -eq 7 ] || [ $month -eq 8 ] || [ $month -eq 10 ] || [ $month -eq 12 ]
      then
          tempDay=31
      elif [ $month -eq 2 ]
      then
          if [ `IsLeapYear $year` -eq "1" ]
          then
              tempDay=29
          else
              tempDay=28
          fi
      else
          tempDay=30
      fi      
    done

    printf "%02d%02d" $month $sumDay
}

# We use "date" command to get the local time zone.
ComputeTimeZone() {
    typeset utcTime=$(date -u +"%H %M")
    typeset localTime=$(date +"%H %M")

    typeset -i utcHour=$(print $utcTime|cut -f1 -d" ")
    typeset -i utcMinute=$(print $utcTime|cut -f2 -d" ")
    typeset -i localHour=$(print $localTime|cut -f1 -d" ")
    typeset -i localMinute=$(print $localTime|cut -f2 -d" ")
    typeset -i timeDiff=0

    if [ $utcMinute -le $localMinute ]
    then
        timeDiff=$(($localHour - $utcHour))
    else
        timeDiff=$(($localHour - $utcHour))
        timeDiff=$(($timeDiff - 1))
    fi

    echo $timeDiff
}

# Converte a unix second time to a local time.
ConverteToDate() {
    typeset -i installDate=$1
    typeset -i timeZone=$2

    typeset -i year=1970
    typeset -i hour=0
    typeset -i minute=0

    # we must calculate the time zone of the local host.
    installDate=$(($installDate + $timeZone * 3600))

    # compute days of the given second time.
    typeset -i secondDis=$(($installDate % 86400))
    typeset -i dayDis=$(($installDate - $secondDis))/86400

    # compute hours and minutes of the given second time.
    minute=$(($secondDis % 3600))
    hour=$(( ($secondDis - $minute) / 3600))
    minute=$(($minute / 60))

    
    # compute years of the given second time.
    typeset -i tempDay=365
    
    while [ $dayDis -ge $tempDay ]
    do
      dayDis=$(($dayDis - $tempDay))
      year=$(($year + 1))
      if [ `IsLeapYear $year` -eq "1" ]
      then
          tempDay=366
      else
          tempDay=365
      fi      
    done
    dayDis=$(($dayDis + 1))
    
    printf "%d%s %02d%02d" $year `DayToDate $dayDis $year` $hour $minute
}

###################################################################
#                         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:/usr/sbin:/usr/ucb:$PATH
Option="inventory"
Mode=""
Menu="yes"
g11n="no"
[ -z "$(whoami 2>/dev/null)" ] && alias whoami='id | cut -d"(" -f2 | cut -d")" -f1'

typeset -i ver=0 vr=0


# We want the "-t" parameter can follow a variable or not.
# But, we can not use the getopts method to do it. 
# Because the getopts can not support this feature.
# So, we do this by ourself.
while [ $# -ne 0 ]
do
  case "$1" in
    -c) 
        shift
        if [ $# -eq 0 ]
        then
            # error, for example: "cinfo -c"
            Use_report
        else
            case "$1" in
              -*)
                 # error, for example: "cinfo -c -h /opt/IBM/ITM"
                 Use_report
                 ;;
               *)
                 Option="config"
                 Menu="no"
                 prod="$1"
               ;;
            esac
        fi
        ;;
     -h)
        shift
        if [ $# -eq 0 ]
        then
            # error, for example: "cinfo -h"
            Use_report
        else
            case "$1" in
              -*)
                 # error, for example: "cinfo -h -c mq"
                 Use_report
                 ;;
               *)
                CANDLEHOME=$1
                 ;;
            esac
        fi 
        ;;
     -i)
        Option="inventory"
        Menu="no"
        ;;        
     -t)
        Option="inventoryEX"
        Menu="no"
        shift
        if [ $# -eq 0 ]
        then
            # support this, for example: "cinfo -t"
            continue 
        else
            case "$1" in
              -*)
                # support this, for example: "cinfo -t -h /opt/IBM/ITM"
                prod=""
                continue 
                ;;
               *)
                # support this, for example: "cinfo -t mq" 
                prod="$1"
                ;;
            esac
        fi
        ;; 
      -d)
        Option="inventoryDump"
        Menu="no"
        ;;       
      -r)
        Option="running" 
        Menu="no"
        ;;
      -s)
        shift
        if [ $# -eq 0 ]
        then
            # error, for example: "cinfo -s"
            Use_report
        else
            case "$1" in
              -*)
                 # error, for example: "cinfo -s -h /opt/IBM/ITM"
                 Use_report
                 ;;
               *)
                 Option="settings"
                 Menu="no"
                 prod=$1
                 ;;
            esac
        fi
        ;;
      -R)
        Option="update"
        Menu="no"
        ;;
      -v)
        Option="version"
        Menu="no"
        ;;
      -p)
        shift
        if [ $# -eq 0 ]
        then
            # error, for example: "cinfo -p"
            Use_report
        else
            case "$1" in
              -*)
                 # error, for example: "cinfo -p -h /opt/IBM/ITM"
                 Use_report
                 ;;
               *)
                 Option="platform"
                 Menu="no"
                 prod=$1
                 ;;
            esac
        fi
        ;;
     -g)
        g11n="yes"
        ;;
     --help)
        Use_report
        ;;
      ?)
        Use_report
        ;;
  esac
  shift
done

if [ $g11n = "no" ]
then
   export LC_ALL=C
   export LANG=C
fi

[ -n "$CANDLEHOME" ] || CANDLEHOME=$(cd $(dirname $0) ; dirname $(pwd) )
export 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

# Get installer version
ciVFile=cienv.ver
typeset curRel="" curVer="" regFile=""

regFile=$CANDLEHOME/registry/$ciVFile
#$NLS-E$ "could not read file: $regFile"
[ -r $regFile ] || Err_report fail "`nls_replace 'KCI0708E' $regFile`"

# Get the version and release numbers.

curVrmf=$(grep "^VRMF =" $regFile | cut -d"=" -f2)
if [ -z "$curVrmf" ]
then
    curVer=$(grep "^ver =" $regFile | cut -d"=" -f2)
	#$NLS-E$ "could not determine the version number from $regFile"
    [ -n "$curVer" ] || Err_report fail "`nls_replace 'KCI0709E' $regFile`"
    curRel=$(grep "^rel =" $regFile | cut -d"=" -f2)
	#$NLS-E$ "could not determine the release number from $regFile"
    [ -n "$curRel" ] || Err_report fail "`nls_replace 'KCI0710E' $regFile`"
else
    curVer1=$(print $curVrmf| cut -c 1,2)
    curVer2=$(print $curVrmf| cut -c 3,4)
    curVer3=$(print $curVrmf| cut -c 5,6)
    curVer4=$(print $curVrmf| cut -c 7,8)
fi


print " " 
print "*********** $(date) ******************"
#$NLS-E$ "User: $(whoami) Groups: $(groups)"
print "`nls_replace 'KCI0711I' $(whoami) "$(groups)"`"
if [ -z "$curVrmf" ]
then
    #$NLS-E$ "Host name : $(hostname)\t Installer Lvl:$curVer /$curRel" 
    print "`nls_replace 'KCI0712I'` : $(hostname)\t `nls_replace 'KCI0713I'` Lvl:$curVer /$curRel" 
else
   #$NLS-E$ "Host name : $(hostname)\t Installer Lvl:$curVer1.$curVer2.$curVer3.$curVer4"
    print "`nls_replace 'KCI0712I'` : $(hostname)\t `nls_replace 'KCI0713I'` Lvl:$curVer1.$curVer2.$curVer3.$curVer4"
fi
print "CandleHome: $CANDLEHOME" 
#suojx
if [ "$Option" = "inventoryEX" ]
then
    #$NLS-E$ "Version Format: VV.RM.FF.II (V: Version; R: Release; M: Modification; F: Fix; I: Interim Fix)"
    print "`nls_replace 'KCI1236I'`"
fi
print "***********************************************************" 

infoTmp=$CANDLEHOME/tmp/runinfo.tmp_$$
infoTmp2=$CANDLEHOME/tmp/runinfo.tmp2_$$
infoTmp3=$CANDLEHOME/tmp/runinfo.tmp3_$$
configData=$CANDLEHOME/config/.ConfigData
RunInfo=$configData/RunInfo
RunInfo_Description=$configData/RunInfo_Description
RunInfo_Description_bak=$CANDLEHOME/tmp/runinfo_description.bak_$$
RunInfo_Description_tmp=$CANDLEHOME/tmp/runinfo_description.tmp_$$
docLoc=$CANDLEHOME/doc
regDir=$CANDLEHOME/registry

# Make sure we always have a RunInfo file
[ -a "$RunInfo" ] || print "FIELDSEP=|" > $RunInfo
#$NLS-E$ "RunInfo database is missing"
[ -r "$RunInfo" ] || Err_report fail "`nls_replace 'KCI0714E' RunInfo`"

# Display menu if no options were used
if [ "$Menu" = "yes" ]
then
  while true
  do
    print "\n    -- CINFO Menu --"
	#$NLS-E$ " 1) Show products installed in this CandleHome"
    print "1) `nls_replace 'KCI0715I'`"
	#$NLS-E$ " 2) Show which products are currently running"
    print "2) `nls_replace 'KCI0716I'`"
	#$NLS-E$ " 3) Show configuration settings"
    print "3) `nls_replace 'KCI0717I'`"
	#$NLS-E$ " 4) Show installed CD release versions"
    print "4) `nls_replace 'KCI0718I'`"
	#$NLS-E$ X) Exit CINFO
    print "5) `nls_replace 'KCI0719I' CINFO`"
    read Response opt
    case "$Response" in
      1) $CANDLEHOME/bin/cinfo -i
         ;;
      2) $CANDLEHOME/bin/cinfo -r
         ;;
      3) [ -z "$opt" ] && opt="all"
	 $CANDLEHOME/bin/cinfo -c $opt
         ;;
      4) $CANDLEHOME/bin/cinfo -v
         ;;
      x|X|5) exit
         ;;
    esac
  done
fi

# Option to check product inventory
# - list by product code (excluding supplemental products), then by OS/platform

if [ "$Option" = "inventory" ]
then
  cd $regDir
  rm -f $infoTmp2
  touch $infoTmp2
  #$NLS-E$ "...Product inventory"
  print "`nls_replace 'KCI0720I'`"
  for pc in $(ls *.ver|grep -v "^ci"|grep -v mvs|grep -v os2|grep -v winnt|grep -v unix|grep -v os400)
  do
    pc=$(print $pc|cut -c 1-2)
    print $pc >> $infoTmp2
  done
  sort -u -o $infoTmp $infoTmp2
  # abstract      SVT: cinfo product information should be consistent
  # need to include data only portions in cinfo output (tms, tpw, tps) components
  listOfOs="aix hp sol l t"
  while read pc
  do
    name=$(grep "^$pc" $regDir/proddsc.tbl|head -1|cut -f2 -d"|")
    print "\n$pc\t$name"
    for OS in $listOfOs
    do
      if [ -n "$(ls ${pc}$OS*.ver 2>/dev/null)" ]
      then
        for vFile in $(ls ${pc}$OS*.ver)
        do
          platform=${vFile#$pc}
          platform=${platform%.ver}
          version=$(grep "^VRMF =" $vFile|cut -f3 -d" ")
          if [ -z "$version" ]
          then
            version=$(grep "^ver =" $vFile|cut -f3 -d" ")
            release=$(grep "^rel =" $vFile|cut -f3 -d" ")
            print "\t $platform\t Version: $version Rel: $release"
          else
            version1=$(print $version| cut -c 1,2)
            version2=$(print $version| cut -c 3,4)
            version3=$(print $version| cut -c 5,6)
            version4=$(print $version| cut -c 7,8)
            print "\t $platform\t Version: $version1.$version2.$version3.$version4"
          fi
        done
      fi
    done
  done < $infoTmp
  rm -f $infoTmp $infoTmp2
  exit
fi

# Option extend to check product inventory

if [ "$Option" = "inventoryEX" ]
then
  cd $regDir
  isHaveProduct=""
  isHavePrerequisite=""
  isHaveSupport=""  
  #$NLS-E$ "...Product inventory"
  print "`nls_replace 'KCI0720I'`"
  
  # Deal with a user input like this: "cinfo -t"
  if [ "$prod" = "" ]
  then
          rm -f $infoTmp2
          touch $infoTmp2
	  for pc in $(ls *.ver|grep -v "^ci"|grep -v mvs|grep -v os2|grep -v winnt|grep -v unix|grep -v os400)
	  do
	    pc=$(print $pc|cut -c 1-2)
	    print $pc >> $infoTmp2
	  done
	  sort -u -o $infoTmp $infoTmp2
	  
	  # List the information of products.
          # PC   PRODUCT DESC       PLAT    VER        BUILD       INSTALL DATE

	  listOfOs="aix hp sol l"

	  while read pc
	  do
	    name=$(grep "^$pc" $regDir/proddsc.tbl|head -1|cut -f2 -d"|")
	    for OS in $listOfOs
	    do
	      if [ -n "$(ls ${pc}$OS*.ver 2>/dev/null)" ]
	      then
	        for vFile in $(ls ${pc}$OS*.ver)
	        do
                  if [ "$isHaveProduct" = "" ]
                  then
                      isHaveProduct="Yes"

                      #$NLS-E$ "PC"
                      printf "%-5s" "`nls_replace 'KCI1237I'`"

                      #$NLS-E$ "PRODUCT DESC"
                      printf "%-58s" "`nls_replace 'KCI1238I'`"

                      #$NLS-E$ "PLAT"
                      printf "%-8s" "`nls_replace 'KCI1239I'`"

                      #$NLS-E$ "VER"
                      printf "%-14s" "`nls_replace 'KCI1240I'`"

                      #$NLS-E$ "BUILD"
                      printf "%-16s" "`nls_replace 'KCI1241I'`"

                      #$NLS-E$ "INSTALL DATE"
                      printf "%-16s" "`nls_replace 'KCI1242I'`"
                      printf "\n\n" 
                  fi
		  Parse_ver_file "$vFile" "$pc" "$name"
	        done
	      fi
	    done
	  done < $infoTmp
	
	  # List the infomation of application supports.
          # PC   APPLICATION SUPPORT DESC       PLAT    APP VER        BUILD       INSTALL DATE

	  listOfSupport="t"

	  while read pc
	  do
            if [ "$pc" = "ms" ] || [ "$pc" = "cj" ] || [ "$pc" = "cq" ] || [ "$pc" = "cw" ]
            then
                continue
            fi
	    name=$(grep "^$pc" $regDir/proddsc.tbl|head -1|cut -f2 -d"|")
	    for SP in $listOfSupport
	    do
	      if [ -n "$(ls ${pc}$SP*.ver 2>/dev/null)" ]
	      then
	        for vFile in $(ls ${pc}$SP*.ver)
	        do
                  if [ "$isHaveSupport" = "" ]
                  then
                      isHaveSupport="Yes"
          
                      print "\n"
                      #$NLS-E$ "PC"
                      printf "%-5s" "`nls_replace 'KCI1237I'`"

                      #$NLS-E$ "APPLICATION SUPPORT DESC"
                      printf "%-58s" "`nls_replace 'KCI1244I'`"

                      #$NLS-E$ "PLAT"
                      printf "%-8s" "`nls_replace 'KCI1239I'`"

                      #$NLS-E$ "APP VER"
                      printf "%-14s" "`nls_replace 'KCI1243I'`"
 
                      #$NLS-E$ "BUILD"
                      printf "%-16s" "`nls_replace 'KCI1241I'`"
  
                      #$NLS-E$ "INSTALL DATE"
                      printf "%-16s" "`nls_replace 'KCI1242I'`"
                      printf "\n\n"
                  fi
                  Parse_ver_file "$vFile" "$pc" "$name"
	        done
	      fi
	    done
	  done < $infoTmp  
	
	  rm -f $infoTmp $infoTmp2
	  exit

     # Deal with a user input like this: "cinfo -t pc"
     else
          # List the information of the given product.
          # PC   PRODUCT DESC       PLAT    VER        BUILD       INSTALL DATE

	  pc=$prod
	  listOfOs="aix hp sol l"

	  name=$(grep "^$pc" $regDir/proddsc.tbl|head -1|cut -f2 -d"|")
	  for OS in $listOfOs
	  do
	      if [ -n "$(ls ${pc}$OS*.ver 2>/dev/null)" ]
	      then
	        for vFile in $(ls ${pc}$OS*.ver)
	        do
                  if [ "$isHaveProduct" = "" ]
                  then
                      isHaveProduct="Yes"
                     
                      printf "\n"
                      #$NLS-E$ "PC"
                      printf "%-5s" "`nls_replace 'KCI1237I'`"

                      #$NLS-E$ "PRODUCT DESC"
                      printf "%-58s" "`nls_replace 'KCI1238I'`"

                      #$NLS-E$ "PLAT"
                      printf "%-8s" "`nls_replace 'KCI1239I'`"

                      #$NLS-E$ "VER"
                      printf "%-14s" "`nls_replace 'KCI1240I'`"

                      #$NLS-E$ "BUILD"
                      printf "%-16s" "`nls_replace 'KCI1241I'`"

                      #$NLS-E$ "INSTALL DATE"
                      printf "%-16s" "`nls_replace 'KCI1242I'`"
                      printf "\n\n"
                  fi
	          Parse_ver_file "$vFile" "$pc" "$name"
	        done
	      fi
	  done
	  
	  # List the information of prerequisite products.
          # PC   PREREQUISITE DESC      PLAT    VER        BUILD       INSTALL DATE

	  for OS in $listOfOs
	  do
	    if [ -n "$(ls ${pc}$OS*.ver 2>/dev/null)" ]
            then
	      for vFile in $(ls ${pc}$OS*.ver)
	      do
		for pre in $(grep "^preReq" $vFile | grep -v "none" |cut -f1 -d"|"|cut -f3 -d" ")
		do
                  if [ "$isHavePrerequisite" = "" ]
                  then
                      isHavePrerequisite="Yes"

                      printf "\n"
                      #$NLS-E$ "PC"
                      printf "%-5s" "`nls_replace 'KCI1237I'`"

                      #$NLS-E$ "PREREQUISITE DESC"
                      printf "%-58s" "`nls_replace 'KCI1245I'`"

                      #$NLS-E$ "PLAT"
                      printf "%-8s" "`nls_replace 'KCI1239I'`"

                      #$NLS-E$ "VER"
                      printf "%-14s" "`nls_replace 'KCI1240I'`"

                      #$NLS-E$ "BUILD"
                      printf "%-16s" "`nls_replace 'KCI1241I'`"

                      #$NLS-E$ "INSTALL DATE"
                      printf "%-16s" "`nls_replace 'KCI1242I'`"
                      printf "\n\n"
                  fi
		  prePC=$(print $pre|cut -c 1-2)
		  name=$(grep "^$prePC" $regDir/proddsc.tbl|head -1|cut -f2 -d"|")
		  Parse_ver_file "$pre.ver" "$prePC" "$name"
		done
	      done
	    fi
          done

	  # List the infomation of application supports.
          # PC   APPLICATION SUPPORT DESC      PLAT    APP VER        BUILD       INSTALL DATE

	  listOfSupport="t"

	  if [ "$pc" = "ms" ] || [ "$pc" = "cj" ] || [ "$pc" = "cq" ] || [ "$pc" = "cw" ]
          then
            if [ "$pc" = "ms" ]
            then
		suffix="ms"
	    elif [ "$pc" = "cj" ]
            then
		suffix="pd"
	    elif [ "$pc" = "cq" ]
            then
		suffix="ps"
	    elif [ "$pc" = "cw" ]
            then
		suffix="pw"
	    fi
            if [ -n "$(ls *${suffix}.ver 2>/dev/null)" ]
            then
                for vFile in $(ls *${suffix}.ver)
                do
                  if [ "$isHaveSupport" = "" ]
                  then
                      isHaveSupport="Yes"

                      print "\n"
                      #$NLS-E$ "PC"
                      printf "%-5s" "`nls_replace 'KCI1237I'`"

                      #$NLS-E$ "APPLICATION SUPPORT DESC"
                      printf "%-58s" "`nls_replace 'KCI1244I'`"

                      #$NLS-E$ "PLAT"
                      printf "%-8s" "`nls_replace 'KCI1239I'`"

                      #$NLS-E$ "APP VER"
                      printf "%-14s" "`nls_replace 'KCI1243I'`"

                      #$NLS-E$ "BUILD"
                      printf "%-16s" "`nls_replace 'KCI1241I'`"

                      #$NLS-E$ "INSTALL DATE"
                      printf "%-16s" "`nls_replace 'KCI1242I'`"
                      printf "\n\n"
                  fi
                  SP=$(print $vFile|cut -c 1-2)
                  name=$(grep "^$SP" $regDir/proddsc.tbl|head -1|cut -f2 -d"|")
                  Parse_ver_file "$vFile" "$SP" "$name"
                done
            fi
	  else
	    name=$(grep "^$pc" $regDir/proddsc.tbl|head -1|cut -f2 -d"|")
	    for SP in $listOfSupport
	    do
	      if [ -n "$(ls ${pc}$SP*.ver 2>/dev/null)" ]
	      then
	        for vFile in $(ls ${pc}$SP*.ver)
	        do
                  if [ "$isHaveSupport" = "" ]
                  then
                      isHaveSupport="Yes"

                      print "\n"
                      #$NLS-E$ "PC"
                      printf "%-5s" "`nls_replace 'KCI1237I'`"

                      #$NLS-E$ "APPLICATION SUPPORT DESC"
                      printf "%-58s" "`nls_replace 'KCI1244I'`"

                      #$NLS-E$ "PLAT"
                      printf "%-8s" "`nls_replace 'KCI1239I'`"
                  
                      #$NLS-E$ "APP VER"
                      printf "%-14s" "`nls_replace 'KCI1243I'`"

                      #$NLS-E$ "BUILD"
                      printf "%-16s" "`nls_replace 'KCI1241I'`"

                      #$NLS-E$ "INSTALL DATE"
                      printf "%-16s" "`nls_replace 'KCI1242I'`"
                      printf "\n\n"
                  fi
		  Parse_ver_file "$vFile" "$pc" "$name"
	        done
	      fi
	    done
	  fi
        
          if [ "$isHaveProduct" = "" ] && [ "$isHaveSupport" = "" ] && [ "$isHavePrerequisite" = "" ]
          then
          		 #$NLS-E$ "The product $pc is not installed."
               print "`nls_replace 'KCI1247I' $pc`"
               exit 1
          fi
        
	  exit	  
    fi
fi

# Option to dump product inventory
if [ "$Option" = "inventoryDump" ]
then
  cd $regDir
  rm -f $infoTmp2
  touch $infoTmp2
  #$NLS-E$ "...Dump product inventory"
  print "`nls_replace 'KCI0721I'`"
  print "\"ProdCode\",\"Description\",\"Platform\",\"Version\",\"Release\""
  # Omit the installer 
  for pc in $(ls *.ver|grep -v "^ci")
  do
    pc=$(print $pc|cut -c 1-2)
    print $pc >> $infoTmp2
  done
  sort -u -o $infoTmp $infoTmp2
  listOfOs="aix hp sol mvs win os400 unix l"
  while read pc
  do
    name=$(grep "^$pc" $regDir/proddsc.tbl|head -1|cut -f2 -d"|")
    for OS in $listOfOs
    do
      if [ -n "$(ls ${pc}$OS*.ver 2>/dev/null)" ]
      then
        for vFile in $(ls ${pc}$OS*.ver)
        do
          platform=${vFile#$pc}
          platform=${platform%.ver}
	  version=$(grep "^VRMF =" $vFile| awk '{ printf("%2s%2s%2s%2s\n", substr($3,1,2), substr($3,3,2),substr($3,5,2),substr($3,7,2)) }')
	  if [ -z "$version" ]
	  then
            version=$(grep "^ver =" $vFile| awk '{ printf("%02s%02s%02s000\n", substr($3,1,1), substr($3,2,1), substr($3,3,1)) }')
            release=$(grep "^rel =" $vFile|cut -f3 -d" ")
            print -n "\n\"$pc\",\"$name\""
            print ",\"$platform\",\"$version\",\"$release\""
	  else
            release=$(grep "^rel =" $vFile|cut -f3 -d" ")
            print -n "\n\"$pc\",\"$name\""
            print ",\"$platform\",\"$version\",\"$release\""
	  fi
        done
      fi
    done
  done < $infoTmp
  rm -f $infoTmp $infoTmp2
  exit
fi

# Option to update running processes

if [ "$Option" = "update" ]
then
  if [ -z "$(grep "\|" $RunInfo|grep -v "FIELDSEP=")" ] 
  then
    #$NLS-E$ "...no known processes are running"
    print "`nls_replace 'KCI0722I'`"
    exit
  fi
  
  cp $RunInfo $infoTmp
  
  grep "\|" $RunInfo|grep -v "FIELDSEP=" | while read line
  do
    host=$(print $line|cut -f1 -d"|"|cut -f1 -d".")
    prod=$(print $line|cut -f2 -d"|")
    pid=$(print $line|cut -f3 -d"|")
    id=$(print $line|cut -f4 -d"|")
    localhost=$(print `hostname` | cut -f1 -d".")
   
    #hostname -s is NOT supported on Solaris, can't use, it changes the hostname to -s
    #if [[ "$host" = "$(hostname)" || "$host" = "$(hostname -s)" ]]
    if [ "$host" = "$localhost" ]
    then
      if [ -z "$(ps -p $pid|grep -v TTY)" ]
      then
        grep -v "$line" $infoTmp > $infoTmp2
        cp $infoTmp2 $infoTmp
        rm -rf $RunInfo_Description_tmp
        cp $RunInfo_Description $RunInfo_Description_bak
        grep -v "$pid.description" $RunInfo_Description > $RunInfo_Description_tmp
        cp $RunInfo_Description_tmp $RunInfo_Description
      fi
    fi
  done
  cp $infoTmp $RunInfo
  
  rm -f $infoTmp $infoTmp2
  Option="running"
fi

# Option to check running processes

if [ "$Option" = "running" ]
then
  if [ -z "$(grep "\|" $RunInfo|grep -v "FIELDSEP=")" ] 
  then
    #$NLS-E$ "...no known processes are running"
    print "`nls_replace 'KCI0722I'`"
    exit
  fi
  
  cp $RunInfo $infoTmp
  
  # First run through get maximum column lengths
  # Thsi code runs through the RunInfo table and
  # calculates the length of the fields.
  # Be sure to change this code if you are also
  # changing the code below that reads RunInfo again
  #$NLS-E$ "Host"
  hosttitle="`nls_replace 'KCI1209I'`"
  #$NLS-E$ "Prod"
  prodtitle="`nls_replace 'KCI1210I'`"
  #$NLS-E$ "PID"
  pidtitle="`nls_replace 'KCI1211I'`"
  #$NLS-E$ "Owner"
  ownertitle="`nls_replace 'KCI1212I'`"
  #$NLS-E$ "Start"
  starttitle="`nls_replace 'KCI1213I'`"
  #$NLS-E$ "ID"
  idtitle="`nls_replace 'KCI1214I'`"
  #$NLS-E$ "..Status"
  statustitle="`nls_replace 'KCI1215I'`"

  let hostlen=${#hosttitle}
  let prodlen=${#prodtitle}
  let pidlen=${#pidtitle}
  let ownerlen=${#ownertitle}
  let startlen=${#starttitle}
  let idlen=${#idtitle}
  let statuslen=${#statustitle}

  # This while..do..done loop pre-reads the entire RunInfo
  # file and works out maximum line lengths for formatting
  while read line
  do
    case $line in
    FIELDSEP=*)
       continue ;;
    esac
    host=$(print $line|cut -f1 -d"|"|cut -f1 -d".")
    prod=$(print $line|cut -f2 -d"|")
    pid=$(print $line|cut -f3 -d"|")
    id=$(print $line|cut -f4 -d"|")
    localhost=$(print `hostname` | cut -f1 -d".")
    status=""
    owner="     "
    start="     "
    #hostname -s is NOT supported on Solaris, can't use, it changes the hostname to -s
    #if [[ "$host" = "$(hostname)" || "$host" = "$(hostname -s)" ]]
    if [ "$host" = "$localhost" ]
    then
      status="running"
      if [ -z "$(ps -p $pid|grep -v TTY)" ]
      then
	    #$NLS-E$ "** process not running **"
        status="`nls_replace 'KCI0723I'`"
      else
	# try and get owner and start time info
	info=$(ps -ef|grep -v grep|grep " $pid ")
	owner=$(print $info|cut -f1 -d" ")
	start=$(print $info|cut -f5 -d" ")
	    #$NLS-E$ "..running"
        status="`nls_replace 'KCI0724I'`"
      fi
    fi
    [ ${#host} -gt $hostlen ] && let hostlen=${#host}
    [ ${#prod} -gt $prodlen ] && let prodlen=${#prod}
    [ ${#pid} -gt $pidlen ] && let pidlen=${#pid}
    [ ${#owner} -gt $ownerlen ] && let ownerlen=${#owner}
    [ ${#start} -gt $startlen ] && let startlen=${#start}
    [ ${#id} -gt $idlen ] && let idlen=${#id}
    [ ${#status} -gt $statuslen ] && let statuslen=${#status}
  done <$RunInfo

  typeset -L${hostlen} hosttitle
  typeset -L${prodlen} prodtitle
  typeset -L${pidlen} pidtitle
  typeset -L${ownerlen} ownertitle
  typeset -L${startlen} starttitle
  typeset -L${idlen} idtitle
  typeset -L${statuslen} statustitle

  print "$hosttitle  $prodtitle  $pidtitle  $ownertitle  $starttitle  $idtitle  $statustitle"

  # Now we have the max line lengths set, run through the RunInfo
  # file again and actually print the data this time.  
  grep "\|" $RunInfo|grep -v "FIELDSEP=" | while read line
  do
    host=$(print $line|cut -f1 -d"|"|cut -f1 -d".")
    prod=$(print $line|cut -f2 -d"|")
    pid=$(print $line|cut -f3 -d"|")
    id=$(print $line|cut -f4 -d"|")
    localhost=$(print `hostname` | cut -f1 -d".")
    status=""
    owner="     "
    start="     "
    #hostname -s is NOT supported on Solaris, can't use, it changes the hostname to -s
    #if [[ "$host" = "$(hostname)" || "$host" = "$(hostname -s)" ]]
    if [ "$host" = "$localhost" ]
    then
      status="running"
      if [ -z "$(ps -p $pid|grep -v TTY)" ]
      then
	    #$NLS-E$ "** process not running **"
        status="`nls_replace 'KCI0723I'`"
      else
        # try and get owner and start time info
        info=$(ps -ef|grep -v grep|grep " $pid ")
        owner=$(print $info|cut -f1 -d" ")
        start=$(print $info|cut -f5 -d" ")
	    #$NLS-E$ "..running"
        status="`nls_replace 'KCI0724I'`"
      fi
    fi
    typeset -L${hostlen} host
    typeset -L${prodlen} prod
    typeset -L${pidlen} pid
    typeset -L${ownerlen} owner
    typeset -L${startlen} start
    typeset -L${idlen} id
    print "$host  $prod  $pid  $owner  $start  $id  $status"
  done
  
  rm -f $infoTmp $infoTmp2
  exit
fi

# Option to display config settings from k<pp>env

if [ "$Option" = "settings" ]
then
  cd $configData
  if [ "$prod" = "all" ]
  then
    prod=""
    for env in $(ls k*env)
    do
      prd=${env#k}
      prdlist="$prdlist ${prd%env}"
    done
  else
    prdlist=$prod
  fi
  for prod in $prdlist
  do
    envFile=k${prod}env
	#$NLS-E$ "Settings for $prod not found"
    [ -r $envFile ] || Err_report fail "`nls_replace 'KCI0725E' $prod`"

	#$NLS-E$ "\nConfiguration\t Parameter=Setting"
    print "\n`nls_replace 'KCI0726I'`\t `nls_replace 'KCI0727I'`"
    while read line
    do
      cfgName=$(print $line|cut -f1 -d"|")
      cfgName=${cfgName#N}
      if [ "$cfgName" != "YPLACEHOLDER" ]
      then
        parm=$(print $line|cut -f2 -d"|")
        value=$(print $line|cut -f3 -d"|")
	[ -n "$value" ] && print "$prod\t $cfgName\t $parm = $value"
      fi
    done < $envFile
  done
  exit
fi

# Option to display config settings relative to prompts

if [ "$Option" = "config" ]
then
  cd $configData
  if [ "$prod" = "all" ]
  then
    prod=""
    for env in $(ls k*env)
    do
      prd=${env#k}
      prdlist="$prdlist ${prd%env}"
    done
  else
    prdlist=$prod
  fi

  # Build SED script to turn rc files into a parm/prompt dictionary
  # Left justify, trim trailing blanks (and tabs)
  print -r "#n          "      >  $infoTmp
  print -r "s/^[ 	]*//"  >> $infoTmp
  print -r "s/[ 	]*$//" >> $infoTmp

  # delete blank lines, comments, conditionals
  print -r "/^ *$/d"          >>  $infoTmp
  print -r "/^[#?.]/d"        >>  $infoTmp
  print -r "/^if/d"           >>  $infoTmp
  print -r "/^else/d"         >>  $infoTmp
  print -r "/^endif/d"        >>  $infoTmp

  # join continuations, remove assignments
  print -r ":again"        >> $infoTmp
  print -r "/+$/{"         >> $infoTmp
  print -r "N"             >> $infoTmp
  print -r "s/+\n//"  >> $infoTmp
  print -r "b again"       >> $infoTmp
  print -r "}"             >> $infoTmp

  # Keep only lines with at least two delimiters (has a prompt)
  print -r "/|.*|/p"             >> $infoTmp

  # build a parm/prompt dictionary from the .rc files
  cat $CANDLEHOME/config/.*.rc | sed -f $infoTmp > $infoTmp2

    # Prepare java environment
	
	# Ensure that required files exist.
	dynShl=$CANDLEHOME/bin/dynarch.shl
	[ -x "$dynShl" ] || Err_report fail "cannot execute \"$dynShl\""

	archTbl=$CANDLEHOME/registry/archdsc.tbl
	[ -r "$archTbl" ] || Err_report fail "cannot read \"$archTbl\""

	# Determine the machine's architecture.
	. $dynShl $archTbl  # dynarch.shl will determine a value for cArch.
	[ -n "$cArch" ] || Err_report fail "could not determine the machine's architecture"
	Err_report info "computed architecture = \"$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

  # Globalize dictionary file
  $JREEXE -DGlobalDbgLevel="$TRACE_LEVEL" -DInstallRASConfig="$CANDLEHOME/config/ITMConfigRAS.properties" utilities.RCDictionaryGlobalizer $infoTmp2
  # Done building prompt dictionary, now match against product config parms
  for prod in $prdlist
  do
    envFile=k${prod}env
	#$NLS-E$ "Settings for $prod not found"
    [ -r $envFile ] || Err_report fail "`nls_replace 'KCI0728E' $prod`"
  
    #$NLS-E$ "\nConfiguration\t Setting"
    print "\n`nls_replace 'KCI0726I'`\t `nls_replace 'KCI0729I'`"
    while read line
    do
      cfgName=$(print $line|cut -f1 -d"|")
      cfgName=${cfgName#N}
      if [ "$cfgName" != "YPLACEHOLDER" ]
      then
        parm=$(print $line|cut -f2 -d"|")
        value=$(print $line|cut -f3 -d"|")

        case "$value" in
          YES | yes | Y | y)
            value="1"
            ;;
          NO | no | N | n)
            value="2"
			;;
        esac

        xx=$(grep "^$parm|" $infoTmp2)
        if [ -n "$xx" ]
        then
          prompt=$(print $xx|cut -f2 -d"|")
	  prompt=${prompt# *}	# remove leading blanks
          [ -n "$value" ] && print "$prod  $cfgName\t \"$prompt\" = $value"
        fi
      fi
    done < $envFile
  done
  exit
fi

# Option to display installed CD versions

if [ "$Option" = "version" ]
then
  cd $docLoc
  #$NLS-E$ "Installed CD release versions in this Candlehome"
  print "`nls_replace 'KCI0730I' Candlehome`"
  if [ -z "$(grep "^Release Information:" * 2>/dev/null)" ]
  then
    #$NLS-E$ "No CD version information is available"
    print "`nls_replace 'KCI0731I'`"
  else
    grep "^Release Information:" * 2>/dev/null | while read line
    do
      print ".. ${line##*:}"
    done
  fi

  # Check for CT99
  [ -n "$(ls -d $CANDLEHOME/jre* 2>/dev/null)" ] && CT99="yes"

  #$NLS-E$ "..  detected possible 99Rx version directories"
  [ "$CT99" = "yes" ] && print "`nls_replace 'KCI0732I' 99Rx`"

  # Check for CT98
  CT98=""
  [ -d $CANDLEHOME/locks ] && CT98="yes"
  [ -d $CANDLEHOME/pipes ] && CT98="yes"

  #$NLS-E$ "..  detected possible 98Rx version directories"
  [ "$CT98" = "yes" ] && print "`nls_replace 'KCI0732I' 98Rx`"

fi

# Option to display platform-code ($BINARCH) for a given product

if [ "$Option" = "platform" ]
then

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

  . $DynShl $ArchTbl

  #$NLS-E$ Platform codes:
  print "`nls_replace 'KCI0733I'`"
  #$NLS-E$ Current machine
  print $cArch   : "`nls_replace 'KCI0734I'`"

  setBinLibArchs $cArch $prod

  #$NLS-E$ "$binArch : Product ($prod)"
  print "$binArch : `nls_replace 'KCI0735I'` ($prod)"
  #$NLS-E$ "$libArch : CT Framework (ax)"
  print "$libArch : CT `nls_replace 'KCI0736I'` (ax)"
fi
