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

%config
    milter_mopher_enable="$openpkg_rc_def"
    milter_mopher_flags=""
    milter_mopher_prolog="true"
    milter_mopher_epilog="true"
    milter_mopher_numfiles="10"
    milter_mopher_minsize="1M"
    milter_mopher_complevel="9"

%common
    milter_mopher_pidfile="@l_prefix@/var/milter-mopher/milter-mopher.pid"
    milter_mopher_logfile="@l_prefix@/var/milter-mopher/milter-mopher.log"
    milter_mopher_socket="@l_prefix@/var/milter/socket/milter-mopher"
    milter_mopher_signal () {
        [ -f $milter_mopher_pidfile ] && kill -$1 `cat $milter_mopher_pidfile`
    }

%status -u @l_susr@ -o
    milter_mopher_usable="unknown"
    milter_mopher_active="no"
    rcService milter-mopher enable yes && \
        milter_mopher_signal 0 && milter_mopher_active="yes"
    echo "milter_mopher_enable=\"$milter_mopher_enable\""
    echo "milter_mopher_usable=\"$milter_mopher_usable\""
    echo "milter_mopher_active=\"$milter_mopher_active\""

%start -u @l_susr@
    rcService milter-mopher enable yes || exit 0
    rcService milter-mopher active yes && exit 0
    cmd="@l_prefix@/sbin/mopherd"
    cmd="$cmd -f -p $milter_mopher_pidfile"
    cmd="$cmd $milter_mopher_flags"
    ( umask 002
      rm -f $milter_mopher_socket >/dev/null 2>&1 || true
      eval $cmd >>$milter_mopher_logfile 2>&1 </dev/null &
    ) || exit $?

%stop -u @l_susr@
    rcService milter-mopher enable yes || exit 0
    rcService milter-mopher active no  && exit 0
    milter_mopher_signal TERM
    sleep 2
    rm -f $milter_mopher_pidfile >/dev/null 2>&1 || true
    rm -f $milter_mopher_socket  >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService milter-mopher enable yes || exit 0
    shtool rotate -f \
        -n $milter_mopher_numfiles -s $milter_mopher_minsize -d \
        -z $milter_mopher_complevel -m 644 -o @l_rusr@ -g @l_rgrp@ \
        -P "$milter_mopher_prolog" \
        -E "$milter_mopher_epilog; rc milter-mopher restart" \
        $milter_mopher_logfile

