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

%config
    pwmd_enable="$openpkg_rc_def"
    pwmd_flags=""
    pwmd_log_prolog="true"
    pwmd_log_epilog="true"
    pwmd_log_numfiles="10"
    pwmd_log_minsize="1M"
    pwmd_log_complevel="9"

%common
    pwmd_cfgfile="@l_prefix@/etc/pwmd/pwmd.cfg"
    pwmd_logfile="@l_prefix@/var/pwmd/pwmd.log"
    pwmd_pidfile="@l_prefix@/var/pwmd/pwmd.pid"
    pwmd_signal () {
        [ -f $pwmd_pidfile ] && kill -$1 `cat $pwmd_pidfile`
    }

%status -u @l_rusr@ -o
    pwmd_usable="unknown"
    pwmd_active="no"
    rcService pwmd enable yes && \
        pwmd_signal 0 && pwmd_active="yes"
    echo "pwmd_enable=\"$pwmd_enable\""
    echo "pwmd_usable=\"$pwmd_usable\""
    echo "pwmd_active=\"$pwmd_active\""

%start -u @l_rusr@
    rcService pwmd enable yes || exit 0
    rcService pwmd active yes && exit 0
    (   umask 0
        nohup @l_prefix@/bin/pwmd \
            -f $pwmd_cfgfile ${pwmd_flags} \
            </dev/null >/dev/null 2>&1 &
        echo $! >$pwmd_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService pwmd enable yes || exit 0
    rcService pwmd active no  && exit 0
    pwmd_signal TERM
    sleep 2
    rm -f $pwmd_pidfile >/dev/null 2>&1 || true

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

%reload -u @l_rusr@
    rcService pwmd enable yes || exit 0
    rcService pwmd active no  && exit 0
    pwmd_signal HUP
    sleep 1
    pwmd_signal USR1

%daily -u @l_rusr@
    rcService pwmd enable yes || exit 0
    shtool rotate -f \
        -n $pwmd_log_numfiles -s $pwmd_log_minsize -d \
        -z $pwmd_log_complevel -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "$pwmd_log_prolog" \
        -E "$pwmd_log_epilog; rc pwmd restart" \
        $pwmd_logfile

