#!/bin/sh

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

# Add the following lines to /etc/rc.conf to enable bosunb:
# bosun_enable="YES"
#
# bosun_enable (bool):       Set to YES to enable bosun
#                               Default: NO
# bosun_conf (str):          bosun configuration file
#                               Default: /usr/local/etc/bosun/${name}.conf
# bosun_user (str):          bosun daemon user
#                               Default: bosun
# bosun_group (str):         bosun daemon group
#                               Default: bosun
# bosun_flags (str):         Extra flags passed to bosun
#                               Default: empty

. /etc/rc.subr

PATH=${PATH}:/usr/local/sbin:/usr/local/bin

name="bosun"
rcvar=bosun_enable
load_rc_config $name

: ${bosun_enable:="NO"}
: ${bosun_user:="bosun"}
: ${bosun_group:="bosun"}
: ${bosun_conf:="/usr/local/etc/bosun/${name}.conf"}
: ${bosun_flags}:=""
: ${bosun_options:="${bosun_flags} -c ${bosun_conf}"}

logfile="/var/log/bosun/${name}.log"
pidfile="/var/run/${name}.pid"
command=/usr/sbin/daemon
start_precmd="bosun_prestart"
start_cmd="bosun_start"
stop_cmd="bosun_stop"

bosun_prestart()
{
    install -d -o ${bosun_user} -g ${bosun_group} -m750 /var/log/bosun
}

bosun_start()
{
    echo "Starting ${name}"
    /usr/sbin/daemon -fcr -P ${pidfile} -u ${bosun_user} -o ${logfile} \
        /usr/local/bin/${name} ${bosun_options}
}

bosun_stop()
{
    pid=$(check_pidfile $pidfile $command)
    if [ -n "${pid}" ]; then
        echo "Stopping ${name} (pid=${pid})"
        kill -- -${pid}
        wait_for_pids ${pid}
    else
        echo "${name} isn't running"
    fi
}

run_rc_command "$1"
