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

%config
    imapd_enable="$openpkg_rc_def"
    imapd_flags=""
    imapd_log_prolog="true"
    imapd_log_epilog="true"
    imapd_log_numfiles="10"
    imapd_log_minsize="1M"
    imapd_log_complevel="9"
    imapd_log_daemons="cyrmaster imapd pop3d sieve lmtpd idled cyr_db misc"
    POP_type="cyrus"
    POP_logfile="@l_prefix@/var/imapd/log/pop3d.log"

%common
    imapd_pidfile="@l_prefix@/var/imapd/cyrmaster.pid"
    imapd_signal () {
        [ -f $imapd_pidfile ] && kill -$1 `cat $imapd_pidfile`
    }

%status -u @l_susr@ -o
    imapd_usable="unknown"
    imapd_active="no"
    rcService imapd enable yes && \
        imapd_signal 0 && imapd_active="yes"
    echo "imapd_enable=\"$imapd_enable\""
    echo "imapd_usable=\"$imapd_usable\""
    echo "imapd_active=\"$imapd_active\""

%start -u @l_susr@
    rcService imapd enable yes || exit 0
    rcService imapd active yes && exit 0
    @l_prefix@/bin/cyrmaster -d ${imapd_flags}

%stop -u @l_susr@
    rcService imapd enable yes || exit 0
    rcService imapd active no && exit 0
    imapd_signal TERM
    rm -f $imapd_pidfile >/dev/null 2>&1 || true

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

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

%daily -u @l_susr@
    rcService imapd enable yes || exit 0
    rcTmp -i
    hintfile=`rcTmp -f -n hint`
    for daemon in $imapd_log_daemons; do
        if [ -f @l_prefix@/var/imapd/log/$daemon.log ]; then
            shtool rotate -f \
                -n $imapd_log_numfiles -s $imapd_log_minsize -d \
                -z $imapd_log_complevel -m 664 -o @l_rusr@ -g @l_rgrp@ \
                -P "$imapd_log_prolog" \
                -E "$imapd_log_epilog; echo 1 >$hintfile" \
                @l_prefix@/var/imapd/log/$daemon.log
        fi
    done
    if [ -s $hintfile ]; then
        rc imapd restart
    fi
    rcTmp -k

