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

%config
    MTA_name="sendmail"
    MTA_aliases_file="@l_prefix@/etc/sendmail/t.aliases"
    MTA_aliases_update="cd @l_prefix@/etc/sendmail && make t.aliases.db"
    sendmail_enable="$openpkg_rc_def"
    sendmail_flags_in="-bd"
    sendmail_flags_out="-q60s"
    sendmail_flags_msp="-Ac -q60s"
    sendmail_flags=""
    sendmail_wait_timeout="60"
    sendmail_log_prolog="true"
    sendmail_log_epilog="true"
    sendmail_log_numfiles="10"
    sendmail_log_minsize="1M"
    sendmail_log_complevel="9"

%common
    sendmail_pidfile_in="@l_prefix@/var/sendmail/sendmail.in.pid"
    sendmail_pidfile_out="@l_prefix@/var/sendmail/sendmail.out.pid"
    sendmail_pidfile_msp="@l_prefix@/var/sendmail/clientmqueue/sendmail.msp.pid"
    sendmail_start () {
        if rcService sendmail enable yes; then
            @l_prefix@/sbin/sendmail $sendmail_flags_in $sendmail_flags \
                -OPidFile=$sendmail_pidfile_in
            @l_prefix@/sbin/sendmail $sendmail_flags_out $sendmail_flags \
                -OPidFile=$sendmail_pidfile_out
            @l_prefix@/sbin/sendmail $sendmail_flags_msp $sendmail_flags \
                -OPidFile=$sendmail_pidfile_msp
        fi
    }
    sendmail_signal1 () {
        [ -f $2 ] && kill -$1 `cat $2`
    }
    sendmail_signal () {
        local rc
        sendmail_signal1 $1 $sendmail_pidfile_in
        rc=$?
        sendmail_signal1 $1 $sendmail_pidfile_out \
        && [ $rc -eq 0 ]
        rc=$?
        sendmail_signal1 $1 $sendmail_pidfile_msp \
        && [ $rc -eq 0 ]
    }
    sendmail_wait1 () {
        local i=0
        while [ $i -lt $sendmail_wait_timeout -a sendmail_signal1 0 $1 ]; do
            sleep 1
            i=`expr $i + 1`
        done
        rm -f $1 2>/dev/null || true
    }
    sendmail_wait () {
        sendmail_wait1 $sendmail_pidfile_in
        sendmail_wait1 $sendmail_pidfile_out
        sendmail_wait1 $sendmail_pidfile_msp
    }

%status -u @l_susr@ -o
    sendmail_usable="unknown"
    sendmail_active="no"
    rcService sendmail enable yes && \
        sendmail_signal 0 && sendmail_active="yes"
    echo "sendmail_enable=\"$sendmail_enable\""
    echo "sendmail_usable=\"$sendmail_usable\""
    echo "sendmail_active=\"$sendmail_active\""

%start -u @l_susr@
    rcService sendmail enable yes || exit 0
    rcService sendmail active yes && exit 0
    sendmail_start

%stop -u @l_susr@
    rcService sendmail enable yes || exit 0
    rcService sendmail active no && exit 0
    sendmail_signal TERM
    sendmail_wait

%restart -u @l_susr@
    rcService sendmail enable yes || exit 0
    rcService sendmail active no && exit 0
    sendmail_signal TERM
    sendmail_wait
    sendmail_start

%reload -u @l_susr@
    rcService sendmail enable yes || exit 0
    rcService sendmail active no  && exit 0
    sendmail_signal TERM
    sendmail_wait
    sendmail_start

%daily -u @l_susr@
    rcService sendmail enable yes || exit 0
    shtool rotate -f \
        -n ${sendmail_log_numfiles} -s ${sendmail_log_minsize} -d \
        -z ${sendmail_log_complevel} -o @l_susr@ -g @l_mgrp@ -m 644 \
        -P "$sendmail_log_prolog" \
        -E "$sendmail_log_epilog && rc sendmail reload" \
        @l_prefix@/var/sendmail/sendmail.log

