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

%config
    fluentbit_enable="$openpkg_rc_def"
    fluentbit_flags=""
    fluentbit_log_prolog="true"
    fluentbit_log_epilog="true"
    fluentbit_log_numfiles="10"
    fluentbit_log_minsize="1M"
    fluentbit_log_complevel="9"

%common
    fluentbit_cfgfile="@l_prefix@/etc/fluentbit/fluentbit.conf"
    fluentbit_pidfile="@l_prefix@/var/fluentbit/run/fluentbit.pid"
    fluentbit_logfile="@l_prefix@/var/fluentbit/log/fluentbit.log"
    fluentbit_signal () {
        [ -f $fluentbit_pidfile ] && kill -$1 `cat $fluentbit_pidfile`
    }

%status -u @l_rusr@ -o
    fluentbit_usable="unknown"
    fluentbit_active="no"
    rcService fluentbit enable yes && \
        fluentbit_signal 0 && fluentbit_active="yes"
    echo "fluentbit_enable=\"$fluentbit_enable\""
    echo "fluentbit_usable=\"$fluentbit_usable\""
    echo "fluentbit_active=\"$fluentbit_active\""

%start -u @l_rusr@
    rcService fluentbit enable yes || exit 0
    rcService fluentbit active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/fluent-bit \
            -d \
            -c $fluentbit_cfgfile \
            -l $fluentbit_logfile \
            $fluentbit_flags \
            </dev/null >>$fluentbit_logfile 2>&1 &
        echo $! >$fluentbit_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService fluentbit enable yes || exit 0
    shtool rotate -f \
        -n $fluentbit_log_numfiles -s $fluentbit_log_minsize -d \
        -z $fluentbit_log_complevel -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "$fluentbit_log_prolog" \
        -E "$fluentbit_log_epilog; rc fluentbit reload" \
        $fluentbit_logfile

