#!/bin/ksh

# we do not want world readable config files
umask 027

# use server and enable 64bit to be able to use tomcat native APR support lib
JAVA_OPTS='-server -d64'
# depending on the install location of libtcnative-1.so
#JAVA_OPTS="${JAVA_OPTS} -Djava.library.path=/local/usr/lib/64"

# used to find the real J2SDK (e.g. j2sdk1.4.2) if JAVA_HOME is not set and
# avoid getting the J2EE (e.g. j2sdkee1.4 )
MAJOR_JAVA_REL="1"

JMXFLAGS=""
# remote management JVM flags - see jre/lib/management/management.properties
# adjust these unsecure settings to your needs. Works since tiger aka J2SE 1.5
# E.g. if enabled, one can use tiger's 'jconsole' and connect to the tomcat
# agent by entering Host 'localhost' and Port '8100' in the applications
# "Remote" tab.
#JMXFLAGS="-Dcom.sun.management.jmxremote"
#JMXFLAGS="$JMXFLAGS -Dcom.sun.management.jmxremote.port=8100"
#JMXFLAGS="$JMXFLAGS -Dcom.sun.management.jmxremote.ssl=false"
#JMXFLAGS="$JMXFLAGS -Dcom.sun.management.jmxremote.ssl.need.client.auth=false"
#JMXFLAGS="$JMXFLAGS -Dcom.sun.management.jmxremote.authenticate=false"

# we do not run X or something else on the server, so make it headless,
# to be able to use graphic stuff. NOTE: Usage of heavy weight (i.e. awt)
# components like JFrame, Frame, Window etc. are still not useable without
# X or xvfb (X virtual framebuffer) and will throw a HeadlessException.
CATALINA_OPTS="-Djava.awt.headless=true $JMXFLAGS"

if [ -z "${JAVA_HOME}" ]; then
	# try to find it in the path of the user
	JAVA_CMD=`which java 2>/dev/null`
	if [ -n "${JAVA_CMD}" ]; then
		pkginfo -q SUNWj5rtx
		if [ $? -eq 0  ]; then
			JAVA_HOME=`pkgparam SUNWj5rtx BASEDIR`
			echo "setting JAVA_HOME = $JAVA_HOME"
		else
			VERS=`java -version 2>&1 | grep '1.5'`
			if [ -n "$VERS" ]; then
				JAVA_HOME=${JAVA_CMD%/*}
				JAVA_HOME=${JAVA_CMD%/*}
			fi
		fi
	fi 
	if [ -z "${JAVA_HOME}" ]; then
		# try to find it in the standard directories
		DIRS="/local/apps /local/usr /opt /usr"
		for d in $DIRS ; do
			# we simply assume, that the last one is the best one
			JD=`ls -1d ${d}/j2{sdk,jre}${MAJOR_JAVA_REL}* \
				2>/dev/null | tail -1`
			if [ -n "$JD" -a -x ${JD}/bin/java ]; then
				JAVA_HOME=${JD}
				echo "setting JAVA_HOME = $JAVA_HOME"
				break
			fi
		done
	fi
fi

if [ -n "${JAVA_HOME}" ]; then
	export JAVA_HOME=${JAVA_HOME}
fi

export CATALINA_HOME=@CLIENT_BASEDIR@/tomcat55

