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

%config
    milter_sa_enable="$openpkg_rc_def"
    milter_sa_flags="-a"
    milter_sa_prolog="true"
    milter_sa_epilog="true"
    milter_sa_numfiles="10"
    milter_sa_minsize="1M"
    milter_sa_complevel="9"

%common
    milter_sa_pidfile="@l_prefix@/var/milter-sa/milter-sa.pid"
    milter_sa_logfile="@l_prefix@/var/milter-sa/milter-sa.log"
    milter_sa_socket="@l_prefix@/var/milter/socket/milter-sa"
    milter_sa_signal () {
        [ -f $milter_sa_pidfile ] && kill -$1 `cat $milter_sa_pidfile`
    }

%status -u @l_susr@ -o
    milter_sa_usable="unknown"
    milter_sa_active="no"
    rcService milter-sa enable yes && \
        milter_sa_signal 0 && milter_sa_active="yes"
    echo "milter_sa_enable=\"$milter_sa_enable\""
    echo "milter_sa_usable=\"$milter_sa_usable\""
    echo "milter_sa_active=\"$milter_sa_active\""

%start -u @l_rusr@
    rcService milter-sa enable yes || exit 0
    rcService milter-sa active yes && exit 0
    rm -f $milter_sa_socket >/dev/null 2>&2 || true
    ( umask 002
      export PATH=@l_prefix@/bin:$PATH
      @l_prefix@/sbin/spamass-milter \
           -f -p local:$milter_sa_socket \
           -P $milter_sa_pidfile \
           $milter_sa_flags
    ) || exit $?

%stop -u @l_rusr@
    rcService milter-sa enable yes || exit 0
    rcService milter-sa active no  && exit 0
    milter_sa_signal TERM
    sleep 4
    rm -f $milter_sa_pidfile >/dev/null 2>&1 || true
    rm -f $milter_sa_socket  >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService milter-sa enable yes || exit 0
    shtool rotate -f \
        -n $milter_sa_numfiles -s $milter_sa_minsize -d \
        -z $milter_sa_complevel -m 644 -o @l_rusr@ -g @l_rgrp@ \
        -P "$milter_sa_prolog" \
        -E "$milter_sa_epilog; rc milter-sa restart" \
        $milter_sa_logfile

