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

%config
    zetcd_enable="$openpkg_rc_def"
    zetcd_daemon="yes"
    zetcd_log_prolog="true"
    zetcd_log_epilog="true"
    zetcd_log_numfiles="10"
    zetcd_log_minsize="1M"
    zetcd_log_complevel="9"

%common
    zetcd_cfgfile="@l_prefix@/etc/zetcd/zetcd.conf"
    zetcd_pidfile="@l_prefix@/var/zetcd/run/zetcd.pid"
    zetcd_logfile="@l_prefix@/var/zetcd/log/zetcd.log"
    zetcd_signal () {
        [ -f $zetcd_pidfile ] && kill -$1 `cat $zetcd_pidfile`
    }

%status -u @l_rusr@ -o
    zetcd_usable="unknown"
    zetcd_active="no"
    rcService zetcd enable yes && \
        zetcd_signal 0 && zetcd_active="yes"
    echo "zetcd_enable=\"$zetcd_enable\""
    echo "zetcd_usable=\"$zetcd_usable\""
    echo "zetcd_active=\"$zetcd_active\""

%start -u @l_rusr@
    rcService zetcd enable yes || exit 0
    rcService zetcd active yes && exit 0
    rcVarIsYes zetcd_daemon || exit 0
    cmd="@l_prefix@/sbin/zetcd"
    while read line; do
        case "$line" in
            -* | --* ) cmd="$cmd $line" ;;
            * ) ;;
        esac
    done <$zetcd_cfgfile
    (   eval "nohup $cmd </dev/null >>$zetcd_logfile 2>&1 &"
        echo $! >$zetcd_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService zetcd enable yes || exit 0
    rcService zetcd active no  && exit 0
    rcVarIsYes zetcd_daemon || exit 0
    zetcd_signal TERM
    sleep 2
    rm -f $zetcd_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService zetcd enable yes || exit 0
    rcVarIsYes zetcd_daemon || exit 0
    shtool rotate -f \
        -n ${zetcd_log_numfiles} -s ${zetcd_log_minsize} -d \
        -z ${zetcd_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${zetcd_log_prolog}" \
        -E "${zetcd_log_epilog}; rc zetcd restart" \
        $zetcd_logfile

