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

%config
    discodns_enable="$openpkg_rc_def"
    discodns_listen_addr="127.0.0.1"
    discodns_listen_port="53"
    discodns_etcd_peers="127.0.0.1:4001"
    discodns_log_prolog="true"
    discodns_log_epilog="true"
    discodns_log_numfiles="10"
    discodns_log_minsize="1M"
    discodns_log_complevel="9"

%common
    discodns_pidfile="@l_prefix@/var/discodns/run/discodns.pid"
    discodns_logfile="@l_prefix@/var/discodns/log/discodns.log"
    discodns_signal () {
        [ -f $discodns_pidfile ] && kill -$1 `cat $discodns_pidfile`
    }

%status -u @l_susr@ -o
    discodns_usable="unknown"
    discodns_active="no"
    rcService discodns enable yes && \
        discodns_signal 0 && discodns_active="yes"
    echo "discodns_enable=\"$discodns_enable\""
    echo "discodns_usable=\"$discodns_usable\""
    echo "discodns_active=\"$discodns_active\""

%start -u @l_susr@
    rcService discodns enable yes || exit 0
    rcService discodns active yes && exit 0
    (   nohup @l_prefix@/sbin/discodns \
           --listen="$discodns_listen_addr" \
           --port="$discodns_listen_port" \
           --etcd="$discodns_etcd_peers" \
        </dev/null >>$discodns_logfile 2>&1 &
        echo $! >$discodns_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService discodns enable yes || exit 0
    shtool rotate -f \
        -n ${discodns_log_numfiles} -s ${discodns_log_minsize} -d \
        -z ${discodns_log_complevel} -m 664 -o @l_susr@ -g @l_sgrp@ \
        -P "${discodns_log_prolog}" \
        -E "${discodns_log_epilog}; rc discodns restart" \
        $discodns_logfile

