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

%config
    sec_enable="$openpkg_rc_def"
    sec_log_prolog="true"
    sec_log_epilog="true"
    sec_log_numfiles="10"
    sec_log_minsize="1M"
    sec_log_complevel="9"

%common
    sec_conffile="@l_prefix@/etc/sec/sec.conf"
    sec_rulefile="@l_prefix@/etc/sec/sec.rule"
    sec_pidfile="@l_prefix@/var/sec/sec.pid"
    sec_logfile="@l_prefix@/var/sec/sec.log"
    sec_dmpfile="@l_prefix@/var/sec/sec.dmp"
    sec_signal () {
        [ -f $sec_pidfile ] && kill -$1 `cat $sec_pidfile`
    }

%status -u @l_susr@ -o
    sec_usable="no"
    sec_active="no"
    @l_prefix@/bin/sec -testonly -conf $sec_rulefile >/dev/null 2>&1 && sec_usable="yes"
    rcService sec enable yes && sec_signal 0 && sec_active="yes"
    echo "sec_enable=\"$sec_enable\""
    echo "sec_usable=\"$sec_usable\""
    echo "sec_active=\"$sec_active\""

%start -p 100 -u @l_susr@
    rcService sec enable yes || exit 0
    rcService sec active yes && exit 0
    sec_flags=`sed <$sec_conffile \
        -e 's;^;X;' \
        -e '/^X.*#.*$/d' \
        -e '/^X[ 	]*$/d' \
        -e 's;^X\([a-zA-Z][a-zA-Z0-9_]*\)[ 	][ 	]*\(..*\)$;--\1="\2";' \
        -e 's;^X\([a-zA-Z][a-zA-Z0-9_]*\)[ 	]*$;--\1;' \
        -e 's;^X.*;;' | tr '\012' ' '`
    eval @l_prefix@/bin/sec \
        -detach \
        -conf $sec_rulefile \
        -pid $sec_pidfile \
        -log $sec_logfile \
        -dump $sec_dmpfile \
        ${sec_flags}

%stop -p 900 -u @l_susr@
    rcService sec enable yes || exit 0
    rcService sec active no  && exit 0
    sec_signal TERM
    sleep 2
    rm -f $sec_pidfile >/dev/null 2>&1 || true

%restart -p 100 -u @l_susr@
    rcService sec enable yes || exit 0
    rcService sec active no  && exit 0
    rc sec stop start

%daily -u @l_susr@
    rcService sec enable yes || exit 0
    shtool rotate -f \
        -n ${sec_log_numfiles} -s ${sec_log_minsize} -d \
        -z ${sec_log_complevel} -m 644 -o @l_rusr@ -g @l_rgrp@ \
        -P "${sec_log_prolog}" \
        -E "${sec_log_epilog} && rc sec reload" \
        $sec_logfile

