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

%config
    milter_dk_enable="$openpkg_rc_def"
    milter_dk_flags=""
    milter_dk_user="@l_rusr@"
    milter_dk_prolog="true"
    milter_dk_epilog="true"
    milter_dk_numfiles="10"
    milter_dk_minsize="1M"
    milter_dk_complevel="9"

%common
    milter_dk_tmpdir="@l_prefix@/var/milter-dk/milter-dk.tmp"
    milter_dk_pidfile="@l_prefix@/var/milter-dk/milter-dk.pid"
    milter_dk_logfile="@l_prefix@/var/milter-dk/milter-dk.log"
    milter_dk_socket="@l_prefix@/var/milter/socket/milter-dk"
    milter_dk_signal () {
        [ -f $milter_dk_pidfile ] && kill -$1 `cat $milter_dk_pidfile`
    }

%status -u @l_susr@ -o
    milter_dk_usable="unknown"
    milter_dk_active="no"
    rcService milter-dk enable yes && \
        milter_dk_signal 0 && milter_dk_active="yes"
    echo "milter_dk_enable=\"$milter_dk_enable\""
    echo "milter_dk_usable=\"$milter_dk_usable\""
    echo "milter_dk_active=\"$milter_dk_active\""

%start -u @l_susr@
    rcService milter-dk enable yes || exit 0
    rcService milter-dk active yes && exit 0
    rm -f $milter_dk_socket >/dev/null 2>&2 || true
    ( umask 002
      export DK_TMPDIR="$milter_dk_tmpdir"
      @l_prefix@/sbin/dk-filter \
           -l -u $milter_dk_user \
           -p local:$milter_dk_socket \
           -P $milter_dk_pidfile \
           $milter_dk_flags
    ) || exit $?

%stop -u @l_susr@
    rcService milter-dk enable yes || exit 0
    rcService milter-dk active no  && exit 0
    milter_dk_signal TERM
    sleep 2
    rm -f $milter_dk_pidfile >/dev/null 2>&1 || true
    rm -f $milter_dk_socket  >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService milter-dk enable yes || exit 0
    shtool rotate -f \
        -n $milter_dk_numfiles -s $milter_dk_minsize -d \
        -z $milter_dk_complevel -m 644 -o @l_rusr@ -g @l_rgrp@ \
        -P "$milter_dk_prolog" \
        -E "$milter_dk_epilog; rc milter-dk restart" \
        $milter_dk_logfile

