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

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

#
# Configuration settings for tomcat41 in /etc/rc.conf:
#
# tomcat41_enable (bool):
#   Set to "NO" by default.
#   Set it to "YES" to enable tomcat41
#
# tomcat41_flags (str):
#   Set to "" by default.
#   Extra flags passed to start command
#
# tomcat41_catalina_home (str)
#   Set to "/usr/local/jakarta-tomcat4.1" by default.
#   Set the CATALINA_HOME variable for the Tomcat process
#
# tomcat41_catalina_base (str)
#   Set to "/usr/local/jakarta-tomcat4.1" by default.
#   Set the CATALINA_BASE variable for the Tomcat process
#
# tomcat41_catalina_tmpdir (str)
#   Set to "/usr/local/jakarta-tomcat4.1/temp" by default.
#   Set the CATALINA_TMPDIR variable for the Tomcat process
#
# tomcat41_stdout_log (str)
#   Set to "/usr/local/jakarta-tomcat4.1/logs/stdout.log" by default.
#   Set the location for the Tomcat process log (standard output)
#
# tomcat41_stderr_log (str)
#   Set to "/usr/local/jakarta-tomcat4.1/logs/stderr.log" by default.
#   Set the location for the Tomcat process log (error output)
#
# tomcat41_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.
#
# tomcat41_java_home (str):
# tomcat41_java_vendor (str):
# tomcat41_java_version (str):
# tomcat41_java_os (str):
#   Specify the requirements of the Java VM to use. See javavm(1).
#
# tomcat41_classpath (str):
#   Set to "" by default.
#   Addtional classes to add to the CLASSPATH
#
# tomcat41_java_opts (str):
#   Set to "" by default.
#   Java VM args to use.
#

tomcat41_enable="${tomcat41_enable:-"NO"}"
tomcat41_java_version="${tomcat41_java_version:-"1.2+"}"
tomcat41_user="${tomcat41_user:-"www"}"
tomcat41_catalina_home="${tomcat41_catalina_home:-"/usr/local/jakarta-tomcat4.1"}"
tomcat41_catalina_base="${tomcat41_catalina_base:-"/usr/local/jakarta-tomcat4.1"}"
tomcat41_catalina_tmpdir="${tomcat41_catalina_tmpdir:-"/usr/local/jakarta-tomcat4.1/temp"}"
tomcat41_stdout_log="${tomcat41_stdout_log:-"/usr/local/jakarta-tomcat4.1/logs/stdout.log"}"
tomcat41_stderr_log="${tomcat41_stderr_log:-"/usr/local/jakarta-tomcat4.1/logs/stderr.log"}"
tomcat41_stop_timeout="${tomcat41_stop_timeout:-"10"}"

. /etc/rc.subr

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

load_rc_config "${name}"

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

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

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

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

java_command="/usr/local/bin/java \
	${tomcat41_java_opts} \
	-Djava.endorsed.dirs=$JAVA_ENDORSED_DIRS \
	-classpath /usr/local/jakarta-tomcat4.1/bin/bootstrap.jar:/usr/local/jakarta-tomcat4.1/bin/commons-logging-api.jar:${tomcat41_classpath} \
	-Dcatalina.base=${tomcat41_catalina_base} \
	-Dcatalina.home=${tomcat41_catalina_home} \
	-Djava.io.tmpdir=${tomcat41_catalina_tmpdir} \
	org.apache.catalina.startup.Bootstrap"

log_args=">> ${tomcat41_stdout_log} \
	2>> ${tomcat41_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="${tomcat41_catalina_home}/conf/server.xml"

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

start_precmd=pid_touch
stop_cmd="tomcat41_stop"

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

tomcat41_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 ${tomcat41_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"
