#!/sbin/sh
#
#ident  "@(#)pprosetup.sh 1.13 03/08/06 SMI"
#

# Declare an interrupt handler to catch various signals.
#
trap intrHandler INT HUP KILL TERM

# Re-enable the echo on stty, and do the clean up.
#
intrHandler() {
    stty echo
    cleanup
}

JAVA_HOME="/usr/j2se"
NOTROOTERR="ERROR: Only the root user can execute this script."
JAVACMDERR="ERROR: Cannot find java2."
PPROCONFERR="ERROR: No PatchPro configuration (etc/patchpro.conf) found. \
	    Default configuration will be used."
NOEXECERR="ERROR: Cannot find PatchPro executables"
#NOLIBPATHWRN="WARNING : LD_LIBRARY_PATH is undefined"
PROCESSFILE=/var/tmp/patchproprocs
Background="true"
CLASSNAME=PatchProSetup
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 {-[[D | W <day> | M <date> ] -s <hh:mm:ss>]"
	echo "            -d <download_directory> -p <none|standard>"
	echo "            -H <hardware selection>} | -L"

	exit 1;
}

#
# Parse the command line. Set up default variable values if not supplied
# by the user.
#
parseCmdline() {
	USEENV="false"

	#
	# Process all of the command line arguments.
	#
	for i in $*; do
		case $i in
		setup)	Subcommand="setup"; shift;;
		policy)	Subcommand="policy"; shift;;
		serv)	Subcommand="serv"; shift;;
		help)	Subcommand="help"; shift;;
		setup)	Subcommand="setup"; shift;;
		-e)	USEENV="true"; shift;;
		-*)	echo ${BADARGSERR}; usage;;
		esac
	done
}

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

	CLS_DIRECTORY=`dirname $BIN_DIRECTORY`/lib/patchpro.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}:${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 0 if the argument #1 represents a newer release than
# argument #2, 1 otherwise.
#
isNewer() { # arguments are version strings
        first=`echo ${1} | awk -F, '{print $1}'`
        if [ "${first}" = "" ]; then
                first=${1}
        fi
	firstmajor=`echo ${first} | awk -F. '{print $1}'`
	firstminor=`echo ${first} | awk -F. '{print $2}'`
	firstminor=${firstminor:-0}
	firstmicro=`echo ${first} | awk -F. '{print $3}'`
	firstmicro=${firstmicro:-0}

        second=`echo ${2} | awk -F, '{print $1}'`
        if [ "${second}" = "" ]; then
                second=${2}
        fi
	secondmajor=`echo ${second} | awk -F. '{print $1}'`
	secondminor=`echo ${second} | awk -F. '{print $2}'`
	secondminor=${secondminor:-0}
	secondmicro=`echo ${second} | awk -F. '{print $3}'`
	secondmicro=${secondmicro:-0}

	if [ ${firstmajor} -gt ${secondmajor} ]; then
		return 0
	elif [ ${firstmajor} -lt ${secondmajor} ]; then
		return 1
	elif [ ${firstminor} -gt ${secondminor} ]; then
		return 0
	elif [ ${firstminor} -lt ${secondminor} ]; then
		return 1
	elif [ ${firstmicro} -gt ${secondmicro} ]; then
		return 0
	else
		return 1
	fi
}

# Cleaning up
cleanup() {
    if [ `cat /opt/SUNWppro/etc/patchpro.conf.lock` = $$ ]; then
        rm -rf /opt/SUNWppro/etc/patchpro.conf.lock
    fi
}

#
# Main routine
#

# set umask to ensure that files altered by Java have appropriate permissions
umask 0022
confirmJava
# parseCmdline $*
getPaths
constructCmd

eval ${fullCommand} $@ -y$$
