#!/bin/bash
#
# chkconfig:   345 99 01
# description: Starts and stops the ${install.package.description} network management \
#              poller and backend processes
# processname: opennms
# pidfile:     ${install.pid.file}
#
### BEGIN INIT INFO
# Provides:          opennms
# Required-Start:    $local_fs $network
# Should-Start:      $time $named ypbind ${install.postgresql.service}
# Required-Stop:     $local_fs $network
# Should-Stop:       $time $named ypbind
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: ${install.package.description} daemon for network monitoring
# Description:       ${install.package.description} daemon for network monitoring
### END INIT INFO
#
#  $Id$
#
#  For info on the "chkconfig:" section, see:
#     http://www.sensi.org/~alec/unix/redhat/sysvinit.html
#
#  For info on the "BEGIN INIT INFO" section, see:
#     http://www.suse.de/~mmj/Package-Conventions/
#
# Modifications:
#
# 2005 Oct 01: Removed `date` from a few echo commands and redirected
#              a few error messages to stderr. -- DJ Gregor
#

#### ------------> DO NOT CHANGE VARIABLES IN THIS FILE <------------- ####
#### Create $OPENNMS_HOME/etc/opennms.conf and put overrides in there. ####
#### ------------> DO NOT CHANGE VARIABLES IN THIS FILE <------------- ####

# Home directory for OpenNMS.
OPENNMS_HOME="${install.dir}"

# PID file for OpenNMS.
OPENNMS_PIDFILE="${install.pid.file}"

# Log directory for OpenNMS
LOG_DIRECTORY="${install.logs.dir}"

# Directory where init functions are likely stored (we also check a few other
# directories, just in case)
INITDIR="${install.init.dir}"

# Where to redirect "start" output.
REDIRECT="$LOG_DIRECTORY/output.log"

# Number of times to do "opennms status" after starting OpenNMS to see
# if it comes up completely.  Set to "0" to disable.  Between each
# attempt we sleep for STATUS_WAIT seconds.
START_TIMEOUT=0

# Number of seconds to wait between each "opennms status" check when
# START_TIMEOUT > 0.
STATUS_WAIT=5

# Number of times to do "opennms status" after stopping OpenNMS to see
# if it has shut down completely.  Set to "0" to disable.  Between each
# attempt we sleep for STATUS_WAIT seconds.
STOP_TIMEOUT=10

# Value of the -Xmx<size>m option passed to Java.
JAVA_HEAP_SIZE=1024

# Additional options passed to Java when starting OpenNMS.
# ADDITIONAL_MANAGER_OPTIONS=""

# Use incremental garbage collection.
USE_INCGC=""

# Use the Java Hotspot server VM.
HOTSPOT=""

# Enable verbose garbage collection debugging.
VERBOSE_GC=""

# Additional options to pass to runjava.
RUNJAVA_OPTIONS=""

# JMX URL that this script uses to communicate with a running OpenNMS daemon
# when it cannot connect using the Attach API (automatically or by PID)
JMX_URL="service:jmx:rmi:///jndi/rmi://127.0.0.1:1099/jmxrmi"

# The user that OpenNMS needs to run as.
RUNAS="root"

# Maximum file Descriptors + 1to be setted by ulimit -n
MAXIMUM_FILE_DESCRIPTORS=20480

# maximum size of stack segment (in kbytes) to be setted by ulimit -s
MAXIMUM_SIZE_STACK_SEGMENT=8192

COMMAND=""

#### ------------> DO NOT CHANGE VARIABLES IN THIS FILE <------------- ####
#### Create $OPENNMS_HOME/etc/opennms.conf and put overrides in there. ####
#### ------------> DO NOT CHANGE VARIABLES IN THIS FILE <------------- ####


# Load opennms.conf, if it exists, to override above configuration options.
if [ -f "${OPENNMS_HOME}/etc/opennms.conf" ]; then
	. "${OPENNMS_HOME}/etc/opennms.conf"
fi

