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

%config
    jocko_enable="$openpkg_rc_def"
    jocko_flags=""
    jocko_log_prolog="true"
    jocko_log_epilog="true"
    jocko_log_numfiles="10"
    jocko_log_minsize="1M"
    jocko_log_complevel="9"

%common
    jocko_cfgfile="@l_prefix@/etc/jocko/jocko.conf"
    jocko_datdir="@l_prefix@/var/jocko/db"
    jocko_logdir="@l_prefix@/var/jocko/log"
    jocko_pidfile="@l_prefix@/var/jocko/run/jocko.pid"
    jocko_logfile="@l_prefix@/var/jocko/log/jocko.log"
    jocko_signal () {
        [ -f $jocko_pidfile ] && kill -$1 `cat $jocko_pidfile`
    }

%status -u @l_rusr@ -o
    jocko_usable="unknown"
    jocko_active="no"
    rcService jocko enable yes && \
        jocko_signal 0 && jocko_active="yes"
    echo "jocko_enable=\"$jocko_enable\""
    echo "jocko_usable=\"$jocko_usable\""
    echo "jocko_active=\"$jocko_active\""

%start -u @l_rusr@
    rcService jocko enable yes || exit 0
    rcService jocko active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        cd $jocko_datdir
        nohup @l_prefix@/sbin/jocko broker \
            `sed -e "/^ *#/d" -e '/^ *$/d' -e "s;^;--;" <$jocko_cfgfile | \
            sed -e ":a" -e "N" -e '$!ba' -e 's/\n/ /g'` \
            $jocko_flags \
            --data-dir=$jocko_datdir/logs \
        </dev/null >>$jocko_logfile 2>&1 &
        echo $! >$jocko_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService jocko enable yes || exit 0
    shtool rotate -f \
        -n ${jocko_log_numfiles} -s ${jocko_log_minsize} -d \
        -z ${jocko_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${jocko_log_prolog}" \
        -E "${jocko_log_epilog}; rc jocko restart" \
        $jocko_logfile

