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

%config
    postgrey_enable="$openpkg_rc_def"
    postgrey_flags=""
    postgrey_delay="300"
    postgrey_maxage="28"
    postgrey_autowhitelist="5"
    postgrey_retrywin="2"
    postgrey_action="DEFER_IF_PERMIT"
    postgrey_text="Greylisted (%s seconds remaining)"
    postgrey_log_prolog="true"
    postgrey_log_epilog="true"
    postgrey_log_numfiles="10"
    postgrey_log_minsize="1M"
    postgrey_log_complevel="9"

%common
    postgrey_wlclnt="@l_prefix@/etc/postgrey/whitelist.clients"
    postgrey_wlrcpt="@l_prefix@/etc/postgrey/whitelist.recipients"
    postgrey_logfile="@l_prefix@/var/postgrey/postgrey.log"
    postgrey_pidfile="@l_prefix@/var/postgrey/postgrey.pid"
    postgrey_socket="@l_prefix@/var/postgrey/postgrey.socket"
    postgrey_dbdir="@l_prefix@/var/postgrey"
    postgrey_signal () {
        [ -f $postgrey_pidfile ] && kill -$1 `cat $postgrey_pidfile`
    }

%status -u @l_rusr@ -o
    postgrey_usable="unknown"
    postgrey_active="no"
    rcService postgrey enable yes && \
        postgrey_signal 0 && postgrey_active="yes"
    echo "postgrey_enable=\"$postgrey_enable\""
    echo "postgrey_usable=\"$postgrey_usable\""
    echo "postgrey_active=\"$postgrey_active\""

%start -u @l_rusr@
    rcService postgrey enable yes || exit 0
    rcService postgrey active yes && exit 0
    @l_prefix@/sbin/postgrey ${postgrey_flags} \
        --daemonize \
        --user="@l_rusr@" \
        --group="@l_rgrp@" \
        --pidfile="$postgrey_pidfile" \
        --unix="$postgrey_socket" \
        --dbdir="$postgrey_dbdir" \
        --delay="$postgrey_delay" \
        --max-age="$postgrey_maxage" \
        --retry-window="$postgrey_retrywin" \
        --auto-whitelist-clients="$postgrey_autowhitelist" \
        --whitelist-clients="$postgrey_wlclnt" \
        --whitelist-recipients="$postgrey_wlrcpt" \
        --greylist-action="$postgrey_action" \
        --greylist-text="$postgrey_text"

%stop -u @l_rusr@
    rcService postgrey enable yes || exit 0
    rcService postgrey active no && exit 0
    postgrey_signal TERM
    rm -f $postgrey_pidfile 2>/dev/null || true

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

%daily -u @l_susr@
    rcService postgrey enable yes || exit 0
    shtool rotate -f \
        -n ${postgrey_log_numfiles} -s ${postgrey_log_minsize} -d \
        -z ${postgrey_log_complevel} -o @l_rusr@ -g @l_mgrp@ -m 644 \
        -P "${postgrey_log_prolog}" \
        -E "${postgrey_log_epilog}; rc postgrey restart" \
        $postgrey_logfile