# Load ~/.opennms-dev/opennms.conf if it exists, to override above configuration options.
if [ -f "${HOME}/.opennms-dev/opennms.conf" ]; then
	. "${HOME}/.opennms-dev/opennms.conf"
fi

show_help () {
	cat <<END

Usage: $0 [-f] [-n] [-t] [-p] [-o] [-c timeout] [-v] [-Q] <command> [<service>]
 
  command options: start|stop|restart|status|check|pause|resume|kill

  service options: all|<a service id from the etc/service-configuration.xml>
      defaults to all

  The following options are available:

      -f  Foreground mode. Don't fork & execute.
      -n  "No execute" mode.  Don't call Java to do anything.
      -t  Test mode.  Enable JPDA on port 8001.
      -p  Enable TIJMP profiling
      -o  Enable OProfile profiling
      -c  Controller HTTP connection timeout in seconds.
      -v  Verbose mode.  When used with the "status" command, gives the
          results for all ${install.package.description} services.
          When used with "start", enables some verbose debugging, such
          as details on garbage collection.
      -Q  Quick mode.  Don't wait for ${install.package.description} to start up.
          Useful if you want to watch the logs while ${install.package.description} starts up
          without wanting to open another terminal window.

END
	return
}

getPid(){
	test -f $OPENNMS_PIDFILE && cat $OPENNMS_PIDFILE
}

getTempFile(){
	mktemp=`which mktemp 2>/dev/null`

	test -z "$TMPDIR" && TMPDIR=/tmp

	__TEMPFILE=""
	test -n "$mktemp" && {
		__TEMPFILE=`mktemp $TMPDIR/opennmsXXXXXX`
	}
	test -z "$mktemp" && {
		__TEMPFILE="$TMPDIR/xmllint.$RANDOM.$RANDOM.$RANDOM.$$"
	}

	rm "$__TEMPFILE"
	(umask 077 && touch "$__TEMPFILE")
	echo "$__TEMPFILE"
}
		
checkXmlFiles(){
	XMLLINT=`which xmllint 2>/dev/null || :`

	if [ -n "$XMLLINT" ] && [ -x "$XMLLINT" ]; then
		TEMPFILE=`getTempFile`
		find "$OPENNMS_HOME/etc" -type f -name \*.xml | while read "FILE"; do
			"$XMLLINT" "$FILE" >/dev/null 2>&1
			if [ $? != 0 ]; then
				echo "ERROR: XML validation failed: $FILE"
				echo "	   run '$XMLLINT $FILE' for details"
			fi
		done >"$TEMPFILE"
		cat "$TEMPFILE" >&2
		FAILCOUNT=`cat "$TEMPFILE" | grep -c 'XML validation failed'`
		rm "$TEMPFILE"
		if [ $FAILCOUNT -gt 0 ]; then
			echo "Validation failed on $FAILCOUNT XML files.  Exiting." >&2
			case $COMMAND in
				status)
					# when calling `opennms status` return 3 for "stopped"
					return 3	# From LSB: 3 - program is stopped
					;;
				*)
					# any other init command should return 6 on error
					return 6	# From LSB: 6 - program is not configured
					;;
			esac
		fi
	fi

	TEMPFILE=`getTempFile`
	find "$OPENNMS_HOME/etc/imports" -type f -name \*.xml | while read "FILE"; do
		if grep "non-ip-snmp-primary" "$FILE" >/dev/null 2>&1 || grep "non-ip-interfaces" "$FILE" >/dev/null 2>&1; then
			echo "$FILE" >&2
		fi
	done >"$TEMPFILE" 2>&1

	FAILCOUNT=`cat "$TEMPFILE" | wc -l`
	if [ $FAILCOUNT -gt 0 ]; then
		cat <<END

WARNING!  The following file(s) contain the
'non-ip-snmp-primary' or 'non-ip-interfaces' attributes,
which no longer exist.

Please check your import files and remove the deprecated
attributes, and restart.

END
		cat "$TEMPFILE"
		echo ""

		return 6 # From LSB: 6 - program is not configured
	fi

	return 0
}

