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

%config
    smtpfeed_enable="$openpkg_rc_def"
    smtpfeed_flags="-u -V"
    smtpfeed_bind="127.0.0.1"
    smtpfeed_port="2525"
    smtpfeed_source_addr=""
    smtpfeed_source_port=""
    smtpfeed_hostname="localhost"
    smtpfeed_timeout_connect="1m"
    smtpfeed_timeout_greet="2m"
    smtpfeed_timeout_rset="2m"
    smtpfeed_maxrcpt="100"
    smtpfeed_maxsize="4194304"
    smtpfeed_log_prolog="true"
    smtpfeed_log_epilog="true"
    smtpfeed_log_numfiles="10"
    smtpfeed_log_minsize="1M"
    smtpfeed_log_complevel="9"

%common
    smtpfeed_pidfile="@l_prefix@/var/smtpfeed/smtpfeed.pid"
    smtpfeed_args="${smtpfeed_flags:--u -V} -E -l mail"
    smtpfeed_args="$smtpfeed_args -t connect=${smtpfeed_timeout_connect:-1m}"
    smtpfeed_args="$smtpfeed_args -t greet=${smtpfeed_timeout_greet:-2m}"
    smtpfeed_args="$smtpfeed_args -t rset=${smtpfeed_timeout_rset:-2m}"
    smtpfeed_args="$smtpfeed_args ${smtpfeed_source_addr:+-o }${smtpfeed_source_addr}"
    smtpfeed_args="$smtpfeed_args ${smtpfeed_source_port:+-p }${smtpfeed_source_port}"
    smtpfeed_args="$smtpfeed_args -c ${smtpfeed_hostname:-localhost}"
    smtpfeed_args="$smtpfeed_args -r 1 -R ${smtpfeed_maxrcpt:-100}"
    smtpfeed_args="$smtpfeed_args -s ${smtpfeed_maxsize:-4194304}"
    smtpfeed_signal () {
        [ -f $smtpfeed_pidfile ] && kill -$1 `cat $smtpfeed_pidfile`
    }
    smtpfeed_start () {
        ( @l_prefix@/sbin/smtpfeedd ${smtpfeed_bind}${smtpfeed_bind:+:}${smtpfeed_port:-2525} \
          @l_prefix@/sbin/smtpfeed ${smtpfeed_args} \
          </dev/null >/dev/null 2>&1 &
          echo $! >${smtpfeed_pidfile}
        ) </dev/null >/dev/null 2>&1
    }
    smtpfeed_stop () {
        smtpfeed_signal TERM
        rm -f $smtpfeed_pidfile 2>/dev/null || true
    }

%status -u @l_susr@ -o
    smtpfeed_usable="unknown"
    smtpfeed_active="no"
    rcService smtpfeed enable yes && \
        smtpfeed_signal 0 && smtpfeed_active="yes"
    echo "smtpfeed_enable=\"$smtpfeed_enable\""
    echo "smtpfeed_usable=\"$smtpfeed_usable\""
    echo "smtpfeed_active=\"$smtpfeed_active\""

%start -u @l_susr@
    rcService smtpfeed enable yes || exit 0
    rcService smtpfeed active yes && exit 0
    smtpfeed_start

%stop -u @l_susr@
    rcService smtpfeed enable yes || exit 0
    rcService smtpfeed active no  && exit 0
    smtpfeed_stop

%restart -u @l_susr@
    rcService smtpfeed enable yes || exit 0
    rcService smtpfeed active no  && exit 0
    smtpfeed_stop
    sleep 2
    smtpfeed_start

%daily -u @l_susr@
    rcService smtpfeed enable yes || exit 0
    shtool rotate -f \
        -n ${smtpfeed_log_numfiles} -s ${smtpfeed_log_minsize} -d \
        -z ${smtpfeed_log_complevel} -m 644 -o @l_susr@ -g @l_mgrp@ \
        -P "${smtpfeed_log_prolog}" \
        -E "${smtpfeed_log_epilog}; rc smtpfeed restart" \
        @l_prefix@/var/smtpfeed/smtpfeed.log

