#!/bin/sh
#
# $FreeBSD: ports/www/tomcat6/files/tomcat6.sh.in,v 1.9 2010/10/26 08:19:05 ale Exp $
#

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

#
# Configuration settings for tomcat60 in /etc/rc.conf:
#
# tomcat60_enable (bool):
#   Set to "NO" by default.
#   Set it to "YES" to enable tomcat60
#
# tomcat60_flags (str):
#   Set to "" by default.
#   Extra flags passed to start command
#
# tomcat60_catalina_home (str)
#   Set to "/usr/local/apache-tomcat6.0" by default.
#   Set the CATALINA_HOME variable for the Tomcat process
#
# tomcat60_catalina_base (str)
#   Set to "/usr/local/apache-tomcat6.0" by default.
#   Set the CATALINA_BASE variable for the Tomcat process
#
# tomcat60_catalina_tmpdir (str)
#   Set to "/usr/local/apache-tomcat6.0/temp" by default.
#   Set the CATALINA_TMPDIR variable for the Tomcat process
#
# tomcat60_stdout_log (str)
#   Set to "/usr/local/apache-tomcat6.0/logs/stdout.log" by default.
#   Set the location for the Tomcat process log (standard output)
#
# tomcat60_stderr_log (str)
#   Set to "/usr/local/apache-tomcat6.0/logs/stderr.log" by default.
#   Set the location for the Tomcat process log (error output)
#
# tomcat60_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.
#
# tomcat60_java_home (str):
# tomcat60_java_vendor (str):
# tomcat60_java_version (str):
# tomcat60_java_os (str):
#   Specify the requirements of the Java VM to use. See javavm(1).
#
# tomcat60_classpath (str):
#   Set to "" by default.
#   Addtional classes to add to the CLASSPATH
#
# tomcat60_java_opts (str):
#   Set to "" by default.
#   Java VM args to use.
#

tomcat60_enable="${tomcat60_enable:-"NO"}"
tomcat60_java_version="${tomcat60_java_version:-"1.6+"}"
tomcat60_user="${tomcat60_user:-"www"}"
tomcat60_catalina_home="${tomcat60_catalina_home:-"/usr/local/apache-tomcat6.0"}"
tomcat60_catalina_base="${tomcat60_catalina_base:-"/usr/local/apache-tomcat6.0"}"
tomcat60_catalina_tmpdir="${tomcat60_catalina_tmpdir:-"/usr/local/apache-tomcat6.0/temp"}"
tomcat60_stdout_log="${tomcat60_stdout_log:-"/usr/local/apache-tomcat6.0/logs/stdout.log"}"
tomcat60_stderr_log="${tomcat60_stderr_log:-"/usr/local/apache-tomcat6.0/logs/stderr.log"}"
tomcat60_stop_timeout="${tomcat60_stop_timeout:-"10"}"

. /etc/rc.subr

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

load_rc_config "${name}"

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

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

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

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

java_class="org.apache.catalina.startup.Bootstrap"
java_command="/usr/local/bin/java \
	${tomcat60_java_opts} \
	-Djava.endorsed.dirs=/usr/local/apache-tomcat6.0/endorsed \
	-classpath /usr/local/apache-tomcat6.0/bin/bootstrap.jar:${tomcat60_classpath} \
	-Dcatalina.base=${tomcat60_catalina_base} \
	-Dcatalina.home=${tomcat60_catalina_home} \
	-Djava.io.tmpdir=${tomcat60_catalina_tmpdir} \
	${java_class}"

log_args=">> ${tomcat60_stdout_log} \
	2>> ${tomcat60_stderr_log} "

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

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

start_precmd="pid_touch"
stop_cmd="tomcat_stop"
status_cmd="tomcat_status"
poll_cmd="tomcat_poll"

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

tomcat_stop() {
	rc_pid=$(tomcat_check_pidfile $pidfile)

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

tomcat_status() {
	rc_pid=$(tomcat_check_pidfile $pidfile)

	if [ -n "$rc_pid" ]; then
		echo "${name} is running as pid $rc_pid."
	else
		echo "${name} is not running."
		return 1
	fi
}

tomcat_poll() {
	rc_pid=$(tomcat_check_pidfile $pidfile)

	_prefix=
	while (true) ; do
		[ -z "$rc_pid" ] && break
		echo -n ${_prefix:-"Waiting for PIDS: "}$rc_pid
		_prefix=", "
		sleep 2
	done
	if [ -n "$_prefix" ]; then
	echo "."
	fi
}

tomcat_check_pidfile() {
	_pidfile=$1
	if [ -z "$_pidfile" ]; then
		err 3 'USAGE: tomcat_check_pidfile pidfile'
	fi
	if [ ! -f $_pidfile ]; then
		debug "pid file ($_pidfile): not readable."
		return
	fi
	read _pid _junk < $_pidfile
	if [ -z "$_pid" ]; then
		debug "pid file ($_pidfile): no pid in file."
		return
	fi
	if [ -n "`/usr/local/bin/jps -l | grep -e "^$_pid $java_class\$"`" ]; then
		echo -n $_pid
	fi
}

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"
