#!/bin/sh

#
# PROVIDE: fabio
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# fabio_enable (bool):                Set to NO by default.
#                                     Set it to YES to enable fabio.
# fabio_user (user):                  Set user to run fabio.
#                                     Default is "nobody".
# fabio_group (group):                Set group to run fabio.
#                                     Default is "nobody".
# fabio_syslog_output_enable (bool):  Set to YES to enable syslog output
#                                     Default is "NO". See daemon(8).
# fabio_syslog_output_tag (str):      Set syslog tag if syslog enabled.
#                                     Default is "fabio". See daemon(8).
# fabio_syslog_output_priority (str): Set syslog priority if syslog enabled.
#                                     Default is "info". See daemon(8).
# fabio_syslog_output_facility (str): Set to YES to enable syslog output
#                                     Default is "daemon". See daemon(8).


. /etc/rc.subr

name=fabio
rcvar=fabio_enable

load_rc_config $name

: ${fabio_enable:="NO"}
: ${fabio_user:="nobody"}
: ${fabio_group:="nobody"}
: ${fabio_syslog_output_enable:="NO"}

if checkyesno fabio_syslog_output_enable; then
    if [ -n "${fabio_syslog_output_tag}" ]; then
        fabio_syslog_output_flags="-T ${fabio_syslog_output_tag}"
    else
        fabio_syslog_output_flags="-T ${name}"
    fi
    if [ -n "${fabio_syslog_output_priority}" ]; then
        fabio_syslog_output_flags="${fabio_syslog_output_flags} -s ${fabio_syslog_output_priority}"
    fi  

    if [ -n "${fabio_syslog_output_facility}" ]; then
        fabio_syslog_output_flags="${fabio_syslog_output_flags} -l ${fabio_syslog_output_facility}"
    fi  
fi  

pidfile=/var/run/fabio.pid
procname="%%PREFIX%%/sbin/fabio"
command="/usr/sbin/daemon"
command_args="-f -t ${name} ${fabio_syslog_output_flags} -p ${pidfile} ${procname} -cfg %%PREFIX%%/etc/fabio.properties"

start_precmd=fabio_startprecmd

fabio_startprecmd()
{
        if [ ! -e ${pidfile} ]; then
                install -o ${fabio_user} -g ${fabio_group} /dev/null ${pidfile};
        fi
}

run_rc_command "$1"
