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

%config
    cfengine_enable="$openpkg_rc_def"
    cfengine_cservd_flags=""
    cfengine_cenvd_flags=""
    cfengine_log_prolog="true"
    cfengine_log_epilog="true"
    cfengine_log_numfiles="10"
    cfengine_log_minsize="1M"
    cfengine_log_complevel="9"

%common
    cfengine_cfservd_pidfile="@l_prefix@/var/cfengine/cfservd.pid"
    cfengine_cfenvd_pidfile="@l_prefix@/var/cfengine/cfenvd.pid"
    cfengine_signal () {
        [ -f $cfengine_cfservd_pidfile ] \
            && kill -$1 `cat $cfengine_cfservd_pidfile`
        local rc=$?
        [ -f $cfengine_cfenvd_pidfile ] \
            && kill -$1 `cat $cfengine_cfenvd_pidfile` \
            && [ $rc -eq 0 ]
    }

%status -u @l_susr@ -o
    cfengine_usable="unknown"
    cfengine_active="no"
    rcService cfengine enable yes && \
        cfengine_signal 0 && cfengine_active="yes"
    echo "cfengine_enable=\"$cfengine_enable\""
    echo "cfengine_usable=\"$cfengine_usable\""
    echo "cfengine_active=\"$cfengine_active\""

%start -u @l_susr@
    rcService cfengine enable yes || exit 0
    rcService cfengine active yes && exit 0
    ( nohup @l_prefix@/sbin/cfservd \
          --no-fork $cfengine_cfservd_flags \
          </dev/null >/dev/null 2>&1 &
      echo $! >$cfengine_cfservd_pidfile
    ) >/dev/null 2>&1
    ( nohup @l_prefix@/sbin/cfenvd \
          --no-fork $cfengine_cfenvd_flags \
          </dev/null >/dev/null 2>&1 &
      echo $! >$cfengine_cfenvd_pidfile
    ) >/dev/null 2>&1

%stop -u @l_susr@
    rcService cfengine enable yes || exit 0
    rcService cfengine active no  && exit 0
    cfengine_signal TERM
    sleep 2
    rm -f $cfengine_cfservd_pidfile 2>/dev/null || true
    rm -f $cfengine_cfenvd_pidfile  2>/dev/null || true

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

%daily -u @l_susr@
    rcService cfengine enable yes || exit 0
    shtool rotate -f \
        -n ${cfengine_log_numfiles} -s ${cfengine_log_minsize} -d \
        -z ${cfengine_log_complevel} -o @l_rusr@ -g @l_rgrp@ -m 644 \
        -P "${cfengine_log_prolog}" \
        -E "${cfengine_log_epilog}" \
        @l_prefix@/var/cfengine/cfengine.log

