#!@l_prefix@/bin/openpkg rc
##
##  rc.ratbox -- Run-Commands
##

%config
    ratbox_enable="$openpkg_rc_def"
    ratbox_server_flags=""
    ratbox_services_flags=""
    ratbox_log_prolog="true"
    ratbox_log_epilog="true"
    ratbox_log_numfiles="10"
    ratbox_log_minsize="1M"
    ratbox_log_complevel="9"

%common
    ratbox_server_logfile="@l_prefix@/var/ratbox/server.log"
    ratbox_server_pidfile="@l_prefix@/var/ratbox/server.pid"
    ratbox_server_signal () {
        [ -f $ratbox_server_pidfile ] && kill -$1 `cat $ratbox_server_pidfile`
    }
    ratbox_services_logfile="@l_prefix@/var/ratbox/services.log"
    ratbox_services_pidfile="@l_prefix@/var/ratbox/services.pid"
    ratbox_services_signal () {
        [ -f $ratbox_services_pidfile ] && kill -$1 `cat $ratbox_services_pidfile`
    }

%status -u @l_rusr@ -o
    ratbox_usable="no"
    ratbox_active="no"
    rcService ratbox enable yes && ratbox_server_signal 0 && ratbox_active="yes"
    @l_prefix@/sbin/ratbox-ircd -conftest >/dev/null 2>&1 && ratbox_usable="yes"
    echo "ratbox_enable=\"$ratbox_enable\""
    echo "ratbox_usable=\"$ratbox_usable\""
    echo "ratbox_active=\"$ratbox_active\""

%start -u @l_rusr@
    rcService ratbox enable yes || exit 0
    rcService ratbox active yes && exit 0
    @l_prefix@/bin/ratbox-ircd ${ratbox_server_flags}
    sleep 2
    @l_prefix@/bin/ratbox-services ${ratbox_services_flags}

%stop -u @l_rusr@
    rcService ratbox enable yes || exit 0
    rcService ratbox active no  && exit 0
    ratbox_server_signal TERM
    ratbox_services_signal TERM
    sleep 2
    rm -f $ratbox_server_pidfile >/dev/null 2>&1 || true
    rm -f $ratbox_services_pidfile >/dev/null 2>&1 || true

%restart -u @l_rusr@
    rcService ratbox enable yes || exit 0
    rcService ratbox active no  && exit 0
    rc ratbox stop start

%daily -u @l_rusr@
    rcService ratbox enable yes || exit 0
    shtool rotate -f \
        -n ${ratbox_log_numfiles} -s ${ratbox_log_minsize} -d \
        -z ${ratbox_log_complevel} -m 644 -o @l_rusr@ -g @l_rgrp@ \
        -P "${ratbox_log_prolog}" \
        -E "${ratbox_log_epilog}; rc ratbox reload" \
        $ratbox_server_logfile
    shtool rotate -f \
        -n ${ratbox_log_numfiles} -s ${ratbox_log_minsize} -d \
        -z ${ratbox_log_complevel} -m 644 -o @l_rusr@ -g @l_rgrp@ \
        -P "${ratbox_log_prolog}" \
        -E "${ratbox_log_epilog}; rc ratbox reload" \
        $ratbox_services_logfile

