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

%config
    maradns_enable="$openpkg_rc_def"
    maradns_daemons="maradns zoneserver"
    maradns_log_prolog="true"
    maradns_log_epilog="true"
    maradns_log_numfiles="10"
    maradns_log_minsize="1M"
    maradns_log_complevel="9"

%common
    maradns_cfgfile="@l_prefix@/etc/maradns/mararc"
    maradns_logfile="@l_prefix@/var/maradns/maradns.log"
    maradns_pidfile_maradns="@l_prefix@/var/maradns/maradns.pid"
    maradns_pidfile_zoneserver="@l_prefix@/var/maradns/zoneserver.pid"
    maradns_signal () {
        [ -f $maradns_pidfile_maradns ] \
            && kill -$1 `cat $maradns_pidfile_maradns`
        local rc_maradns_maradns=$?
        [ -f $maradns_pidfile_zoneserver ] \
            && kill -$1 `cat $maradns_pidfile_zoneserver`
        local rc_maradns_zoneserver=$?
        [ $rc_maradns_maradns    -eq 0 -o \
          $rc_maradns_zoneserver -eq 0 ]
    }

%status -u @l_susr@ -o
    maradns_usable="unknown"
    maradns_active="no"
    rcService maradns enable yes && \
        maradns_signal 0 && maradns_active="yes"
    echo "maradns_enable=\"$maradns_enable\""
    echo "maradns_usable=\"$maradns_usable\""
    echo "maradns_active=\"$maradns_active\""

%start -u @l_susr@
    rcService maradns enable yes || exit 0
    rcService maradns active yes && exit 0
    touch $maradns_logfile
    chown @l_rusr@:@l_rgrp@ $maradns_logfile
    for daemon in $maradns_daemons; do
        ( nohup @l_prefix@/sbin/$daemon -f ${maradns_cfgfile} \
          </dev/null >>$maradns_logfile 2>&1 &
          eval "echo \$! >\$maradns_pidfile_$daemon"
        ) >/dev/null 2>&1
    done

%stop -u @l_susr@
    rcService maradns enable yes || exit 0
    rcService maradns active no  && exit 0
    maradns_signal TERM
    sleep 2
    rm -f $maradns_pidfile_maradns    >/dev/null 2>&1 || true
    rm -f $maradns_pidfile_zoneserver >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService maradns enable yes || exit 0
    shtool rotate -f \
        -n $maradns_log_numfiles -s $maradns_log_minsize -d \
        -z $maradns_log_complevel -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "$maradns_log_prolog" \
        -E "$maradns_log_epilog" \
        $maradns_logfile