checkRpmFiles(){
	CHECKDIRS="$OPENNMS_HOME/etc"

	for dir in $OPENNMS_HOME/*webapps/*; do
		if [ -d "$dir/WEB-INF" ]; then
			CHECKDIRS="$CHECKDIRS $dir/WEB-INF"
		fi
	done

	for EXTENSION in rpmnew rpmsave dpkg-dist; do
		if [ `find $CHECKDIRS -name \*.$EXTENSION | wc -l` -gt 0 ]; then
			cat <<END >&2

WARNING!  You have files that end in .$EXTENSION in your
OPENNMS_HOME ($OPENNMS_HOME) directory.

The format of the original files may have changed since
you modified them before installing a new version.
Please double-check that your configuration files are
up-to-date and delete any leftover .$EXTENSION files or
${install.package.description} will not start.

END
			case $COMMAND in
				status)
					# when calling `opennms status` return 3 for "stopped"
					return 3	# From LSB: 3 - program is stopped
					;;
				*)
					# any other init command should return 6 on error
					return 6	# From LSB: 6 - program is not configured
					;;
			esac
		fi
	done

	return 0
}

checkLogDir(){
	local LOGDIR=$OPENNMS_HOME/logs

	if [ ! -e "$LOGDIR" ]; then
		mkdir -p "$LOGDIR"
	fi

	return 0
}

doStart(){
	checkXmlFiles || return $?
	checkRpmFiles || return $?
	checkLogDir   || return $?

	doStatus
	status=$?
	case $status in
		0)
			echo "${install.package.description} is already running." >&2
			return 0
			;;

		160)
			echo "${install.package.description} is partially running." >&2
			echo "If you have just attempted starting ${install.package.description}, please try again in a few" >&2
			echo "moments, otherwise, at least one service probably had issues starting." >&2
			echo "Check your logs in $LOG_DIRECTORY for errors." >&2
			return 1
			;;

		3)
			true  # don't do anything, it isn't running, which is good 
				  # because we are going to start it. :-)
			;;

		*)
			echo "Unknown value return from doStatus: $status" >&2
			return 1
	esac


	$JAVA_CMD -Dopennms.home=$OPENNMS_HOME -jar $BOOTSTRAP check
	if [ $? -ne 0 ]; then
		echo "${install.package.description} was unable to connect to the 'opennms' database configured in opennms-datasources.xml." >&2
		return 1
	fi

	##########################################################################
	# Run opennms with the "-t" option to enable the Java Platform Debugging
	# Architecture. This will open a server socket on port 8001 that can be
	# connected to by a remote java debugger. A good choice is JSwat which can 
	# be found at http://www.bluemarsh.com
	###########################################################################
	if [ $TEST -gt 0 ]; then
		echo "- enabling JPDA debugging on port 8001" >&2
		JPDA="-agentlib:jdwp=transport=dt_socket,server=y,address=8001,suspend=n"
	fi

	# See: http://www.khelekore.org/jmp/tijmp/
	if [ $TPROFILE -gt 0 ]; then
		echo "- enabling TIJMP Profiling" >&2
		JPDA="-Dtijmp.jar=/usr/share/java/tijmp-0.6.jar -agentlib:tijmp ${JPDA}"
	fi

	# See: http://oprofile.sourceforge.net/doc/setup-jit.html
	if [ $OPROFILE -gt 0 ]; then
		echo "- enabling OProfile support" >&2
		JPDA="-agentpath:/usr/lib/oprofile/libjvmti_oprofile.so ${JPDA}"
	fi

	if [ "$SERVICE" = "" ]; then
		APP_VM_PARMS="$JPDA $MANAGER_OPTIONS"
		APP_PARMS_BEFORE="start"

	else
		APP_VM_PARMS="$CONTROLLER_OPTIONS"
		APP_PARMS_BEFORE="start $SERVICE"
	fi

	JAVA_EXE=`$OPENNMS_HOME/bin/runjava -c -v`
	JAVA_EXE_BINDIR=`dirname $JAVA_EXE`
	if [ ! -x "$JAVA_EXE_BINDIR"/javac ]; then
		# this is a JRE, try to use ECJ for Jetty compilation instead
		APP_VM_PARMS="-Dorg.apache.jasper.compiler.disablejsr199=true $APP_VM_PARMS"
	fi

	if [ -z "$NOEXECUTE" ]; then
		CMD="$JAVA_CMD -Djava.endorsed.dirs=$OPENNMS_HOME/lib/endorsed $APP_VM_PARMS -jar $BOOTSTRAP $APP_PARMS_CONTROLLER $APP_PARMS_BEFORE "$@" $APP_PARMS_AFTER"
		if [ $BACKGROUND = 1 ]; then
			echo "------------------------------------------------------------------------------" >> "$REDIRECT"
			date >> "$REDIRECT"
			echo "begin ulimit settings:" >> "$REDIRECT"
			ulimit -a >> "$REDIRECT"
			echo "end ulimit settings" >> "$REDIRECT"
			echo "Executing command: $CMD" >> "$REDIRECT"
			$CMD >>"$REDIRECT" 2>&1 &
			OPENNMS_PID=$!
			echo $OPENNMS_PID > "$OPENNMS_PIDFILE"
		else
			echo "running ulimit -a"
			ulimit -a
			echo "Executing command: $CMD"
			$CMD
			exit $?
		fi
	fi

	if [ $START_TIMEOUT -eq 0 ]; then
		# don't wait for startup
		$opennms_echo "(not waiting for startup) \c"
		return 0
	fi

	# wait for startup
	STATUS_ATTEMPTS=0
	while [ $STATUS_ATTEMPTS -lt $START_TIMEOUT ]; do
		if doStatus; then
			return 0
		fi
		if /bin/ps -p $OPENNMS_PID | grep "^ *$OPENNMS_PID " > /dev/null; then
			true		# Java process is still running... don't do anything
		else
			echo "Started ${install.package.description}, but it stopped running: for details see $REDIRECT" >&2
			return 1
		fi
		sleep $STATUS_WAIT
		STATUS_ATTEMPTS=`expr $STATUS_ATTEMPTS + 1`
	done

	echo "Started ${install.package.description}, but it has not finished starting up" >&2
	return 1
}

doPause(){
	if doStatus; then
		# If there is a PID file for OpenNMS, specify the PID
		PID=`getPid`
		if [ x"$PID" != x"" ]; then
			PARM_PID="-p $PID"
		else
			PARM_PID=""
		fi

		APP_VM_PARMS="$CONTROLLER_OPTIONS"
		APP_PARMS_BEFORE="$PARM_PID -u $JMX_URL pause $SERVICE"
		if [ -z "$NOEXECUTE" ]; then
			$JAVA_CMD $APP_VM_PARMS -jar $BOOTSTRAP $APP_PARMS_CONTROLLER $APP_PARMS_BEFORE "$@" $APP_PARMS_AFTER
		fi
	else
		echo "${install.package.description} is not running." >&2
	fi
}

doResume(){
	if doStatus; then
		# If there is a PID file for OpenNMS, specify the PID
		PID=`getPid`
		if [ x"$PID" != x"" ]; then
			PARM_PID="-p $PID"
		else
			PARM_PID=""
		fi

		APP_VM_PARMS="$CONTROLLER_OPTIONS"
		APP_PARMS_BEFORE="$PARM_PID -u $JMX_URL resume $SERVICE"
		if [ -z "$NOEXECUTE" ]; then
			$JAVA_CMD $APP_VM_PARMS -jar $BOOTSTRAP $APP_PARMS_CONTROLLER $APP_PARMS_BEFORE "$@" $APP_PARMS_AFTER
		fi
	else
		echo "${install.package.description} is not running." >&2
	fi
}

doCheck() {
	if doStatus; then
		# do nothing.. it's running
		exit 0
	fi

	echo "${install.package.description} is not running... Restarting" >&2
	$OPENNMS_HOME/bin/opennms start

	exit 0
}

doStop() {
	doStatus
	if [ $? -eq 3 ]; then
		echo "Trying to stop ${install.package.description} but it's already stopped." >&2
		return 0   # LSB says: stopping when stopped is successful
	fi

	pid=`getPid`
	if [ -n "$pid" ]; then
		echo "=== ${install.package.description} Complimentary Thread Dump ===" >> $REDIRECT
		kill -3 "$pid" >> $REDIRECT 2>&1
	fi

	STATUS_ATTEMPTS=0
	while [ $STATUS_ATTEMPTS -lt $STOP_TIMEOUT ]; do
		doStatus
		if [ $? -eq 3 ]; then
			echo "" > "$OPENNMS_PIDFILE"
			return 0
		fi

		# If there is a PID file for OpenNMS, specify the PID
		PID=`getPid`
		if [ x"$PID" != x"" ]; then
			PARM_PID="-p $PID"
		else
			PARM_PID=""
		fi

		if [ -z "$NOEXECUTE" ]; then
			APP_VM_PARMS="$CONTROLLER_OPTIONS"
			APP_PARMS_BEFORE="$PARM_PID -u $JMX_URL stop $SERVICE"
			$JAVA_CMD $APP_VM_PARMS -jar $BOOTSTRAP $APP_PARMS_CONTROLLER $APP_PARMS_BEFORE "$@" $APP_PARMS_AFTER
		fi

		sleep $STATUS_WAIT
		STATUS_ATTEMPTS=`expr $STATUS_ATTEMPTS + 1`
	done

	return 1
}

doKill(){
	if doStatus; then
		# If there is a PID file for OpenNMS, specify the PID
		PID=`getPid`
		if [ x"$PID" != x"" ]; then
			PARM_PID="-p $PID"
		else
			PARM_PID=""
		fi

		APP_VM_PARMS="$CONTROLLER_OPTIONS"
		APP_PARMS_BEFORE="$PARM_PID -u $JMX_URL exit"
		$JAVA_CMD $APP_VM_PARMS -jar $BOOTSTRAP $APP_PARMS_CONTROLLER $APP_PARMS_BEFORE "$@" $APP_PARMS_AFTER
	fi

	pid=`getPid`
	if [ x"$pid" != x"" ]; then
		if /bin/ps -p "$pid" | grep "^$RUNAS" > /dev/null; then
			kill -9 $pid > /dev/null 2>&1
		fi
	fi

	echo "" > "$OPENNMS_PIDFILE"
}

doStatus(){
	if [ $VERBOSE -gt 0 ]; then
		PARM_VERBOSE="-v"
	else
		PARM_VERBOSE=""
	fi

	# If there is a PID file for OpenNMS, specify the PID
	PID=`getPid`
	if [ x"$PID" != x"" ]; then
		PARM_PID="-p $PID"
	else
		PARM_PID=""
	fi

	if [ -z "$NOEXECUTE" ]; then
		APP_VM_PARMS="$CONTROLLER_OPTIONS"
		APP_PARMS_BEFORE="$PARM_PID -u $JMX_URL $PARM_VERBOSE status"
		$JAVA_CMD $APP_VM_PARMS -jar $BOOTSTRAP $APP_PARMS_CONTROLLER $APP_PARMS_BEFORE "$@" $APP_PARMS_AFTER
	fi
}

FUNCTIONS_LOADED=0

if [ -f /etc/SuSE-release ]; then
	. /etc/rc.status
	rc_reset
else
	# Source function library.
	for dir in "$INITDIR" /etc /etc/rc.d; do
		if [ -f "$dir/init.d/functions" -a $FUNCTIONS_LOADED -eq 0 ]; then
			. "$dir/init.d/functions"
			FUNCTIONS_LOADED=1
		fi
	done
fi

if [ `echo "\000\c" | wc -c` -eq 1 ]; then
	opennms_echo="echo"
elif [ `echo -e "\000\c" | wc -c` -eq 1 ]; then
	opennms_echo="echo -e"
elif [ `/bin/echo "\000\c" | wc -c` -eq 1 ]; then
	opennms_echo="/bin/echo"
elif [ `/bin/echo -e "\000\c" | wc -c` -eq 1 ]; then
	opennms_echo="/bin/echo -e"
else
	echo "ERROR: could not get 'echo' to emit just a null character" >&2
	exit 1
fi

ulimit -s $MAXIMUM_SIZE_STACK_SEGMENT > /dev/null 2>&1
ulimit -n $MAXIMUM_FILE_DESCRIPTORS > /dev/null 2>&1
if [ x"`uname`" = x"Darwin" ]; then
	for flag in "-d" "-f" "-l" "-m" "-n" "-s" "-u" "-v"; do
		ulimit $flag unlimited >/dev/null 2>&1
	done
fi

umask 002

# XXX is this needed?  maybe we should "cd $OPENNMS_HOME/logs" so hotspot
# XXX error files go somewhere reasonable
cd "$OPENNMS_HOME" || { echo "could not \"cd $OPENNMS_HOME\"" >&2; exit 1; }

# define needed for grep to find opennms easily
JAVA_CMD="$OPENNMS_HOME/bin/runjava -r $RUNJAVA_OPTIONS --"
BOOTSTRAP="$OPENNMS_HOME/lib/opennms_bootstrap.jar"

#MANAGER_OPTIONS="-DOPENNMSLAUNCH"
MANAGER_OPTIONS=""
MANAGER_OPTIONS="$MANAGER_OPTIONS -Dopennms.home=$OPENNMS_HOME"
#MANAGER_OPTIONS="$MANAGER_OPTIONS -Djcifs.properties=$OPENNMS_HOME/etc/jcifs.properties"
MANAGER_OPTIONS="$MANAGER_OPTIONS -Xmx${JAVA_HEAP_SIZE}m"
MANAGER_OPTIONS="$MANAGER_OPTIONS -XX:+HeapDumpOnOutOfMemoryError"

if [ -n "$USE_INCGC" -a "$USE_INCGC" = true ] ; then
	MANAGER_OPTIONS="$MANAGER_OPTIONS -Xincgc"
fi

#if [ `echo "$ADDITIONAL_MANAGER_OPTIONS" | grep -c -- -Dcom.sun.management.jmxremote.port=` -eq 0 ]; then
#    ADDITIONAL_MANAGER_OPTIONS="$ADDITIONAL_MANAGER_OPTIONS -Dcom.sun.management.jmxremote.port=18980"
#fi

#if [ `echo "$ADDITIONAL_MANAGER_OPTIONS" | grep -c -- -Dcom.sun.management.jmxremote.ssl=` -eq 0 ]; then
#    ADDITIONAL_MANAGER_OPTIONS="$ADDITIONAL_MANAGER_OPTIONS -Dcom.sun.management.jmxremote.ssl=false"
#fi

if [ `echo "$ADDITIONAL_MANAGER_OPTIONS" | grep -c -- -Dcom.sun.management.jmxremote.authenticate=` -eq 0 ]; then
    ADDITIONAL_MANAGER_OPTIONS="$ADDITIONAL_MANAGER_OPTIONS -Dcom.sun.management.jmxremote.authenticate=true"
fi

if [ `echo "$ADDITIONAL_MANAGER_OPTIONS" | grep -c -- -Dcom.sun.management.jmxremote.login.config=` -eq 0 ]; then
    ADDITIONAL_MANAGER_OPTIONS="$ADDITIONAL_MANAGER_OPTIONS -Dcom.sun.management.jmxremote.login.config=opennms"
fi

if [ `echo "$ADDITIONAL_MANAGER_OPTIONS" | grep -c -- -Dcom.sun.management.jmxremote.access.file=` -eq 0 ]; then
    ADDITIONAL_MANAGER_OPTIONS="$ADDITIONAL_MANAGER_OPTIONS -Dcom.sun.management.jmxremote.access.file=$OPENNMS_HOME/etc/jmxremote.access"
fi

#if [ `echo "$ADDITIONAL_MANAGER_OPTIONS" | grep -c -- -Djava.security.debug=` -eq 0 ]; then
#    ADDITIONAL_MANAGER_OPTIONS="$ADDITIONAL_MANAGER_OPTIONS -Djava.security.debug=all"
#fi

if [ `echo "$ADDITIONAL_MANAGER_OPTIONS" | grep -c -- -DisThreadContextMapInheritable=` -eq 0 ]; then
    ADDITIONAL_MANAGER_OPTIONS="$ADDITIONAL_MANAGER_OPTIONS -DisThreadContextMapInheritable=true"
fi

# Fix for NMS-8125: Groovy meta-space leak
if [ `echo "$ADDITIONAL_MANAGER_OPTIONS" | grep -c -- -Dgroovy.use.classvalue=` -eq 0 ]; then
    ADDITIONAL_MANAGER_OPTIONS="$ADDITIONAL_MANAGER_OPTIONS -Dgroovy.use.classvalue=true"
fi

if [ `echo "$ADDITIONAL_MANAGER_OPTIONS" | grep -c MaxMetaspaceSize=` -eq 0 ]; then
	ADDITIONAL_MANAGER_OPTIONS="$ADDITIONAL_MANAGER_OPTIONS -XX:MaxMetaspaceSize=256m"
fi

if [ `echo "$ADDITIONAL_MANAGER_OPTIONS" | grep -c -- -Djava.io.tmpdir=` -eq 0 ]; then
	if [ ! -d "$OPENNMS_HOME/data/tmp" ]; then
		mkdir -p "$OPENNMS_HOME/data/tmp"
	fi
	ADDITIONAL_MANAGER_OPTIONS="$ADDITIONAL_MANAGER_OPTIONS -Djava.io.tmpdir=$OPENNMS_HOME/data/tmp"
fi

if [ x"$ADDITIONAL_MANAGER_OPTIONS" != x"" ]; then
	MANAGER_OPTIONS="$MANAGER_OPTIONS $ADDITIONAL_MANAGER_OPTIONS"
fi


if [ -n "$HOTSPOT" -a "$HOTSPOT" = true ] ; then
	JAVA_CMD="$JAVA_CMD -server"
fi

CONTROLLER_OPTIONS="-Dopennms.home=$OPENNMS_HOME"
#CONTROLLER_OPTIONS="$CONTROLLER_OPTIONS -Dlog4j.configuration=log4j.properties"

if [ x"$ADDITIONAL_CONTROLLER_OPTIONS" != x"" ]; then
	CONTROLLER_OPTIONS="$CONTROLLER_OPTIONS $ADDITIONAL_CONTROLLER_OPTIONS"
fi

TEST=0
TPROFILE=0
OPROFILE=0
NOEXECUTE=""
VERBOSE=0
BACKGROUND=1

NAME="opennms"

while getopts c:fntvpoQ c; do
	case $c in
		c)
			APP_PARMS_CONTROLLER="$APP_PARMS_CONTROLLER -t $OPTARG"
			;;
		f)
			BACKGROUND=0
			;;
		n)
			NOEXECUTE="foo"
			;;
		Q)
			START_TIMEOUT=0
			;;
		t)
			TEST=1
			;;
		p)
			TPROFILE=1
			;;
		o)
			OPROFILE=1
			;;
		v)
			VERBOSE=1
			VERBOSE_GC=1
			;;

		"?")
			show_help
			exit 1
			;;
	esac
done
shift `expr $OPTIND - 1`

if [ $# -eq 0 ]; then
	show_help
	exit 1
else
	COMMAND="$1"; shift
fi

if [ $# -gt 0 ]; then
	SERVICE="$1"; shift
else
	SERVICE=""
fi

if [ $# -gt 0 ]; then
	show_help
	exit 1
fi

if [ x"$SERVICE" = x"all" ]; then
	SERVICE=""
fi

if [ x"$VERBOSE_GC" != x"" ]; then
	MANAGER_OPTIONS="$MANAGER_OPTIONS -verbose:gc"
fi

if [ ! -f $OPENNMS_HOME/etc/configured ]; then
	cat <<END >&2
$0: ${install.package.description} not configured.
$OPENNMS_HOME/etc/configured does not exist.

You need to run the installer to set up the database.  In most
cases, it is enough to run:

  $OPENNMS_HOME/bin/install -dis

For details, see the install guide at:

http://www.opennms.org/index.php/QuickStart#Initialize_OpenNMS_and_the_Database

END

	case $COMMAND in
		status)
			# when calling `opennms status` return 3 for "stopped"
			exit 3	# From LSB: 3 - program is stopped
			;;
		*)
			# any other init command should return 6 on error
			exit 6	# From LSB: 6 - program is not configured
			;;
	esac
fi

myuser="`id -u -n`"
if [ x"$myuser" = x"$RUNAS" ]; then
	true # all is well
else
	echo "Error: you must run this script as $RUNAS, not '$myuser'" >&2
	exit 4	# According to LSB: 4 - user had insufficient privileges
fi

case "$COMMAND" in
	start|spawn)
		$opennms_echo "Starting ${install.package.description}: \c"

		if [ -f /etc/SuSE-release ]; then
			doStart

			# Remember status and be verbose
			rc_status -v
		elif [ $FUNCTIONS_LOADED -ne 0 ]; then
			doStart
			ret=$?
			if [ $ret -eq 0 ]; then
				echo_success
				if [ -w /var/lock/subsys ]; then
					touch /var/lock/subsys/${NAME}
				fi
			else
				echo_failure
			fi
			echo ""
		else
			doStart
			ret=$?
			if [ $ret -eq 0 ]; then
				echo "ok"
			else
				echo "failed"
			fi
		fi
		;;

	stop)
		$opennms_echo "Stopping ${install.package.description}: \c"
		if [ -f /etc/SuSE-release ]; then
			doStop

			# Remember status and be verbose
			rc_status -v

			doKill
		elif [ $FUNCTIONS_LOADED -ne 0 ]; then
			doStop
			ret=$?
			if [ $ret -eq 0 ]; then
				echo_success
			else
				echo_failure
			fi
			if [ -w /var/lock/subsys ]; then
				rm -f /var/lock/subsys/${NAME}
			fi
			echo ""

			doKill
		else
			doStop
			ret=$?
			if [ $ret -eq 0 ]; then
				echo "stopped"
			else
				echo "failed"
			fi

			doKill
		fi
		;;

	restart)
		## Stop the service and regardless of whether it was
		## running or not, start it again.
		$OPENNMS_HOME/bin/opennms stop
		$OPENNMS_HOME/bin/opennms start
		ret=$?

		if [ -f /etc/SuSE-release ]; then
			rc_failed $ret
		fi
		;;

	status)
		if [ -f /etc/SuSE-release ]; then
			$opennms_echo "Checking for ${install.package.description}: \c"
			if [ $VERBOSE -gt 0 ]; then
				echo ""
			fi
			doStatus

			# Remember status and be verbose
			rc_status -v
		else
			doStatus
			ret=$?
			case $ret in
				0)
					echo "${NAME} is running"
					;;

				3)
					echo "${NAME} is stopped"
					;;

				160)
					echo "${NAME} is partially running"
					;;

				*)
					echo "Unknown return code from doStatus: $ret" >&2
			esac
		fi
		;;

	configtest)
		$opennms_echo "Validating XML files: \c"
		checkXmlFiles
		ret=$?
		case $ret in
			0)
				echo "PASSED"
				;;
			*)
				# nothing
				;;
		esac
		;;

	pause)
		doPause
		;;

	check)
		doCheck
		;;

	resume)
		doResume
		;;

	kill)
		doKill
		;;

	*)
		echo ""
		echo "ERROR: unknown command \"$COMMAND\""
		show_help
		exit 2
		;;
esac


if [ -f /etc/SuSE-release ]; then
	rc_exit
else
	exit $ret
fi
