#!/bin/sh
#
# PROVIDE: benthos
# REQUIRE: DAEMON
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to run NATS:
#
# benthos_enable (bool):	Set it to "YES" to enable benthos.
#				Default is "NO".
# benthos_user:			User name to run as. default "benthos"
# benthos_group:		Group name to run as. default "benthos"
# benthos_resources:		Path to benthos resources files (glob).
# benthos_config:		Path to benthos configuration file.
# benthos_watcher (bool):	Set it to "YES" to enable benthos watcher mode.
#				Default is "NO".
# benthos_options:		Options to pass benthos
#                               (e.g. 'streams' for streams mode).
#

. /etc/rc.subr

name=benthos
rcvar=`set_rcvar`

load_rc_config ${name}

: ${benthos_enable:=NO}
: ${benthos_user:=benthos}
: ${benthos_group:=benthos}
: ${benthos_resources:=""}
: ${benthos_config:="%%PREFIX%%/etc/benthos/config.yaml"}
: ${benthos_watcher:=NO}
: ${benthos_options=""}

if [ -n "${benthos_resources}" ]; then
    benthos_options="--resources '${benthos_resources}' ${benthos_options}"
fi

if [ -n "${benthos_config}" ]; then
    benthos_options="--config ${benthos_config} ${benthos_options}"
fi

if checkyesno benthos_watcher; then
    benthos_options="--watcher ${benthos_options}"
fi

start_precmd=benthos_start_precmd

pidfile="/var/run/${name}.pid"
procname=%%PREFIX%%/bin/benthos
command=/usr/sbin/daemon
command_args="-p ${pidfile} ${procname} ${benthos_options}"

benthos_start_precmd()
{
    install -o ${benthos_user} -g ${benthos_group} /dev/null ${pidfile}
}

run_rc_command "$1"
