#!/bin/sh
#
# $FreeBSD: ports/www/red5/files/red5.in,v 1.1 2010/11/18 13:50:50 pav Exp $
#

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

#
# Configuration settings for red5 in /etc/rc.conf:
#
# red5_enable (bool):
#   Set to "NO" by default.
#   Set it to "YES" to enable red5
#
# red5_home (str)
#   Set to "/usr/local/red5" by default.
#   Set the directory red5 will be executed
#
# red5_stdout_log (str)
#   Set to /usr/local/red5/log/red5.log" by default.
#   Set the location for the Tomcat process log (standard output)
#
# red5_stderr_log (str)
#   Set to "/usr/local/red5/log/red5.err" by default.
#   Set the location for the Tomcat process log (error output)
#
# red5_java_opts (str):
#   Set to "" by default.
#   Java VM args to use.
#   Eg: red5_java_opts="-Xrs -Xms512M -Xmx1024M -Xss128K -XX:NewSize=256m -XX:SurvivorRatio=16 \
#			-XX:MinHeapFreeRatio=20 -XX:+AggressiveHeap -XX:+ExplicitGCInvokesConcurrent \
#			-XX:+UseConcMarkSweepGC -Dsun.rmi.dgc.client.gcInterval=990000 \
#			-Dsun.rmi.dgc.server.gcInterval=990000 
#			-Djava.net.preferIPv4Stack=true -Xverify:none"
#
# red5_user (str)
#   Set to www by default.
#   Set the user that will execute the server.
#  
# red5_connection_token (str)
#   Set to "" by default.
#   Set the "port username pass" which will be used to shutdown the server
#   Eg: "9999 red5user s3cr3t"
#

. /etc/rc.subr

name="red5"
rcvar=`set_rcvar`
pidfile="/var/run/red5.pid"
java_command="/usr/local/bin/java"

#constant options
red5_logging_opts="-Dlogback.ContextSelector=org.red5.logging.LoggingContextSelector \
			-Dcatalina.useNaming=true"
red5_security_opts="-Djava.security.debug=failure"
red5_mainclass="org.red5.server.Bootstrap"
red5_stop_mainclass="org.red5.server.Shutdown"
red5_jython_opt="-Dpython.home=lib"
red5_debug_opts="-Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n"

load_rc_config "${name}"

#configurable options
red5_enable="${red5_enable:-"NO"}"
red5_user="${red5_user:-"www"}"
red5_home="${red5_home:-"/usr/local/red5"}"
red5_stdout_log="${red5_stdout_log:-"/usr/local/red5/log/red5.log"}"
red5_stderr_log="${red5_stderr_log:-"/usr/local/red5/log/red5.err"}"
red5_stop_timeout="${red5_stop_timeout:-"10"}"


red5_opts="${red5_logging_opts} ${red5_security_opts} ${red5_java_opts}"
red5_stop_opts="-Djavax.net.ssl.keyStore=${red5_home}/conf/keystore.jmx \
		-Djavax.net.ssl.keyStorePassword=password"
red5_classpath="${red5_home}/boot.jar:${red5_home}/conf:${CLASSPATH}"

red5_start_cmd="${java_command} ${red5_jython_opt} -Dred5.root=${red5_home} \
		${red5_opts} -cp ${red5_classpath} ${red5_mainclass}"

red5_stop_cmd="${java_command} ${red5_jython_opt} -Dred5.root=${red5_home} \
		${red5_logging_opts} ${red5_security_opts} ${red5_stop_opts} \
		-cp ${red5_classpath} ${red5_stop_mainclass}"

red5_debug_cmd="${java_command} ${red5_jython_opt} -Dred5.root=${red5_home} \
		${red5_opts} ${red5_debug_opts} -cp ${red5_classpath} ${red5_mainclass}"

log_args=">> ${red5_stdout_log} \
	2>> ${red5_stderr_log} "


command="/usr/sbin/daemon"
flags="-p ${pidfile} ${red5_start_cmd} ${log_args}"

start_precmd="red5_pre_start"
stop_cmd="red5_stop"
status_cmd="red5_status"
poll_cmd="red5_poll"
extra_commands="debug"
debug_cmd="red5_debug"

red5_pre_start() {
	export RED5_HOME=${red5_home}
	touch $pidfile
	chown $red5_user $pidfile
	cd ${red5_home} || exit 1
}

red5_debug() {
	${red5_debug_cmd}
}

red5_stop() {
	rc_pid=$(red5_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}."
	if [ ${red5_connection_token} ]; then
		${red5_stop_cmd}
	fi
	red5_wait_max_for_pid ${red5_stop_timeout} ${rc_pid}
	kill -KILL ${rc_pid} 2> /dev/null && echo "Killed."
	rm -f ${pidfile}
}

red5_status() {
	rc_pid=$(red5_check_pidfile $pidfile)

	if [ -z "$rc_pid" ]; then
		[ -n "$rc_fast" ] && return 0
		echo "${name} not running? (check $pidfile)."
		return 1
	fi
	echo "${name} is running has pid $rc_pid."
}

red5_poll() {
	echo -n "Waiting for pid: $(cat $pidfile)"
	while (true) ; do
		rc_pid=$(red5_check_pidfile $pidfile)
		[ -z "$rc_pid" ] && break
		sleep 2
	done
	echo -e "\tdone\n"
}


red5_check_pidfile() {
	_pidfile=$1
	if [ -z "$_pidfile" ]; then
		err 3 'USAGE: red5_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 $red5_mainclass\$"`" ]; then
		echo -n $_pid
	fi
}

red5_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"




