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

%config
    amavisd_enable="$openpkg_rc_def"
    amavisd_daemons="amavisd amavis-milter"
    amavisd_log_prolog="true"
    amavisd_log_epilog="true"
    amavisd_log_numfiles="10"
    amavisd_log_minsize="1M"
    amavisd_log_complevel="9"
    amavisd_sum_flags=""

%common
    amavisd_cfgfile="@l_prefix@/etc/amavisd/amavisd.conf"
    amavisd_pidfile_amavisd="@l_prefix@/var/amavisd/amavisd.pid"
    amavisd_pidfile_amavis_milter="@l_prefix@/var/amavisd/amavis-milter.pid"
    amavisd_socket_amavis_milter="@l_prefix@/var/milter/socket/amavis-milter"
    amavisd_signal () {
        [ -f $amavisd_pidfile_amavisd ] \
            && kill -$1 `cat $amavisd_pidfile_amavisd`
        local rc_amavisd_amavisd=$?
        [ -f $amavisd_pidfile_amavis_milter ] \
            && kill -$1 `cat $amavisd_pidfile_amavis_milter`
        local rc_amavisd_amavis_milter=$?
        [    $rc_amavisd_amavisd       -eq 0 \
          -o $rc_amavisd_amavis_milter -eq 0 ]
    }

%status -u @l_rusr@ -o
    amavisd_usable="unknown"
    amavisd_active="no"
    rcService amavisd enable yes && \
        amavisd_signal 0 && amavisd_active="yes"
    echo "amavisd_enable=\"$amavisd_enable\""
    echo "amavisd_usable=\"$amavisd_usable\""
    echo "amavisd_active=\"$amavisd_active\""

%start -u @l_susr@
    rcService amavisd enable yes || exit 0
    rcService amavisd active yes && exit 0
    for daemon in $amavisd_daemons; do
        if [ ".$daemon" = ".amavisd" ]; then
            @l_prefix@/sbin/amavisd -c $amavisd_cfgfile
        elif [ ".$daemon" = ".amavis-milter" -a -x @l_prefix@/sbin/amavis-milter ]; then
            rm -f $amavisd_socket_amavis_milter
            ( umask 002
              @l_prefix@/sbin/amavis-milter \
                  -p local:$amavisd_socket_amavis_milter \
                  -P $amavisd_pidfile_amavis_milter
            ) || exit $?
        fi
    done

%stop -u @l_susr@
    rcService amavisd enable yes || exit 0
    rcService amavisd active no && exit 0
    @l_prefix@/sbin/amavisd -c $amavisd_cfgfile stop
    amavisd_signal TERM
    sleep 1
    rm -f $amavisd_socket_amavis_milter

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

%daily -u @l_susr@
    rcService amavisd enable yes || exit 0
    shtool rotate -f \
        -n ${amavisd_log_numfiles} -s 0 \
        -z ${amavisd_log_complevel} -m 640 -o @l_rusr@ -g @l_rgrp@ \
        @l_prefix@/var/amavisd/amavisd.sum
    logfiles="@l_prefix@/var/amavisd/amavisd.log"
    if [ -f "@l_prefix@/var/amavisd/amavisd.log.0" ]; then
        logfiles="@l_prefix@/var/amavisd/amavisd.log.0 $logfiles"
    fi
    cat ${logfiles} | @l_prefix@/sbin/amavislogsumm \
        -d yesterday -R -B ${amavisd_sum_flags} \
        >@l_prefix@/var/amavisd/amavisd.sum 2>/dev/null
    shtool rotate -f \
        -n ${amavisd_log_numfiles} -s ${amavisd_log_minsize} -d \
        -z ${amavisd_log_complevel} -m 640 -o @l_rusr@ -g @l_rgrp@ \
        -P "${amavisd_log_prolog}" \
        -E "${amavisd_log_epilog}; rc amavisd restart" \
        @l_prefix@/var/amavisd/amavisd.log

