#!/bin/sh
#
# $FreeBSD: ports/mail/james/files/james.sh.in,v 1.3 2012/01/14 08:55:57 dougb Exp $
#

#
# Configuration settings for geronimo%%GERONIMO_VERSION%% in /etc/rc.conf:
#
# james231_enable (bool):
#   Set to "NO" by default.
#   Set it to "YES" to enable james231
#
#
# james231_home (str)
#   Set to "/usr/local/james2.3.1" by default.
#   Set the JAMES_HOME variable for the James process
#
# james231_base (str)
#   Set to "/usr/local/james2.3.1" by default.
#   Set the JAMES_BASE variable for the James process
#
# james231_tmpdir (str)
#   Set to "/tmp" by default.
#
# geronimo%%GERONIMO_VERSION%%_stop_timeout (num)
#   Set to "10" by default.
#   Sets the timeout in seconds to allow geronimo to shutdown.
#   After the timeout has elapsed, geronimo will be killed.
#
# james231_java_home (str):
# james231_java_vendor (str):
# james231_java_version (str):
# james231_java_os (str):
#   Specify the requirements of the Java VM to use. See javavm(1).
#
# james231_classpath (str):
#   Set to "" by default.
#   Addtional classes to add to the CLASSPATH
#
# james231_java_opts (str):
#   Set to "" by default.
#   Java VM args to use.
#

james231_enable="${james231_enable:-"NO"}"
james231_java_version="${james231_java_version:-"1.5+"}"
james231_home="${james231_home:-"/usr/local/james2.3.1"}"
james231_base="${james231_base:-"/usr/local/james2.3.1"}"
james231_tmpdir="${james231_tmpdir:-"/tmp"}"
james231_stop_timeout="${james231_stop_timeout:-"10"}"

. /etc/rc.subr

name="james231"
rcvar=james231_enable
pidfile="/var/run/.pid"

JAVA_HOME=/usr/local/openjdk6
JRE_HOME=$JAVA_HOME/jre

JVM_EXT_DIRS="${james231_home}/lib:${james231_home}/tools/lib"
JVM_OPTS="-Djava.ext.dirs=$JVM_EXT_DIRS"

load_rc_config "${name}"

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

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

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

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

if [ "$JAVA_HOME" = "" ] ; then
  echo "ERROR: JAVA_HOME not found in your environment."
  echo
  echo "Please, set the JAVA_HOME variable in your environment to match the"
  echo "location of the Java Virtual Machine you want to use."
  exit 1
fi

if [ -z "${james231_tmpdir}" ] ; then
  # Define the java.io.tmpdir to use for Phoenix
  james231_tmpdir="${james231_home}"/temp
  mkdir -p "${james231_tmpdir}"
fi

java_cmd=
if [ -z "${JAVA_HOME}" ] ; then
	java_cmd=/usr/local/bin/java
else
	java_cmd=${JAVA_HOME}/bin/java
fi


java_command="$java_cmd $JVM_OPTS \
    -Djava.security.policy=jar:file:${james231_home}/bin/phoenix-loader.jar!/META-INF/java.policy \
    -Dphoenix.home=${james231_home} \
    -Djava.io.tmpdir=${james231_tmpdir} \
    -jar ${james231_home}/bin/phoenix-loader.jar >/dev/null"

PHOENIX_CONSOLE="$PHOENIX_TMPDIR/phoenix.console"
if [  -z "$PHOENIX_CONSOLE" ]
then
  if [ -w /dev/console ]
  then
    PHOENIX_CONSOLE=/dev/console
  else
    PHOENIX_CONSOLE=/dev/tty
  fi
fi


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

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

start_precmd=pid_touch
stop_cmd="james231_stop"

pid_touch ()
{
	touch $pidfile
}

james231_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}."
        kill ${rc_pid}>/dev/null
	james_wait_max_for_pid ${james231_stop_timeout} ${rc_pid}
	kill -KILL ${rc_pid} 2> /dev/null && echo "Killed."
	echo -n > ${pidfile}
}

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