#!/bin/sh

# PROVIDE: kapacitord
# REQUIRE: DAEMON NETWORKING
# BEFORE: LOGIN
# KEYWORD: shutdown

# Add the following lines to /etc/rc.conf to enable telegrafb:
# kapacitord_enable="YES"
#
# kapacitord_enable (bool):       Set to YES to enable kapacitord
#                               Default: NO
# kapacitord_conf (str):          kapacitord configuration file
#                               Default: ${PREFIX}/etc/kapacitord.conf
# kapacitord_flags (str):         Extra flags passed to kapacitord

. /etc/rc.subr

name="kapacitord"
rcvar=kapacitord_enable
load_rc_config $name

: ${kapacitord_enable:="NO"}
: ${kapacitord_user:="%%KAPACITORD_USER%%"}
: ${kapacitord_group:="%%KAPACITORD_GROUP%%"}
: ${kapacitord_facility:="daemon"}
: ${kapacitord_priority:="info"}
: ${kapacitord_flags:=""}
: ${kapacitord_conf:="%%PREFIX%%/etc/kapacitor.conf"}
: ${kapacitord_options:="${kapacitord_flags} -config=${kapacitord_conf}"}

# daemon
start_precmd=kapacitord_prestart
kapacitord_pidfile="/var/run/${name}.pid"
procname="%%PREFIX%%/bin/${name}"
command=/usr/sbin/daemon
start_cmd="kapacitord_startcmd_daemon"

kapacitord_startcmd_daemon()
{
	echo "Starting ${name}."
	/usr/sbin/daemon -c -p ${kapacitord_pidfile} -S -s ${kapacitord_priority} -l ${kapacitord_facility} \
		-u ${kapacitord_user} ${procname} ${kapacitord_options}
}

kapacitord_startcmd_logger()
{
	echo "Starting ${name}."
	/usr/sbin/daemon -c -p ${kapacitord_pidfile} -u ${kapacitord_user} /bin/sh -c "${procname} ${kapacitord_options} 2>&1 \
		| /usr/bin/logger -t ${name} -p ${kapacitord_facility}.${kapacitord_priority}"
}

kapacitord_prestart() {
# Have to empty rc_flags so they don't get passed to daemon(8)
        rc_flags=""
}

run_rc_command "$1"
