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

%config
    kapacitor_enable="$openpkg_rc_def"
    kapacitor_flags=""
    kapacitor_log_prolog="true"
    kapacitor_log_epilog="true"
    kapacitor_log_numfiles="10"
    kapacitor_log_minsize="1M"
    kapacitor_log_complevel="9"

%common
    kapacitor_cfgfile="@l_prefix@/etc/kapacitor/kapacitor.conf"
    kapacitor_pidfile="@l_prefix@/var/kapacitor/run/kapacitor.pid"
    kapacitor_logfile="@l_prefix@/var/kapacitor/log/kapacitor.log"
    kapacitor_signal () {
        [ -f $kapacitor_pidfile ] && kill -$1 `cat $kapacitor_pidfile`
    }

%status -u @l_rusr@ -o
    kapacitor_usable="unknown"
    kapacitor_active="no"
    rcService kapacitor enable yes && \
        kapacitor_signal 0 && kapacitor_active="yes"
    echo "kapacitor_enable=\"$kapacitor_enable\""
    echo "kapacitor_usable=\"$kapacitor_usable\""
    echo "kapacitor_active=\"$kapacitor_active\""

%start -u @l_rusr@
    rcService kapacitor enable yes || exit 0
    rcService kapacitor active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/kapacitord \
           -config "$kapacitor_cfgfile" \
           $kapacitor_flags \
        </dev/null >>$kapacitor_logfile 2>&1 &
        echo $! >$kapacitor_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService kapacitor enable yes || exit 0
    shtool rotate -f \
        -n ${kapacitor_log_numfiles} -s ${kapacitor_log_minsize} -d \
        -z ${kapacitor_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${kapacitor_log_prolog}" \
        -E "${kapacitor_log_epilog}; rc kapacitor restart" \
        $kapacitor_logfile

