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

%config
    opendkim_enable="$openpkg_rc_def"
    opendkim_selector="main"
    opendkim_domain="example.com"
    opendkim_keyfile="@l_prefix@/etc/opendkim/mail.private"
    opendkim_flags="-c relaxed -h"
    opendkim_prolog="true"
    opendkim_epilog="true"
    opendkim_numfiles="10"
    opendkim_minsize="1M"
    opendkim_complevel="9"

%common
    opendkim_cfgfile="@l_prefix@/etc/opendkim/opendkim.conf"
    opendkim_tmpdir="@l_prefix@/var/opendkim/opendkim.tmp"
    opendkim_pidfile="@l_prefix@/var/opendkim/opendkim.pid"
    opendkim_logfile="@l_prefix@/var/opendkim/opendkim.log"
    opendkim_socket="@l_prefix@/var/milter/socket/opendkim"
    opendkim_signal () {
        [ -f $opendkim_pidfile ] && kill -$1 `cat $opendkim_pidfile`
    }

%status -u @l_susr@ -o
    opendkim_usable="unknown"
    opendkim_active="no"
    rcService opendkim enable yes && \
        opendkim_signal 0 && opendkim_active="yes"
    echo "opendkim_enable=\"$opendkim_enable\""
    echo "opendkim_usable=\"$opendkim_usable\""
    echo "opendkim_active=\"$opendkim_active\""

%start -u @l_susr@
    rcService opendkim enable yes || exit 0
    rcService opendkim active yes && exit 0
    cmd="@l_prefix@/sbin/opendkim"
    cmd="$cmd -l -u @l_rusr@"
    cmd="$cmd -p local:$opendkim_socket"
    cmd="$cmd -P $opendkim_pidfile"
    if [ ".$opendkim_selector" != . ]; then
        cmd="$cmd -s \"$opendkim_selector\""
    fi
    if [ ".$opendkim_domain" != . ]; then
        cmd="$cmd -d \"$opendkim_domain\""
    fi
    if [ ".$opendkim_keyfile" != . ]; then
        cmd="$cmd -k \"$opendkim_keyfile\""
    fi
    cmd="$cmd -x \"$opendkim_cfgfile\""
    cmd="$cmd $opendkim_flags"
    ( umask 002
      export DKIM_TMPDIR="$opendkim_tmpdir"
      rm -f $opendkim_socket >/dev/null 2>&1 || true
      eval $cmd
    ) || exit $?

%stop -u @l_susr@
    rcService opendkim enable yes || exit 0
    rcService opendkim active no  && exit 0
    opendkim_signal TERM
    sleep 2
    rm -f $opendkim_pidfile >/dev/null 2>&1 || true
    rm -f $opendkim_socket  >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService opendkim enable yes || exit 0
    shtool rotate -f \
        -n $opendkim_numfiles -s $opendkim_minsize -d \
        -z $opendkim_complevel -m 644 -o @l_rusr@ -g @l_rgrp@ \
        -P "$opendkim_prolog" \
        -E "$opendkim_epilog; rc opendkim restart" \
        $opendkim_logfile

