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

%config
    dovecot_enable="$openpkg_rc_def"
    dovecot_log_prolog="true"
    dovecot_log_epilog="true"
    dovecot_log_numfiles="10"
    dovecot_log_minsize="1M"
    dovecot_log_complevel="9"
    dovecot_deliver_log_prolog="true"
    dovecot_deliver_log_epilog="true"
    dovecot_deliver_log_numfiles="10"
    dovecot_deliver_log_minsize="1M"
    dovecot_deliver_log_complevel="9"

%common
    dovecot_pidfile="@l_prefix@/var/dovecot/run/master.pid"
    dovecot_signal () {
        [ -f $dovecot_pidfile ] && kill -$1 `cat $dovecot_pidfile`
    }

%status -u @l_susr@ -o
    dovecot_usable="unknown"
    dovecot_active="no"
    rcService dovecot enable yes && \
        dovecot_signal 0 && dovecot_active="yes"
    echo "dovecot_enable=\"$dovecot_enable\""
    echo "dovecot_usable=\"$dovecot_usable\""
    echo "dovecot_active=\"$dovecot_active\""

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

%stop -u @l_susr@
    rcService dovecot enable yes || exit 0
    rcService dovecot active no  && exit 0
    dovecot_signal TERM
    sleep 2
    rm -f $dovecot_pidfile >/dev/null 2>&1 || true

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

%reload -u @l_susr@
    rcService dovecot enable yes || exit 0
    rcService dovecot active no  && exit 0
    dovecot_signal HUP

%daily -u @l_susr@
    rcService dovecot enable yes || exit 0
    rcTmp -i
    hintfile=`rcTmp -f -n hint`
    shtool rotate -f \
        -n ${dovecot_log_numfiles} -s ${dovecot_log_minsize} -d \
        -z ${dovecot_log_complevel} -m 600 -o @l_susr@ -g @l_sgrp@ \
        -P "${dovecot_log_prolog}" \
        -E "${dovecot_log_epilog}; echo 1 >$hintfile" \
        @l_prefix@/var/dovecot/log/dovecot.log
    if [ -s $hintfile ]; then
        dovecot_signal USR1
    fi
    rcTmp -k
    shtool rotate -f \
        -n ${dovecot_deliver_log_numfiles} -s ${dovecot_deliver_log_minsize} -d \
        -z ${dovecot_deliver_log_complevel} -m 600 -o @l_rusr@ -g @l_rgrp@ \
        -P "${dovecot_deliver_log_prolog}" \
        -E "${dovecot_deliver_log_epilog}" \
        @l_prefix@/var/dovecot/log/deliver.log

