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

%config
    pks_enable="$openpkg_rc_def"
    pks_log_prolog="true"
    pks_log_epilog="true"
    pks_log_numfiles="10"
    pks_log_minsize="1M"
    pks_log_complevel="9"

%common
    pks_pidfile="@l_prefix@/var/pks/pks-queue-run.pid"
    pks_signal () {
        [ -f $pks_pidfile ] && kill -$1 `cat $pks_pidfile`
    }

%status -u @l_rusr@ -o
    pks_usable="unknown"
    pks_active="no"
    rcService pks enable yes && \
        pks_signal 0 && pks_active="yes"
    echo "pks_enable=\"$pks_enable\""
    echo "pks_usable=\"$pks_usable\""
    echo "pks_active=\"$pks_active\""

%start -u @l_rusr@
    rcService pks enable yes || exit 0
    rcService pks active yes && exit 0
    (nohup @l_prefix@/sbin/pksd \
        @l_prefix@/etc/pks/pksd.conf \
        </dev/null >/dev/null 2>&1 &) >/dev/null 2>&1
    sleep 2
    (nohup @l_prefix@/bin/pks-queue-run \
        @l_prefix@/etc/pks/pksd.conf \
        </dev/null >/dev/null 2>&1 &
     echo $! >$pks_pidfile ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService pks enable yes || exit 0
    rcService pks active no && exit 0
    @l_prefix@/bin/pksdctl @l_prefix@/var/pks/pksd.ctl shutdown
    pks_signal TERM
    rm -f $pks_pidfile 2>/dev/null || true

%restart -u @l_rusr@
    rcService pks enable yes || exit 0
    rcService pks active no && exit 0
    rc pks stop
    sleep 2
    rc pks start

%daily -u @l_rusr@
    rcService pks enable yes || exit 0
    shtool rotate -f \
        -n ${pks_log_numfiles} -s ${pks_log_minsize} -d \
        -z ${pks_log_complevel} -o @l_rusr@ -g @l_mgrp@ -m 644 \
        -P "${pks_log_prolog}" \
        -E "${pks_log_epilog}; rc pks restart" \
        @l_prefix@/var/pks/pks.log

