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

# @(#) UnSetRoot - for certain files, changes the owner from root to the
# @(#) user ID specified in the call.

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

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

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

getVFNames()
{
  typeset -i i=0
  typeset arch 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|ms|or|ox|oy|ul|um|ux|ax) ]] || continue   # Only a few products need special permissions.
  
    arch=$(print $verName | cut -c3- 2> /dev/null)  # Determine the bin arch.
    [[ $arch = @(aix|hp|sol)* ]] || continue  # Skip CMS data pieces.

    vfNames[i]=$verName
    i=i+1
  done
}

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

# UnSet files for Shared Libraries (ax).

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

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

  file=$CANDLEHOME/tmaitm6/$binArch/bin/kbbacf1
  [ -r $file ] || return
  chown $owner $file > /dev/null 2>&1
  #$NLS-E$ "could not change the owner of \"$file\""
  [ $? -eq 0 ] || Err_report fail "`nls_replace 'KCI0850E' \\"$file\\"`"
}

# UnSet files for Command Pro (cz).

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

  files="kumsnetd"

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

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

  binDir=$CANDLEHOME/$binArch/cz/bin
  for name in $files
  do
    [ -r $binDir/$name ] || continue
    chown $owner $binDir/$name > /dev/null 2>&1
	#$NLS-E$ "could not change the owner of \"$binDir/$name\""
    [ $? -eq 0 ] || Err_report fail "`nls_replace 'KCI0850E' \\"$binDir/$name\\"`"
  done
}

# Unset files for the database agents (or, ox and oy).

unSetDB()
{
  # $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
  chown $owner $kddos > /dev/null 2>&1
  #$NLS-E$ "could not change the owner of \"$kddos\""
  [ $? -eq 0 ] || Err_report fail "`nls_replace 'KCI0850E' \\"$kddos\\"`"
}

# Unset CMS files.  Required if security validation was used.

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

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

  kds=$CANDLEHOME/$binArch/ms/bin/kdsvlunx
  [ -r $kds ] || return
  chown $owner $kds > /dev/null 2>&1
  #$NLS-E$ "could not change the owner of \"$kds\""
  [ $? -eq 0 ] || Err_report fail "`nls_replace 'KCI0850E' \\"$kds\\"`"
}

# Unset files for the UNIX Logs Monitoring Agent (ul).

unSetUL()
{
  # $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
  chown $owner $kul > /dev/null 2>&1
  #$NLS-E$ "could not change the owner of \"$kul\""
  [ $? -eq 0 ] || Err_report fail "`nls_replace 'KCI0850E' \\"$kul\\"`"
}

#  Unset files for the Universal Agent (um).

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

  files="kumsnetd kumstrap"

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

  binDir=$CANDLEHOME/$binArch/um/bin
  for name in $files
  do
    [ -r $binDir/$name ] || continue
    chown $owner $binDir/$name > /dev/null 2>&1
	#$NLS-E$ "could not change the owner of \"$binDir/$name\""
    [ $? -eq 0 ] || Err_report fail "`nls_replace 'KCI0850E' \\"$binDir/$name\\"`"
  done
}

# Unset files for the UNIX OS Monitoring Agent (ux)

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

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

  if [[ "$binArch" = aix+([0-9]) ]]  # Retained for when we were changing the owner of stat_daemon to root.
  then
    name=$binDir/stat_daemon
    [ -r $name ] || return
    chown $owner $name > /dev/null 2>&1
	#$NLS-E$ "could not change the owner of \"$name\""
    [ $? -eq 0 ] || Err_report fail "`nls_replace 'KCI0850E' \\"$name\\"`"
    return
  fi
     
  if [[ "$binArch" = sol+([0-9]) ]]
  then
    name=$binDir/kuxagent
    [ -r $name ] || return
    chown $owner $name > /dev/null 2>&1
	#$NLS-E$ "could not change the owner of \"$name\""
    [ $? -eq 0 ] || Err_report fail "`nls_replace 'KCI0850E' \\"$name\\"`"
  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 ddArch owner

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 1 ] || Use_report  # A user ID is required.
owner=$1

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

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

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

set -A vfNames
getVFNames  # getVFNames() will fill the vfNames array.

# Change the owner of root-owned files from root to the ID specified at invocation.

for name in ${vfNames[*]}
do
  pCode=$(print $name | cut -c1,2)  # Get the 2-character product code.
  case $pCode in
    ax)
      unSetAX $name
      ;;  
    cz)
      unSetCZ $name
      ;;
    ms)
      unSetMS $name
      ;;
    or|ox|oy)
      unSetDB $name
      ;;
    ul)
      unSetUL $name
      ;;
    um)
      unSetUM $name
      ;;
    ux)
      unSetUX $name
      ;;
  esac
done

exit 0
