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

# @(#) setagentperm - performs operations (via SetPerm) requiring root authority.

Err_report()
{
  case $1 in
    warn) print -u2  "$PROGNAME `nls_replace 'KCI0535I'`:  $2, `nls_replace 'KCI0536I'` ..."
          sleep 2
          return
          ;;
    fail) print -u2 "$PROGNAME `nls_replace 'KCI0537I'`:  $2."
          print "\n`nls_replace 'KCI0538I'`\n" 
          read
          exit 2
          ;;
    info) print -u2 "$PROGNAME        :  $2."
          return
          ;;
  esac
}

Use_report()
{
  print -u2 "$PROGNAME [ -h CANDLEHOME ]"
  exit 0
}

# Get the names of all installed products requiring special permissions.

getVFNames()
{
  typeset -i i=0
  typeset arch fullName name pc verName
  
  ls $CANDLEHOME/registry/*.ver > /dev/null 2>&1
  [ $? -eq 0 ] || return  # Nothing to process.

  for name in $(ls $CANDLEHOME/registry/*.ver)
  do
    verName=${name##*/}  # Remove the pathname.
    pc=$(print $verName | cut -c1-2 2> /dev/null)   # Get the product code.
    [[ $pc = @(cz|or|ox|oy|ul|um|ux) ]] || continue   # Only a few products need special permissions.
  
    arch=$(print $verName | cut -c3- 2> /dev/null)  # Determine the bin arch.
    [[ $arch = @(aix|hp|hpi|sol)* ]] || continue  # Skip CMS data pieces.

    vfNames[i]=$verName
    i=i+1
  done
  
  procNum=${#vfNames[*]}
}

# 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 ] || return  # Error. 
  
  fileOwn=${lsInfo[0]}
  fileGrp=${lsInfo[1]}
}

# Determine the arch of the common database code (dd).

setDDArch()
{
  # $1 = the name of the product's .ver file

  typeset vFile=$CANDLEHOME/registry/$1
  [ -r $vFile ] || return

  ddArch=$(grep " *preReqALL *= *dd" $vFile | cut -d "=" -f2 | cut -d "|" -f1)
  if [ -n "$ddArch" ]
  then
    ddArch=$(print $ddArch | cut -c3-)
    [ -n "$ddArch" ] && return
  fi

  # Try just plain "preReq".

  ddArch=$(grep " *preReq *= *dd" $vFile | cut -d "=" -f2 | cut -d "|" -f1)
  if [ -n "$ddArch" ]
  then
    ddArch=$(print $ddArch | cut -c3-)
    [ -n "$ddArch" ] && return
  fi

  # If no "dd" prereq could be found, use the bin arch of the product.

  ddArch=${1%.ver}  # Remove the extension.
  ddArch=$(print $ddArch | cut -c3-)  # Get the architecture.
}

# Check Command Pro (cz).

checkCZ()
{
  # $1 is the name of the product's .ver file.
  
  # Define local variables.
  typeset binArch binDir files name sys  

  files="kumsnetd"
  sys=sys  # Use for HP-UX and Solaris.

  binArch=${1%.ver}  # Remove the extension.
  binArch=$(print $binArch | cut -c3-)  # Get the architecture.
  [[ "$binArch" = aix+([0-9]) ]] && sys=system

  if [[ "$binArch" = "ccp" ]]
  then
   return
  fi

  binDir=$CANDLEHOME/$binArch/cz/bin
  for name in $files
  do
    [ -r $binDir/$name ] || continue
    getOwnGrp $binDir/$name
    if [ "$fileOwn" != "root" ]
    then
      specProds="$specProds $1"
      return
    fi
    
    if [ "$fileGrp" != "$sys" ]
    then
      specProds="$specProds $1"
      return
    fi

    if [ ! -u $binDir/$name ]  # See if the set-user-id bit is set.
    then
      specProds="$specProds $1"
      return
    fi
  done
}

# Check the database agents.

checkDB()
{ # $1 is the name of the product's .ver file.
  
  # Define local variables.
  typeset kddos

  setDDArch $1

  kddos=$CANDLEHOME/$ddArch/bin/kddos
  [ -r $kddos ] || return
  getOwnGrp $kddos
  if [ "$fileOwn" != "root" ]
  then
    specProds="$specProds $1"
    return
  fi
  
  if [ ! -u $kddos ]  # See if the set-user-id bit is set.
  then
    specProds="$specProds $1"
  fi
}

# Check the UNIX Logs Monitoring Agent (ul).

checkUL()
{ # $1 is the name of the product's .ver file.
  
  # Define local variables.
  typeset binArch kul

  binArch=${1%.ver}  # Remove the extension.
  binArch=$(print $binArch | cut -c3-)  # Get the architecture.

  kul=$CANDLEHOME/$binArch/ul/bin/kulagent
  [ -r $kul ] || return
  getOwnGrp $kul
  if [ "$fileOwn" != "root" ]
  then
    specProds="$specProds $1"
    return
  fi
  
  if [ ! -u $kul ]  # See if the set-user-id bit is set.
  then
    specProds="$specProds $1"
  fi
}

