#!/sbin/sh
#
# ident	"%Z%%M%	%I%	%E% SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

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

SHUTUTIL=/usr/lib/patch/shututil
TOMCAT_CONF_DIR=/var/patchsvr/conf
JAVACMD=/usr/bin/java
Background="true"
PATHNAME=com/sun/patchpro/server/cli
CLASSNAME=PatchServerSetup
PKGNAME=com.sun.patchpro.server.cli
PRG=$0
SDBLIBNAME=usr/lib/patch/simpledb.jar
PKGINFO="/usr/bin/pkginfo"
PKGCMDARGS=""
NAWK="/usr/bin/nawk"

CATALINA_HOME=/usr/apache/tomcat
CATALINA_BASE=/var/patchsvr
JAVA_HOME=/usr/java
JAVA_OPTS="-Djava.library.path=/usr/lib/cc-ccr/lib"
export JAVA_HOME CATALINA_HOME CATALINA_BASE JAVA_OPTS


##############################################################################
# Set up localization text domain
TEXTDOMAIN=SUNW_PATCHMGR_CLI_SCRIPTS
export TEXTDOMAIN

#
# Confirm that the user is root.
#
confirmRoot() {
	Uid=`id | sed 's/uid=\([0-9]*\)(.*/\1/'`
	if [ "$Uid" -ne 0 ]; then
		gettext -s "You must be root to run patchsvr command."
		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/patch/patchpro.jar
	SDB_LIBRARY=`dirname $BIN_DIRECTORY`/lib/patch/simpledb.jar
	PLUGINS_DIRECTORY=`dirname $BIN_DIRECTORY`/lib/patch

	CLASSPATHEXT="${CLS_DIRECTORY}:${SDB_LIBRARY}:${PLUGINS_DIRECTORY}"

	#
	# 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
	#	gettext -s "ERROR: Couldn't find $PKGNAME.$CLASSNAME."
	#	exit 1
	#fi      

	export CLASSPATH

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

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

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

#
# Confirm that the user is root.
#
updateShutdownPort() {
     # Now make a copy of the output template file to change the
     # shutdown port number and command value.  Fix ownership.
     PORT=`${SHUTUTIL} -p`
     if [ $? -ne 0 -o "${PORT}" = "" ]; then
         gettext -s "Error obtaining shutdown portnumber"
         exit 1
     fi
     SHUT=`${SHUTUTIL} -c`
     if [ $? -ne 0 -o "${SHUT}" = "" ]; then
         gettext -s "Error obtaining shutdown command"
         exit 1
     fi
     TGT="${TOMCAT_CONF_DIR}"/server.xml

     /usr/bin/sed -e "s@SHUTDOWN_PORT@${PORT}@" \
                  -e "s@SHUTDOWN_COMMAND@${SHUT}@" \
                  "${TOMCAT_CONF_DIR}"/server.template > ${TGT}

}


#
# Main routine
#

# set umask to ensure that files altered by Java have appropriate permissions
umask 0022
confirmRoot
getPaths
constructCmd

case $1 in
	start) updateShutdownPort ;
		gettext -s "Starting Local Patch Server" ;		
		$CATALINA_HOME/bin/startup.sh > /dev/null ;;
	stop) gettext -s "Shutting down Local Patch Server" ;
		$CATALINA_HOME/bin/shutdown.sh > /dev/null ;;
	setup) shift
		eval ${fullCommand} $@ ;;
	enable) touch /etc/patch/patchsvr ;;
	disable) rm -f /etc/patch/patchsvr ;;
	status)  if [ -f /etc/patch/patchsvr ]
		then
		    gettext -s "enabled" ;
		else
		    gettext -s "disabled" ;
		fi ;;
	*) gettext -s "Please specify one of the following options: start, stop, setup, status, enable or disable" ;;
esac

