#!/sbin/sh
#
#ident  "@(#)smpatch.sh 1.4 03/08/27 SMI"
#

JAVA_HOME="/usr/j2se"
NOTROOTERR="ERROR: Only the root user can execute this script."
JAVACMDERR="ERROR: Cannot find java2."
CLASSNAME=SMPatchServices
PKGNAME=com.sun.patchpro.cli
PATHNAME=com/sun/patchpro/cli
PRG=$0
SDBLIBNAME=lib/simpledb.jar
APCYLIBNAME=lib/authpcy.jar
PKGINFO="/usr/bin/pkginfo"
PKGPARAM="/usr/bin/pkgparam"
PKGCMDARGS=""
NAWK="/usr/bin/nawk"

usage() {
	echo "usage: $PRG -ild"
	echo "Usage: $PRG ["
	echo "		   [[add | download]"
	echo "			[[-d <patchdir>] [-h]"
	echo "			[[-i <patch_id> [ -i <patch_id]>]... |"
	echo "		   	    idlist=<patchlist_file>]]"
	echo "		   [analyze [-h]]"
	echo "		   [remove [-p <patch_id>] [-h]]"
	echo "		   ]]"
	exit 1;
}

#
# Confirm that Java can be found.
#
confirmJava() {
	JAVACMD=""

	#
	# Start with the assumption that everything is set up already
	#
	if [ -n "${JAVA_HOME}" -a -x ${JAVA_HOME}/bin/java ];
	then
		JAVACMD=${JAVA_HOME}/bin/java
	elif [ -n "${JAVA_BIN}" -a -x ${JAVA_BIN}/java ]; then
		JAVACMD=${JAVA_BIN}/java
	else
	    echo $JAVACMDERR
	    exit 1;

	fi	

	if [ -z "${JAVA_BIN}" ]; then
		JAVA_BIN=${JAVA_HOME}/bin
	fi
}

#
# Confirm that the user is root.
#
confirmRoot() {
	Uid=`id | sed 's/uid=\([0-9]*\)(.*/\1/'`
	if [ $Uid -ne 0 ]; then
		echo ${NOTROOTERR}
		exit 1
	fi
}

#
# set up all paths to all the various executables, classes and libraries
getPaths() {
	BIN_DIRECTORY=`dirname $PRG`
	BIN_PROGRAM=`basename $PRG`

	if [ "$BIN_DIRECTORY" = "." ]; then
		BIN_DIRECTORY=`pwd`
		PRG=${BIN_DIRECTORY}/${BIN_PROGRAM}
	fi

	pkginsts=`getpkginsts "SUNWppro"`
	pkginst=`getlatestpkg "${pkginsts}"`
	CLS_DIRECTORY=`/usr/bin/pkgparam ${pkginst} BASEDIR`/lib/patchpro.jar
	DET_DIRECTORY=`/usr/bin/pkgparam ${pkginst} BASEDIR`/lib/cache/pprodetectors.jar

	pkginsts=`getpkginsts "SUNWsdb"`
	pkginst=`getlatestpkg "${pkginsts}"`
	SDB_LIBRARY=`/usr/bin/pkgparam ${pkginst} BASEDIR`/${SDBLIBNAME}

	pkginsts=`getpkginsts "SUNWapcy"`
	pkginst=`getlatestpkg "${pkginsts}"`
	APCY_LIBRARY=`/usr/bin/pkgparam ${pkginst} BASEDIR`/${APCYLIBNAME}

	CLASSPATHEXT="${CLS_DIRECTORY}:${DET_DIRECTORY}:${SDB_LIBRARY}:${APCY_LIBRARY}"

	#
	# Set up CLASSPATH.
	#
	CLASSFILE="${CLASSNAME}.class"
	if [ -z "${CLASSPATH}" ]; then
		CLASSPATH=${CLASSPATHEXT}
	else
		CLASSPATH=${CLASSPATHEXT}:${CLASSPATH}
	fi

	CLS_DIRECTORY=""

	CLASSPATHLIST=`echo $CLASSPATH | sed s/:/\ /g`

	# See if we can find the class
	#for dir in ${CLASSPATHLIST}; do
	#	if [ -f $dir/${PATHNAME}/${CLASSFILE} ]; then
	#		CLS_DIRECTORY=$dir
	#		break;
	#	fi
	#done

	#if [ -z "${CLS_DIRECTORY}" ]; then
	#	echo "ERROR: Couldn't find $PKGNAME.$CLASSNAME."
	#	exit 1
	#fi      

	export CLASSPATH

	#
	# No LD_LIBRARY_PATH could be a bad thing, but we'll let it go
	# with a warning.
	#
	if [ -z "${LD_LIBRARY_PATH}" ]; then
		echo ${NOLIBPATHWRN}
	else
		export LD_LIBRARY_PATH
	fi

	classname=${PKGNAME}.${CLASSNAME}
	classpath=${CLS_DIRECTORY}/${PATHNAME}/${CLASSFILE}
}

#
# Construct the command
#
constructCmd() {
	fullCommand="${JAVACMD} "

	fullCommand="${fullCommand} ${classname}"
}

#
# This returns the list of installed package instances of the provided
# package.
#
getpkginsts() { #1 is the pkg
	l=`eval ${PKGINFO} ${PKGCMDARGS} ${1}.* 2> /dev/null`
	if [ -n "${l}" ]; then
		 echo "${l}" | ${NAWK} ' { print $2;  } '
	else
		echo ""
	fi
}

#
# This prints the newest package instance provided a list of 0 or more
# pkg instances
#
getlatestpkg() { # 1 is a list of pkginst's
	p=""
	vs=""
	for pk in ${1}; do
		nvs=`eval ${PKGPARAM} ${PKGCMDARGS} ${pk} VERSION`
		if [ -n "${vs}" ]; then
			if [ isNewer ${nvs} ${vs} ]; then
				nvs=${vs}
				p=${pk}
			fi
		else
			vs=${nvs}
			p=${pk}
		fi
	done
	echo ${p}
}

#
# This returns 1 if the argument #1 represents a newer release than
# argument #2.
#
isNewer() { # arguments are version strings
	first=${1%,REV*}
	if [[ "${first}" = "" ]]; then
		first=${1}
	fi

	second=${2%,REV*}
	if [[ "${second}" = "" ]]; then
		second=${2}
	fi

	return `/usr/bin/expr ${first} \<= ${second}`
}
#
# Main routine
#

# Set permissions appropriately in support of patch extraction
# and installation. Remember that the user "nobody" must have
# read access to the checkinstall scripts in the patches
#
umask 022

confirmJava
getPaths
constructCmd

eval ${fullCommand} $@
