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

%config
    pb4sd_enable="$openpkg_rc_def"
    pb4sd_dbfile="@l_prefix@/var/pb4sd/pb4sd.db"
    pb4sd_logfile="@l_prefix@/var/pb4sd/pb4sd.log"
    pb4sd_grace="3600"
    pb4sd_exclude="127.0.0.0/8"
    pb4sd_log_prolog="true"
    pb4sd_log_epilog="true"
    pb4sd_log_numfiles="10"
    pb4sd_log_minsize="1M"
    pb4sd_log_complevel="9"

%common
    pb4sd_pidfile="@l_prefix@/var/pb4sd/pb4sd.pid"
    pb4sd_signal () {
        [ -f $pb4sd_pidfile ] && kill -$1 `cat $pb4sd_pidfile`
    }

%status -u @l_susr@ -o
    pb4sd_usable="unknown"
    pb4sd_active="no"
    rcService pb4sd enable yes && \
        pb4sd_signal 0 && pb4sd_active="yes"
    echo "pb4sd_enable=\"$pb4sd_enable\""
    echo "pb4sd_usable=\"$pb4sd_usable\""
    echo "pb4sd_active=\"$pb4sd_active\""

%start -u @l_susr@
    rcService pb4sd enable yes || exit 0
    rcService pb4sd active yes && exit 0
    pb4sd_popserver="${POP_type:-qpopper}"
    pb4sd_infile="${POP_logfile:-@l_prefix@/var/qpopper/qpopper.log}"
    @l_prefix@/sbin/pb4sd \
        --daemon \
        --popserver="${pb4sd_popserver}" \
        --infile="${pb4sd_infile}" \
        --dbfile="${pb4sd_dbfile}" \
        --logfile="${pb4sd_logfile}" \
        --pidfile="${pb4sd_pidfile}" \
        --grace="${pb4sd_grace}" \
        --exclude="${pb4sd_exclude}"

%stop -u @l_susr@
    rcService pb4sd enable yes || exit 0
    rcService pb4sd active no && exit 0
    pb4sd_signal TERM

%restart -u @l_susr@
    rcService pb4sd enable yes || exit 0
    rcService pb4sd active no && exit 0
    rc pb4sd stop
    sleep 2
    rc pb4sd start

%daily -u @l_susr@
    rcService pb4sd enable yes || exit 0
    shtool rotate -f \
        -n ${pb4sd_log_numfiles} -s ${pb4sd_log_minsize} -d \
        -z ${pb4sd_log_complevel} -o @l_susr@ -g @l_mgrp@ -m 644 \
        -P "${pb4sd_log_prolog}" \
        -E "${pb4sd_log_epilog}; rc pb4sd restart" \
        ${pb4sd_logfile}

