#!/bin/sh

# PROVIDE: riemann
# REQUIRE: LOGIN NETWORKING SERVERS
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# riemann_enable    (bool):   Set to NO by default.
# Set it to YES to enable riemann.
#
# riemann_config          (string): Optional full path for riemann config file
# riemann_user            (user):   Set to riemann by default.
# riemann_group           (group):  Set to riemann by default.
# riemann_jarfile         (string): Optional path to a custom jarfile.
# riemann_java_home       (args):   Specify which JVM to use, if not default.
# riemann_java_classpath  (args):   Specify a custom Java ClassPath.
# riemann_min_mem         (num):    Minumum JVM heap size, 256m by default.
# riemann_max_mem         (num):    Maximum JVM heap size, 1g by default.
# riemann_java_opts       (args):   Additional JVM properties or arguments.

. /etc/rc.subr

name="riemann"
rcvar=riemann_enable

load_rc_config $name

: ${riemann_enable="NO"}
: ${riemann_user:="riemann"}
: ${riemann_group:="riemann"}
: ${riemann_config:="%%ETCDIR%%/riemann.config"}
: ${riemann_jarfile:="%%JAVAJARDIR%%/riemann.jar"}
: ${riemann_java_home:="%%JAVA_HOME%%"}
: ${riemann_min_mem:="256m"}
: ${riemann_max_mem:="1g"}
: ${riemann_java_classpath:=${riemann_jarfile}}
: ${riemann_java_opts:=" -server \
	-cp ${riemann_java_classpath} \
        -Dapp=riemann \
        -Xms${riemann_min_mem} \
        -Xmx${riemann_max_mem} \
        -Xss256k \
        -Djava.awt.headless=true \
        -XX:+AggressiveOpts \
        -XX:+CMSClassUnloadingEnabled \
        -XX:+CMSParallelRemarkEnabled \
        -XX:+DisableExplicitGC \
        -XX:+HeapDumpOnOutOfMemoryError \
        -XX:+UseCMSInitiatingOccupancyOnly \
        -XX:+UseCompressedOops \
        -XX:+UseConcMarkSweepGC \
        -XX:+UseFastAccessorMethods \
        -XX:+UseParNewGC \
        -XX:CMSInitiatingOccupancyFraction=75 "}

pidfile="/var/run/riemann.pid"
start_precmd="install -o ${riemann_user} /dev/null ${pidfile}"

command="/usr/sbin/daemon"
command_args=" \
	-c \
	-t riemann \
	-r \
	-P ${pidfile} \
	${riemann_java_home}/bin/java \
	${riemann_java_opts} \
	riemann.bin start ${riemann_config}"

required_files="${java_cmd} ${riemann_config}"

# support SIGHUP to reload config file using pkill. parent pid
# is that of daemon(8) and not of riemann's JVM instance
extra_commands="reload"
reload_cmd="pkill -SIGHUP -U ${riemann_user} -G ${riemann_group} -f Dapp=riemann"

run_rc_command "$1"
