#!/bin/sh
#
# eXist startup script.
#
# $FreeBSD: ports/databases/exist/files/eXist.in,v 1.2 2010/03/27 00:12:45 dougb Exp $
#

# PROVIDE: eXist
# REQUIRE: NETWORKING SERVERS

# Add the following lines to /etc/rc.conf to enable eXist:
# eXist_enable (bool):		Set to "NO" by default.
#				Set it to "YES" to enable eXist
# eXist_flags (str):		Set to "-server" by default.
#				Extra JVM flags.
#
. /etc/rc.subr

name="eXist"
rcvar=`set_rcvar`

# Read settings and set default values.
load_rc_config "$name"
: ${eXist_enable="NO"}
: ${eXist_flags="-server -Djetty.port=8081"}

# Variables.
eXist_user="www"
eXist_group="www"
eXist_home="/usr/local/eXist"
java_flags="-Xms16000k -Xmx128000k -Dfile.encoding=UTF-8 -Dexist.home=${eXist_home} -Djetty.home=${eXist_home}/tools/jetty -Djava.endorsed.dirs=${eXist_home}/lib/endorsed -jar ${eXist_home}/start.jar"
java_command="/usr/local/diablo-jdk1.6.0/bin/java ${java_flags}"

# Commands.
pidfile="/var/run/${name}.pid"
command="/usr/sbin/daemon"
flags="-f -p ${pidfile} ${java_command} jetty ${eXist_flags} >${eXist_home}/startup.log 2>&1"
stop_cmd="eXist_stop"

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

eXist_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} shutdown >${eXist_home}/shutdown.log 2>&1
	wait_for_pids $rc_pid
}

run_rc_command "$1"
