#!/bin/ksh
# set -x
cliargs=$@

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
}

usage() {
    print ""
	print "Usage: itmpatch -h <ITM home> [OPTIONS]"
	print ""
	print "   itmpatch -h <ITM home> -i { <patch_file_directory> | <patch_file> }"
	print "   itmpatch -h <ITM home> -t { <patch_file_directory> | <patch_file> }"
	print ""
	#$NLS-E$ "   where"
	print "\t`nls_replace 'KCI0876I`"
	print ""
	#$NLS-E$ "   -h  Specifies the ITM installation directory"
	print "\t`nls_replace 'KCI0877I' -h`"
	#$NLS-E$ "   -i  Specifies the path to the directory or patch file to be installed"
	print "\t`nls_replace 'KCI0878I' -i`"
	#$NLS-E$ "   -t  Generates a report of the actions to be taken by the patch"
	print "\t`nls_replace 'KCI0879I' -t`"
	print ""
	print ""

	exit 2
}

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

#just check for -h CANDLEHOME, the itmpatch will do the rest of the
#CLI argument checking
while getopts ":i:t:h:" OPTS
do case $OPTS in
	h) CANDLEHOME=$OPTARG
		export CANDLEHOME
		;;
	esac
done

[ -n "$CANDLEHOME" ] || usage 

if [ ! -d "$CANDLEHOME" ]
then
   usage
fi

CurRegPath=$CANDLEHOME/registry
ArchTbl=$CurRegPath/archdsc.tbl
DynShl=$CANDLEHOME/bin/dynarch.shl
if [ ! -d $CurRegPath ] 
then
   print ""
   #$NLS-E$ "Unable to execute itmpatch please make sure that -h <ITM home> is set correctly"
   print "`nls_replace 'KCI0880E' itmpatch -h <ITM_home>`"
   print ""
   usage
fi

. $DynShl $ArchTbl 2>/dev/null

#
# Set the product code for CLI's
#
PRODUCT=ui

#setBinLibArchs $cArch $PRODUCT

#print "cArch: $cArch"
#print "binArch: $binArch"

baseArch=${cArch%%[0-9]*} 
#print "baseArch: $baseArch"

latestVer=0
for pc in $(ls $CurRegPath/ui$baseArch*.ver  2>/dev/null)
do
	#print $pc
	curVer=$(grep VRMF $pc | awk '{ printf("%s\n", $3) }')
	if [ $curVer -gt $latestVer ]
	then
		latestVer=$curVer
		latestFile=$pc
	fi
done

if [ $latestVer -eq 0 ]
then
  print ""
  #$NLS-E$ "The ui component is not installed, therefore the itmpatch executable is not installed."
  #$NLS-E$ "Please install the Tivoli Enterprise Services User Interface - ui component."
  print "`nls_replace 'KCI0881E'`"
  print ""
  exit 2 
fi

#print "latest ver: $latestVer"
#print "latest file: $latestFile"

#remove the path from the file
patchArch=${latestFile##*/}
#remove the extension
patchArch=${patchArch%.ver}
#remove the product code
patchArch=${patchArch#$PRODUCT}
#print "patch arch: $patchArch"

$CANDLEHOME/$patchArch/ui/bin/itmpatch $cliargs
