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

%config
    dbmail_enable="$openpkg_rc_def"
    dbmail_protocols="imap lmtp pop3 timsieve"
    dbmail_flags_imapd=""
    dbmail_flags_lmtpd=""
    dbmail_flags_pop3d=""
    dbmail_flags_timsieved=""
    dbmail_log_prolog="true"
    dbmail_log_epilog="true"
    dbmail_log_numfiles="10"
    dbmail_log_minsize="1M"
    dbmail_log_complevel="9"
    dbmail_log_tags="imapd lmtpd pop3d timsieved smtp sievecmd adduser maint misc output"
    POP_type="dbmail"
    POP_logfile="@l_prefix@/var/dbmail/log/dbmail-pop3.log"

%common
    dbmail_logdir="@l_prefix@/var/dbmail/log"
    dbmail_pidfile_imapd="@l_prefix@/var/dbmail/run/dbmail-imapd.pid"
    dbmail_pidfile_lmtpd="@l_prefix@/var/dbmail/run/dbmail-lmtpd.pid"
    dbmail_pidfile_pop3d="@l_prefix@/var/dbmail/run/dbmail-pop3d.pid"
    dbmail_pidfile_timsieved="@l_prefix@/var/dbmail/run/dbmail-timsieved.pid"
    dbmail_signal () {
        [ -f $dbmail_pidfile_imapd ] \
            && kill -$1 `cat $dbmail_pidfile_imapd`
        local rc_dbmail_imapd=$?
        [ -f $dbmail_pidfile_lmtpd ] \
            && kill -$1 `cat $dbmail_pidfile_lmtpd`
        local rc_dbmail_lmtpd=$?
        [ -f $dbmail_pidfile_pop3d ] \
            && kill -$1 `cat $dbmail_pidfile_pop3d`
        local rc_dbmail_pop3d=$?
        [ -f $dbmail_pidfile_timsieved ] \
            && kill -$1 `cat $dbmail_pidfile_timsieved`
        local rc_dbmail_timsieved=$?
        [ $rc_dbmail_imapd     -eq 0 -o \
          $rc_dbmail_lmtpd     -eq 0 -o \
          $rc_dbmail_pop3d     -eq 0 -o \
          $rc_dbmail_timsieved -eq 0 ]
    }

%status -u @l_susr@ -o
    dbmail_usable="unknown"
    dbmail_active="no"
    rcService dbmail enable yes && \
        dbmail_signal 0 && dbmail_active="yes"
    echo "dbmail_enable=\"$dbmail_enable\""
    echo "dbmail_usable=\"$dbmail_usable\""
    echo "dbmail_active=\"$dbmail_active\""

%start -u @l_susr@ -p 450
    rcService dbmail enable yes || exit 0
    rcService dbmail active yes && exit 0
    for protocol in $dbmail_protocols; do
        case "$protocol" in
            imap     ) @l_prefix@/sbin/dbmail-imapd ${dbmail_flags_imapd} ;;
            lmtp     ) @l_prefix@/sbin/dbmail-lmtpd ${dbmail_flags_lmtpd} ;;
            pop3     ) @l_prefix@/sbin/dbmail-pop3d ${dbmail_flags_pop3d} ;;
            timsieve ) @l_prefix@/sbin/dbmail-timsieved ${dbmail_flags_timsieved} ;;
        esac
    done

%stop -u @l_susr@ -p 550
    rcService dbmail enable yes || exit 0
    rcService dbmail active no  && exit 0
    dbmail_signal TERM
    sleep 5

%restart -u @l_susr@
    rcService dbmail enable yes || exit 0
    rcService dbmail active no  && exit 0
    rc dbmail stop
    sleep 10
    rc dbmail stop

%hourly -u @l_susr@
    rcService dbmail enable yes || exit 0
    rcService dbmail active no  && exit 0
    @l_prefix@/sbin/dbmail-util -l 1h -qq

%daily -u @l_susr@
    rcService dbmail enable yes || exit 0
    rcService dbmail active no  && exit 0
    @l_prefix@/sbin/dbmail-util -aqy
    for tag in $dbmail_log_tags; do
        if [ -f $dbmail_logdir/dbmail-$tag.log ]; then
            shtool rotate -f \
                -n ${dbmail_log_numfiles} -s ${dbmail_log_minsize} -d \
                -z ${dbmail_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
                -P "${dbmail_log_prolog}" \
                -E "${dbmail_log_epilog}" \
                $dbmail_logdir/dbmail-$tag.log
        fi
    done

