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

%config
    dkron_enable="$openpkg_rc_def"
    dkron_flags="-server"
    dkron_log_prolog="true"
    dkron_log_epilog="true"
    dkron_log_numfiles="10"
    dkron_log_minsize="1M"
    dkron_log_complevel="9"

%common
    dkron_pidfile="@l_prefix@/var/dkron/run/dkron.pid"
    dkron_logfile="@l_prefix@/var/dkron/log/dkron.log"
    dkron_signal () {
        [ -f $dkron_pidfile ] && kill -$1 `cat $dkron_pidfile`
    }

%status -u @l_rusr@ -o
    dkron_usable="unknown"
    dkron_active="no"
    rcService dkron enable yes && \
        dkron_signal 0 && dkron_active="yes"
    echo "dkron_enable=\"$dkron_enable\""
    echo "dkron_usable=\"$dkron_usable\""
    echo "dkron_active=\"$dkron_active\""

%start -u @l_rusr@
    rcService dkron enable yes || exit 0
    rcService dkron active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/dkron agent \
           $dkron_flags \
        </dev/null >>$dkron_logfile 2>&1 &
        echo $! >$dkron_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService dkron enable yes || exit 0
    shtool rotate -f \
        -n ${dkron_log_numfiles} -s ${dkron_log_minsize} -d \
        -z ${dkron_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${dkron_log_prolog}" \
        -E "${dkron_log_epilog}; rc dkron restart" \
        $dkron_logfile