# Check the Universal Agent (um).

checkUM()
{
  # $1 is the name of the product's .ver file.
  
  # Define local variables.
  typeset binArch binDir files name sys  

  files="kumsnetd kumstrap"
  sys=sys  # Use for HP-UX and Solaris.

  binArch=${1%.ver}  # Remove the extension.
  binArch=$(print $binArch | cut -c3-)  # Get the architecture.
  [[ "$binArch" = aix+([0-9]) ]] && sys=system

  binDir=$CANDLEHOME/$binArch/um/bin
  for name in $files
  do
    [ -r $binDir/$name ] || continue
    getOwnGrp $binDir/$name
    if [ "$fileOwn" != "root" ]
    then
      specProds="$specProds $1"
      return
    fi
    
    if [ "$fileGrp" != "$sys" ]
    then
      specProds="$specProds $1"
      return
    fi

    if [ ! -u $binDir/$name ]  # See if the set-user-id bit is set.
    then
      specProds="$specProds $1"
      return
    fi
  done
}

# Check the UNIX OS Monitoring Agent (ux)

checkUX()
{
  # $1 is the name of the product's .ver file.
  
  # Define local variables.
  typeset binArch binDir curLoc libArch linkName name needLoc optLib uxVFile

  needVMStat=false
  binArch=${1%.ver}  # Remove the extension.
  binArch=$(print $binArch | cut -c3-)  # Get the architecture.
  binDir=$CANDLEHOME/$binArch/ux/bin

  if [[ "$binArch" = aix+([0-9]) ]]
  then
#    [ "$binArch" = "aix43" ] && checkVMStat
#    [ "$needVMStat" = "true" ] && return  # Count uxaix43 only once.

    name=$binDir/stat_daemon
    [ -r $name ] || return
    getOwnGrp $name
#    if [ "$fileOwn" != "root" ]
#    then
#      specProds="$specProds $1"
#      return
#    fi
    
    if [ "$fileGrp" != "system" ]
    then
      specProds="$specProds $1"
      return
    fi

    if [ ! -g $name ]  # See if the set-group-id bit is set.
    then
      specProds="$specProds $1"
    fi
  fi
  
  if [[ "$binArch" = hp+([0-9])  ||  "$binArch" = hpi+([0-9]) ]]
  then

    if [[ "$binArch" = "hp116" || "$binArch" = "hpi116" ]]
    then
      if [ ! -L $CANDLEHOME/$binArch/dst/hb06/lib ]
      then
        cd $binArch  > /dev/null 2>&1
		#$NLS-E$ "could not cd to \"$binArch\" directory"
        [ $? -eq 0 ] || Err_report fail "`nls_replace 'KCI0553E' \\"$binArch\\"`"
        mkdir -p dst/hb06  > /dev/null 2>&1
        cd dst/hb06   > /dev/null 2>&1
		#$NLS-E$ "could not cd to \"dst/hb06\" directory"
        [ $? -eq 0 ] || Err_report fail "`nls_replace 'KCI0554E' \\"dst/hb06\\"`"
        ln -s $CANDLEHOME/$binArch/lib lib  > /dev/null 2>&1
		#$NLS-E$ "could not create link \"lib\""
        [ -L lib ] || Err_report fail "`nls_replace 'KCI0555E' \\"lib\\"`"
      fi
    fi

    name=$binDir/kuxagent
    [ -r $name ] || return
    getOwnGrp $name
    if [ "$fileGrp" != "sys" ]
    then
      specProds="$specProds $1"
      return
    fi

    if [ ! -g $name ]  # See if the set-group-id bit is set.
    then
      specProds="$specProds $1"
    fi
  fi

  if [[ "$binArch" = sol+([0-9]) ]]
  then
    name=$binDir/kuxagent
    uxVFile=$regDir/$1
    optLib=/opt/candle/lib

    if [ -r $name ]
    then
      getOwnGrp $name
      if [ "$fileOwn" != "root" ]
      then
        specProds="$specProds $1"
        return
      fi
    
      if [ ! -u $name ]  # See if the set-user-id bit is set.
      then
        specProds="$specProds $1"
      fi
    fi

    # Get the libArch.  The sed is needed to remove the space after the "=".

    libArch=$(grep "preReq[A-Z]* = ax" $uxVFile | cut -d"=" -f2 | sed "s/^  *//" | cut -c3- | cut -d"|" -f1)
	#$NLS-E$ "could not determine the lib arch"
    [ -n "$libArch" ] || Err_report fail "`nls_replace 'KCI0556E'`"

    # Get the link name.  Refer to IMR 626240.

    linkName=$(print $1 | cut -c3-)  # Remove the product code.
    linkName=${linkName%.*}  # Remove the .ver extension
	#$NLS-E$ "could not determine the link name"
    [ -n "$linkName" ] || Err_report fail "`nls_replace 'KCI0557E'`"
    [ "$linkName" = "sol276" ] && linkName=sol27  # Special processing required for sol276.

    # If there is no link, execute SetPerm.

    if [ ! -L "$optLib/$linkName" ]
    then
      specProds="$specProds $1"
      return
    fi
    
    # If the link exists, see if it points to the correct location.

    curLoc=$(ls -l $optLib/$linkName 2> /dev/null | awk '{print $NF}')
    needLoc=$CANDLEHOME/$libArch/lib
    if [ "$curLoc" != "$needLoc" ]
    then
      specProds="$specProds $1"
    fi
  fi
}

