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

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

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."
          #$NLS-E$ "\nPress Enter to exit. \n"
		  print "\n`nls_replace 'KCI0538I'`\n" 
          read
          exit 2
          ;;
    info) print -u2 "$PROGNAME        :  $2."
          return
          ;;
  esac
}

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

###################################################################
#                          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
TEMSFLAG=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
     TEMSFLAG=true
     ;;
  ?) 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 'KCI0539E' CANDLEHOME`"

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

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

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

if [ "$TEMSFLAG" = true ]; then
  cmd=$CANDLEHOME/bin/SetPerm
else
  cmd=$CANDLEHOME/bin/UpdateAutoRun.sh 
fi
#$NLS-E$ "command \"$cmd\" not found"
[ -x $cmd ] || Err_report fail "`nls_replace 'KCI0544E' \\"$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 'KCI0545I'

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

    if [ "$TEMSFLAG" = true ]
    then
      print "\n"
	#$NLS-E$ "The root password is invalid. TEMS User Authentication setup require root-user authority."
      nls_replace 'KCI0547I'
	#$NLS-E$ "If you can enter the root password below when prompted, these operations will be automatically performed."
      nls_replace 'KCI0548I'
	#$NLS-E$ "\nIf you want to exit this program, press Enter. \n"
      print "\n`nls_replace 'KCI0549I'`\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"
    else
      print "\n"
	#$NLS-E$ "The root password is invalid. Auto restart User Authentication setup require root-user authority."
      nls_replace 'KCI0547I'
	#$NLS-E$ "If you can enter the root password below when prompted, these operations will be automatically performed."
      nls_replace 'KCI0548I'
	#$NLS-E$ "\nIf you want to exit this program, press Enter. \n"
      print "\n`nls_replace 'KCI0549I'`\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"
    fi
    
    read
    case $REPLY in
      y*|Y*|1)

        continue
        ;;
      *)
	if [ "$TEMSFLAG" = true ]
	then
        print ""
          #$NLS-E$ "TEMS User Authentication incomplete.\n  Please acquire root access and run\n  \"$cmd -s $TEMSNAME\"\n  from the command line"
		Err_report fail "`nls_replace 'KCI0551E'`\n`nls_replace 'KCI0552I' '\n' \\" $cmd '-s' $TEMSNAME \\" '\n'`"
	else
          #$NLS-E$ "Auto restart User Authentication incomplete.\n  Please acquire root access and run\n  \"$cmd $CANDLEHOME\"\n  from the command line"
		Err_report fail "`nls_replace 'KCI0551E'`\n`nls_replace 'KCI0552I' '\n' \\" $cmd  $CANDLEHOME \\" '\n'`"
	fi
        sleep 5
        exit 0
        ;;
    esac
    
  fi
done
