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

%config
    stuntman_enable="$openpkg_rc_def"
    stuntman_flags="--mode basic --primaryinterface 127.0.0.1 --primaryport 3478 --protocol udp"
    stuntman_log_prolog="true"
    stuntman_log_epilog="true"
    stuntman_log_numfiles="10"
    stuntman_log_minsize="1M"
    stuntman_log_complevel="9"

%common
    stuntman_pidfile="@l_prefix@/var/stuntman/stuntman.pid"
    stuntman_logfile="@l_prefix@/var/stuntman/stuntman.log"
    stuntman_signal () {
        [ -f $stuntman_pidfile ] && kill -$1 `cat $stuntman_pidfile`
    }

%status -u @l_susr@ -o
    stuntman_usable="unknown"
    stuntman_active="no"
    rcService stuntman enable yes && \
        stuntman_signal 0 && stuntman_active="yes"
    echo "stuntman_enable=\"$stuntman_enable\""
    echo "stuntman_usable=\"$stuntman_usable\""
    echo "stuntman_active=\"$stuntman_active\""

%start -u @l_susr@
    rcService stuntman enable yes || exit 0
    rcService stuntman active yes && exit 0
    cmd="@l_prefix@/sbin/stunserver ${stuntman_flags}"
    ( eval "nohup $cmd </dev/null >>$stuntman_logfile 2>&1 &"
      echo $! >$stuntman_pidfile
    ) >/dev/null 2>&1

%stop -u @l_susr@
    rcService stuntman enable yes || exit 0
    rcService stuntman active no  && exit 0
    stuntman_signal TERM
    sleep 2
    rm -f $stuntman_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService stuntman enable yes || exit 0
    shtool rotate -f \
        -n ${stuntman_log_numfiles} -s ${stuntman_log_minsize} -d \
        -z ${stuntman_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${stuntman_log_prolog}" \
        -E "${stuntman_log_epilog}; rc stuntman restart" \
        $stuntman_logfile