# Check kux_vmstat on aix43.

checkVMStat()
{ 
  # Define local variables.
  typeset name=$CANDLEHOME/aix43/ux/bin/kux_vmstat vf=uxaix43.ver
  
  [ -r $name ] || return
  getOwnGrp $name
  if [ "$fileOwn" != "root" ]
  then
    specProds="$specProds $vf"
    needVMStat=true
    return
  fi
    
  if [ "$fileGrp" != "system" ]
  then
    specProds="$specProds $vf"
    needVMStat=true
    return
  fi
    
  if [ ! -u $name ]  # See if the set-user-id bit is set.
  then
    specProds="$specProds $vf"
    needVMStat=true
    return
  fi

  if [ ! -g $name ]  # See if the set-group-id bit is set.
  then
    specProds="$specProds $vf"
    needVMStat=true
  fi
}


###################################################################
#                          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 -i procNum=0
typeset ddArch needVMStat=false

while getopts :h: opt
do case $opt in
  h) CANDLEHOME=$OPTARG
     CANDLEHOME=$(eval print $CANDLEHOME)  # Process ~ if it was entered.
     ;;
  ?) 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 'KCI0558E' CANDLEHOME`"

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

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

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

set -A vfNames
procNum=${#vfNames[*]}

# Get the names of the .ver files for products that require special permissions.

getVFNames  # getVFNames() will set the procNum variable.
[ procNum -eq 0 ] && exit 1  # Per Kurt, return 1 if there is NO need to run SetPerm.

# Determine which products require special permissions.

specProds=""

for name in ${vfNames[*]}
do
  pCode=$(print $name | cut -c1,2)  # Get the 2-character product code.
  case $pCode in
    cz)
      checkCZ $name
      ;;
    or|ox|oy)
      checkDB $name
      ;;
    ul)
      checkUL $name
      ;;
    um)
      checkUM $name
      ;;
    ux)
      checkUX $name
      ;;
  esac
done

set -A specProds $specProds
[ ${#specProds[*]} -gt 0 ] || exit 1  # Per Kurt, return 1 if there is NO need to run SetPerm.

cmd=$CANDLEHOME/bin/SetPerm
#$NLS-E$ "command \"$cmd\" not found"
[ -x $cmd ] || Err_report fail "`nls_replace 'KCI0563E' \\"$cmd\\"`"

# Acquire root authority, and process the special products.

# Note: The way this works is, if you are root, "su root" doesn't prompt you for a password
#       If you aren't root, the "su" itself asks for the password. So we DONT want to prompt
#	for it, just print out things as needed and direct logic

#$NLS-E$ "Switching to root user if not already root.  A password may be required."
nls_replace 'KCI0564I'

while true
do
  su root -c "$cmd ${specProds[*]}"  # The name "root" is required, or the command will fail on Solaris 2.5. 
  rc=$?
  if [ $rc -eq 0 ]  # su worked, and SetPerm ran successfully.
  then
    exit 0
  elif [ $rc -eq 74 ]  # SetPerm failed.
  then
   #$NLS-E$ "please acquire root access, and run \"$cmd\" from the command line"
    Err_report fail "`nls_replace 'KCI0565E' \\"$cmd\\"`"
    sleep 5
    exit 74
  else  # su failed.

    print "\n"
	#$NLS-E$ "The root password is invalid. A few post-installation operations require root-user authority."
    nls_replace 'KCI0566I'
	#$NLS-E$ "If you can enter the root password below when prompted, these operations will be automatically performed."
    nls_replace 'KCI0567I'
	#$NLS-E$ "\nIf you want to exit this program, press Enter. \n"
    print "\n`nls_replace 'KCI0568I'` \n"
    
	#$NLS-E$ "Try again [ y or n; \"n\" is default ]?  \c"
    print "`nls_replace 'KCI0550I'` [ 1-`nls_replace 'KCI0197I'`, 2-`nls_replace 'KCI0198I'`; \\"2\\" `nls_replace 'KCI0199I'` ]?  \c"
    
    read
    case $REPLY in
      y*|Y*|1)
	print ""
        continue
        ;;
      *)
        print ""
        exit 0
        ;;
    esac
    
  fi
done
