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

%config
    MTA_name="postfix"
    MTA_aliases_file="@l_prefix@/etc/postfix/aliases"
    MTA_aliases_update="cd @l_prefix@/etc/postfix && @l_prefix@/sbin/postalias aliases"
    postfix_enable="$openpkg_rc_def"
    postfix_log_prolog="true"
    postfix_log_epilog="true"
    postfix_log_numfiles="10"
    postfix_log_minsize="1M"
    postfix_log_complevel="9"
    postfix_sum_flags=""

%status -u @l_susr@ -o
    postfix_usable="no"
    postfix_active="no"
    @l_prefix@/sbin/postfix check >/dev/null 2>&1 && postfix_usable="yes"
    @l_prefix@/libexec/postfix/master -t >/dev/null 2>&1 || postfix_active="yes"
    echo "postfix_enable=\"$postfix_enable\""
    echo "postfix_usable=\"$postfix_usable\""
    echo "postfix_active=\"$postfix_active\""

%start -u @l_susr@
    rcService postfix enable yes || exit 0
    rcService postfix active yes && exit 0
    @l_prefix@/sbin/postfix start

%stop -u @l_susr@
    rcService postfix enable yes || exit 0
    rcService postfix active no  && exit 0
    @l_prefix@/sbin/postfix stop
    sleep 2

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

%reload -u @l_susr@
    rcService postfix enable yes || exit 0
    rcService postfix active no  && exit 0
    @l_prefix@/sbin/postfix reload

%daily -u @l_susr@
    rcService postfix enable yes || exit 0

    #   rotate summary logfile
    shtool rotate -f \
        -n ${postfix_log_numfiles} -s 0 \
        -z ${postfix_log_complevel} -m 644 -o @l_musr@ -g @l_mgrp@ \
        @l_prefix@/var/postfix/log/postfix.sum

    #   generate summary logfile
    logfiles="@l_prefix@/var/postfix/log/postfix.log"
    if [ -f "@l_prefix@/var/postfix/log/postfix.log.0" ]; then
        logfiles="$logfiles @l_prefix@/var/postfix/log/postfix.log.0"
    fi
    @l_prefix@/sbin/pflogsumm -d yesterday -h 10 -u 10 -i \
        --iso_date_time --problems_first --smtpd_stats --verbose_msg_detail \
        ${postfix_sum_flags} \
        ${logfiles} >@l_prefix@/var/postfix/log/postfix.sum 2>/dev/null

    #   rotate logfile
    shtool rotate -f \
        -n ${postfix_log_numfiles} -s ${postfix_log_minsize} -d \
        -z ${postfix_log_complevel} -m 644 -o @l_musr@ -g @l_mgrp@ \
        -P "$postfix_log_prolog" \
        -E "$postfix_log_epilog" \
        @l_prefix@/var/postfix/log/postfix.log

