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

%config
    beehive_enable="$openpkg_rc_def"
    beehive_flags="-bind 127.0.0.1:8181 -canonicalurl http://localhost:8181"
    beehive_log_prolog="true"
    beehive_log_epilog="true"
    beehive_log_numfiles="10"
    beehive_log_minsize="1M"
    beehive_log_complevel="9"

%common
    beehive_cfgfile="@l_prefix@/etc/beehive/beehive.conf"
    beehive_pidfile="@l_prefix@/var/beehive/run/beehive.pid"
    beehive_logfile="@l_prefix@/var/beehive/log/beehive.log"
    beehive_signal () {
        [ -f $beehive_pidfile ] && kill -$1 `cat $beehive_pidfile`
    }

%status -u @l_rusr@ -o
    beehive_usable="unknown"
    beehive_active="no"
    rcService beehive enable yes && \
        beehive_signal 0 && beehive_active="yes"
    echo "beehive_enable=\"$beehive_enable\""
    echo "beehive_usable=\"$beehive_usable\""
    echo "beehive_active=\"$beehive_active\""

%start -u @l_rusr@
    rcService beehive enable yes || exit 0
    rcService beehive active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        . @l_prefix@/etc/beehive/beehive.cf
        nohup @l_prefix@/sbin/beehive \
            -config $beehive_cfgfile \
            $beehive_flags \
        </dev/null >>$beehive_logfile 2>&1 &
        echo $! >$beehive_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService beehive enable yes || exit 0
    shtool rotate -f \
        -n ${beehive_log_numfiles} -s ${beehive_log_minsize} -d \
        -z ${beehive_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${beehive_log_prolog}" \
        -E "${beehive_log_epilog}; rc beehive restart" \
        $beehive_logfile

