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

%config
    serfdom_enable="$openpkg_rc_def"
    serfdom_flags=""
    serfdom_log_prolog="true"
    serfdom_log_epilog="true"
    serfdom_log_numfiles="10"
    serfdom_log_minsize="1M"
    serfdom_log_complevel="9"

%common
    serfdom_cfgfile="@l_prefix@/etc/serfdom/serf.json"
    serfdom_logfile="@l_prefix@/var/serfdom/log/serf.log"
    serfdom_pidfile="@l_prefix@/var/serfdom/run/serf.pid"
    serfdom_snpfile="@l_prefix@/var/serfdom/run/serf.snp"
    serfdom_signal () {
        [ -f $serfdom_pidfile ] && kill -$1 `cat $serfdom_pidfile`
    }

%status -u @l_rusr@ -o
    serfdom_usable="unknown"
    serfdom_active="no"
    rcService serfdom enable yes && \
        serfdom_signal 0 && serfdom_active="yes"
    echo "serfdom_enable=\"$serfdom_enable\""
    echo "serfdom_usable=\"$serfdom_usable\""
    echo "serfdom_active=\"$serfdom_active\""

%start -u @l_rusr@
    rcService serfdom enable yes || exit 0
    rcService serfdom active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/serf agent \
            -snapshot "$serfdom_snpfile" \
            -config-file "$serfdom_cfgfile" \
            $serfdom_flags \
            </dev/null >>$serfdom_logfile 2>&1 &
        echo $! >$serfdom_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService serfdom enable yes || exit 0
    rcService serfdom active no  && exit 0
    serfdom_signal TERM
    sleep 2
    rm -f $serfdom_pidfile >/dev/null 2>&1 || true

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

%reload -u @l_rusr@
    rcService serfdom enable yes || exit 0
    rcService serfdom active no  && exit 0
    @l_prefix@/sbin/serfdom reload

%daily -u @l_susr@
    rcService serfdom enable yes || exit 0
    shtool rotate -f \
        -n ${serfdom_log_numfiles} -s ${serfdom_log_minsize} -d \
        -z ${serfdom_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${serfdom_log_prolog}" \
        -E "${serfdom_log_epilog}; rc serfdom restart" \
        $serfdom_logfile

