#!/bin/sh
#
# $FreeBSD: ports/www/jakarta-tomcat4/files/tomcat4.sh.in,v 1.4 2006/02/20 20:47:48 dougb Exp $
#

# PROVIDE: tomcat4
# REQUIRE: NETWORKING SERVERS
# BEFORE: DAEMON
# KEYWORD: shutdown

#
# Configuration settings for tomcat4 in /etc/rc.conf:
#
# tomcat4_enable (bool):
#   Set to "NO" by default.
#   Set it to "YES" to enable tomcat4
#
# tomcat4_flags (str):
#   Set to "" by default.
#   Extra flags passed to start command
#
# tomcat4_catalina_home (str)
#   Set to "/usr/local/jakarta-tomcat4.0" by default.
#   Set the CATALINA_HOME variable for the Tomcat process
#
# tomcat4_catalina_base (str)
#   Set to "/usr/local/jakarta-tomcat4.0" by default.
#   Set the CATALINA_BASE variable for the Tomcat process
#
# tomcat4_catalina_tmpdir (str)
#   Set to "/usr/local/jakarta-tomcat4.0/temp" by default.
#   Set the CATALINA_TMPDIR variable for the Tomcat process
#
# tomcat4_stdout_log (str)
#   Set to "/usr/local/jakarta-tomcat4.0/logs/stdout.log" by default.
#   Set the location for the Tomcat process log (standard output)
#
# tomcat4_stderr_log (str)
#   Set to "/usr/local/jakarta-tomcat4.0/logs/stderr.log" by default.
#   Set the location for the Tomcat process log (error output)
#
# tomcat4_stop_timeout (num)
#   Set to "10" by default.
#   Sets the timeout in seconds to allow tomcat to shutdown.
#   After the timeout has elapsed, tomcat will be killed.
#
# tomcat4_java_home (str):
# tomcat4_java_vendor (str):
# tomcat4_java_version (str):
# tomcat4_java_os (str):
#   Specify the requirements of the Java VM to use. See javavm(1).
#
# tomcat4_classpath (str):
#   Set to "" by default.
#   Addtional classes to add to the CLASSPATH
#
# tomcat4_java_opts (str):
#   Set to "" by default.
#   Java VM args to use.
#

tomcat4_enable="${tomcat4_enable:-"NO"}"
tomcat4_java_version="${tomcat4_java_version:-"1.2+"}"
tomcat4_user="${tomcat4_user:-"www"}"
tomcat4_catalina_home="${tomcat4_catalina_home:-"/usr/local/jakarta-tomcat4.0"}"
tomcat4_catalina_base="${tomcat4_catalina_base:-"/usr/local/jakarta-tomcat4.0"}"
tomcat4_catalina_tmpdir="${tomcat4_catalina_tmpdir:-"/usr/local/jakarta-tomcat4.0/temp"}"
tomcat4_stdout_log="${tomcat4_stdout_log:-"/usr/local/jakarta-tomcat4.0/logs/stdout.log"}"
tomcat4_stderr_log="${tomcat4_stderr_log:-"/usr/local/jakarta-tomcat4.0/logs/stderr.log"}"
tomcat4_stop_timeout="${tomcat4_stop_timeout:-"10"}"

. /etc/rc.subr

name="tomcat4"
rcvar=`set_rcvar`
pidfile="/var/run/tomcat4.pid"

load_rc_config "${name}"

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

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

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

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

java_command="/usr/local/bin/java \
	${tomcat4_java_opts} \
	-Djava.endorsed.dirs=$JAVA_ENDORSED_DIRS \
	-classpath /usr/local/jakarta-tomcat4.0/bin/bootstrap.jar:/usr/local/jakarta-tomcat4.0/bin/commons-logging-api.jar:${tomcat4_classpath} \
	-Dcatalina.base=${tomcat4_catalina_base} \
	-Dcatalina.home=${tomcat4_catalina_home} \
	-Djava.io.tmpdir=${tomcat4_catalina_tmpdir} \
	org.apache.catalina.startup.Bootstrap"

log_args=">> ${tomcat4_stdout_log} \
	2>> ${tomcat4_stderr_log} "

# Subvert the check_pid_file procname check.
if [ -f $pidfile ]; then
  read rc_pid junk < $pidfile
  if [ ! -z "$rc_pid" ]; then
    procname=`ps -o comm= $rc_pid`
  fi
fi

required_files="${tomcat4_catalina_home}/conf/server.xml"

command="/usr/sbin/daemon"
flags="-p ${pidfile} ${java_command} start ${tomcat4_flags} ${log_args}"

start_precmd=pid_touch
stop_cmd="tomcat4_stop"

pid_touch ()
{
	touch $pidfile
	chown $tomcat4_user $pidfile
}

tomcat4_stop() {
	rc_pid=$(check_pidfile $pidfile $procname)

	if [ -z "$rc_pid" ]; then
		[ -n "$rc_fast" ] && return 0
		if [ -n "$pidfile" ]; then
			echo "${name} not running? (check $pidfile)."
		else
			echo "${name} not running?"
		fi
		return 1
	fi
	
	echo "Stopping ${name}."
	${java_command} stop
	tomcat_wait_max_for_pid ${tomcat4_stop_timeout} ${rc_pid}
	kill -KILL ${rc_pid} 2> /dev/null && echo "Killed."
	echo -n > ${pidfile}
}

tomcat_wait_max_for_pid() {
	_timeout=$1
	shift
	_pid=$1
	_prefix=
	while [ $_timeout -gt 0 ] ; do
		echo -n ${_prefix:-"Waiting (max $_timeout secs) for PIDS: "}$_pid
		_prefix=", "
		sleep 2
		kill -0 $_pid 2> /dev/null || break
		_timeout=$(($_timeout-2))
	done
	if [ -n "$_prefix" ]; then
		echo "."
	fi
}

run_rc_command "$1"
