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

%config
    milter_dkim_enable="$openpkg_rc_def"
    milter_dkim_selector="main"
    milter_dkim_domain="example.com"
    milter_dkim_keyfile="@l_prefix@/etc/milter-dkim/mail.example.com.pem"
    milter_dkim_flags="-c relaxed -h"
    milter_dkim_prolog="true"
    milter_dkim_epilog="true"
    milter_dkim_numfiles="10"
    milter_dkim_minsize="1M"
    milter_dkim_complevel="9"

%common
    milter_dkim_dkfile="@l_prefix@/etc/milter-dkim/milter-dkim.pem"
    milter_dkim_tmpdir="@l_prefix@/var/milter-dkim/milter-dkim.tmp"
    milter_dkim_pidfile="@l_prefix@/var/milter-dkim/milter-dkim.pid"
    milter_dkim_logfile="@l_prefix@/var/milter-dkim/milter-dkim.log"
    milter_dkim_socket="@l_prefix@/var/milter/socket/milter-dkim"
    milter_dkim_signal () {
        [ -f $milter_dkim_pidfile ] && kill -$1 `cat $milter_dkim_pidfile`
    }

%status -u @l_susr@ -o
    milter_dkim_usable="unknown"
    milter_dkim_active="no"
    rcService milter-dkim enable yes && \
        milter_dkim_signal 0 && milter_dkim_active="yes"
    echo "milter_dkim_enable=\"$milter_dkim_enable\""
    echo "milter_dkim_usable=\"$milter_dkim_usable\""
    echo "milter_dkim_active=\"$milter_dkim_active\""

%start -u @l_susr@
    rcService milter-dkim enable yes || exit 0
    rcService milter-dkim active yes && exit 0
    cmd="@l_prefix@/sbin/dkim-filter"
    cmd="$cmd -l -u @l_rusr@"
    cmd="$cmd -p local:$milter_dkim_socket"
    cmd="$cmd -P $milter_dkim_pidfile"
    if [ ".$milter_dkim_selector" != . ]; then
        cmd="$cmd -s \"$milter_dkim_selector\""
    fi
    if [ ".$milter_dkim_domain" != . ]; then
        cmd="$cmd -d \"$milter_dkim_domain\""
    fi
    if [ ".$milter_dkim_keyfile" != . ]; then
        cmd="$cmd -k \"$milter_dkim_keyfile\""
    fi
    cmd="$cmd $milter_dkim_flags"
    ( umask 002
      export DK_TMPDIR="$milter_dkim_tmpdir"
      rm -f $milter_dkim_socket >/dev/null 2>&1 || true
      eval $cmd
    ) || exit $?

%stop -u @l_susr@
    rcService milter-dkim enable yes || exit 0
    rcService milter-dkim active no  && exit 0
    milter_dkim_signal TERM
    sleep 2
    rm -f $milter_dkim_pidfile >/dev/null 2>&1 || true
    rm -f $milter_dkim_socket  >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService milter-dkim enable yes || exit 0
    shtool rotate -f \
        -n $milter_dkim_numfiles -s $milter_dkim_minsize -d \
        -z $milter_dkim_complevel -m 644 -o @l_rusr@ -g @l_rgrp@ \
        -P "$milter_dkim_prolog" \
        -E "$milter_dkim_epilog; rc milter-dkim restart" \
        $milter_dkim_logfile

