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

# @(#) CheckForRoot - checks for root-ownership of certain files.

# Note the meaning of the following exit codes:
#   0 = program ran successfully, and there are no root-owned files.
#   1 = program ran successfully, and there are root-owned files.
#   2 = program failure.

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 CANDLEHOME ]"
  exit 0
}

# Get the owner and group names for a file.

getOwnGrp()
{ # $1 = full pathname of the file to check.

  typeset -i numEle=0
  fileOwn="" fileGrp=""
  
  set -A lsInfo $(ls -l $1 2> /dev/null | awk '{print $3" "$4}') 
  numEle=${#lsInfo[*]}
  [ numEle -eq 2 ] || exit 2  # Error. 
  
  fileOwn=${lsInfo[0]}
  fileGrp=${lsInfo[1]}
}

# Get the permissions for a file.

getFilePerm()
{ # $1 = full pathname of the file to check.

  typeset -i numEle=0
  typeset permAll
  fileTyp="" permOwn="" permGrp="" permOth=""
  
  permAll=$(ls -l $1 2> /dev/null | awk '{print $1}') 


  # 37091 $permAll for a file begins with "-". Since echo was changed for print in 33342,
  # print was thinking that all the letters after "-" were options. Additional letter 
  # was added to $permAll and cut was moved one column to the right.
  
  fileTyp=$(print "a$permAll" | cut -c2)
  permUsr=$(print "a$permAll" | cut -c3-5)
  permGrp=$(print "a$permAll" | cut -c6-8)
  permOth=$(print "a$permAll" | cut -c9-11)

}

# Check the CMS.

checkMS()
{ 

  # Define local variables.
  typeset cfgFile binPath binArch libArch verFile optLib kds

  thisMach=$(uname -a | cut -d' ' -f2 | cut -d. -f1)
  ls $CANDLEHOME/config/${thisMach}_ms_$TEMSNAME.config > /dev/null 2>&1
  [ $? -eq 0 ] || return  # Nothing to process.
  cfgFile=$CANDLEHOME/config/${thisMach}_ms_$TEMSNAME.config

  binPath=$(grep "CMS_BINPATH" $cfgFile | cut -d"=" -f2 | sed "s/'//g")  # binPath -> /aix42/ms/bin

  binArch=${binPath%/ms/bin}  # Remove the extension.
  binArch=${binArch##*/}  # Remove the path.

  kds=$binPath/kdsvlunx
  verFile=$CANDLEHOME/registry/ms$binArch.ver
  optLib=/opt/IBM/ITM/tmaitm6/links 
     
  # Get the libArch.  The sed is needed to remove the space after the "=".
  libArch=$(grep "preReq[A-Z]* = ax" $verFile | cut -d"=" -f2 | sed "s/^  *//" | cut -c3- | cut -d"|" -f1 | head -1)

  linkPath=$(ls -l $optLib/*/lib 2> /dev/null | grep $CANDLEHOME/tmaitm6/$libArch/lib | cut -d'>' -f1 | cut -d'/' -f2- | cut -d' ' -f1)
  [ -n "$linkPath" ] && linkPath=/$linkPath 

## 29706 root ownership and permissions are still needed for ITM 6.1
  [ -r $kds ] || return
  getOwnGrp $kds
  getFilePerm $kds
  if [[ -n "$linkPath" && "$fileOwn" = "root" ]] &&
     [[ "$permUsr" = @(*s*) || "$permGrp" = @(*s*) ]]
  then
    TEMSdone=true
  fi

## 29706 root ownership and permissions are still needed for ITM 6.1
##  [ -r $kds ] || return
##  [ -n "$linkPath" ] && TEMSdone=true
}


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

typeset ddArch TEMSdone=false

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

#$NLS-E$ "TEMS Name not specified"
[ -n "$TEMSNAME" ] || Err_report fail "`nls_replace 'KCI0685E'`"

#$NLS-E$ "CANDLEHOME directory not set or not specified"
[ -n "$CANDLEHOME" ] || Err_report fail "`nls_replace 'KCI0680E' CANDLEHOME`"
#$NLS-E$ "\"$CANDLEHOME\" is not a directory"
[ -d "$CANDLEHOME" ] || Err_report fail "`nls_replace 'KCI0681E' \\"$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

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

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

# See if the TEMS Security chowns and links have been done already

checkMS
if [ "$TEMSdone" = "true" ]
then
 exit 0
else
  exit 1  # SetPerm -s needs to be run.
fi
