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

%config
    flowtools_enable="$openpkg_rc_def"
    flowtools_fanout="no"
    flowtools_fanout_flags="-S5 -V5"
    flowtools_fanout_listen="127.0.0.1/0/4433 0/127.0.0.1/4434 0/127.0.0.1/4435"
    flowtools_capture="no"
    flowtools_capture_flags="-S5 -V5 -N-1 -n95 -e665 -z4"
    flowtools_capture_listen="127.0.0.1/0/4432"
    flowtools_capture_workdir="@l_prefix@/var/flowtools/db"
    flowtools_capture_log_prolog="true"
    flowtools_capture_log_epilog="true"
    flowtools_capture_log_numfiles="10"
    flowtools_capture_log_minsize="1M"
    flowtools_capture_log_complevel="9"

%common
    flowtools_logfile="@l_prefix@/var/flowtools/flowtools.log"
    flowtools_fanout_pidfile="@l_prefix@/var/flowtools/flow-fanout.pid"
    flowtools_fanout_signal () {
        [ -f $flowtools_fanout_pidfile ] && kill -$1 `cat $flowtools_fanout_pidfile`
    }
    flowtools_capture_pidfile="@l_prefix@/var/flowtools/flow-capture.pid"
    flowtools_capture_signal () {
        [ -f $flowtools_capture_pidfile ] && kill -$1 `cat $flowtools_capture_pidfile`
    }

%status -u @l_rusr@ -o
    flowtools_usable="unknown"
    flowtools_active="no"
    rcService flowtools enable yes && rcVarIsYes flowtools_fanout && \
        flowtools_fanout_signal 0 && flowtools_active="yes"
    rcService flowtools enable yes && rcVarIsYes flowtools_capture && \
        flowtools_capture_signal 0 && flowtools_active="yes"
    echo "flowtools_enable=\"$flowtools_enable\""
    echo "flowtools_usable=\"$flowtools_usable\""
    echo "flowtools_active=\"$flowtools_active\""

%start -u @l_rusr@
    rcService flowtools enable yes || exit 0
    rcService flowtools active yes && exit 0
    if rcVarIsYes flowtools_fanout; then
        eval @l_prefix@/bin/flow-fanout \
            $flowtools_fanout_flags \
            -p "$flowtools_fanout_pidfile" \
            "$flowtools_fanout_listen"
    fi
    if rcVarIsYes flowtools_capture; then
        eval @l_prefix@/bin/flow-capture \
            $flowtools_capture_flags \
            -p "$flowtools_capture_pidfile" \
            -w "$flowtools_capture_workdir" \
            "$flowtools_capture_listen"
    fi

%stop -u @l_rusr@
    rcService flowtools enable yes || exit 0
    rcService flowtools active no  && exit 0
    if rcVarIsYes flowtools_fanout; then
        flowtools_fanout_signal TERM
        sleep 2
        rm -f $flowtools_fanout_pidfile
    fi
    if rcVarIsYes flowtools_capture; then
        flowtools_capture_signal TERM
        sleep 2
        rm -f $flowtools_capture_pidfile
    fi

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

%daily -u @l_rusr@
    rcService flowtools enable yes || exit 0
    rcVarIsYes flowtools_capture || exit 0
    rmdir $flowtools_capture_workdir/* >/dev/null 2>&1 || true
    shtool rotate -f \
        -n ${flowtools_capture_log_numfiles} -s ${flowtools_capture_log_minsize} -d \
        -z ${flowtools_capture_log_complevel} -m 644 -o @l_rusr@ -g @l_rgrp@ \
        -P "${flowtools_capture_log_prolog}" \
        -E "${flowtools_capture_log_epilog}; rc flowtools restart" \
        $flowtools_logfile

