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

%config
    coredns_enable="$openpkg_rc_def"
    coredns_flags=""
    coredns_log_prolog="true"
    coredns_log_epilog="true"
    coredns_log_numfiles="10"
    coredns_log_minsize="1M"
    coredns_log_complevel="9"

%common
    coredns_cfgfile="@l_prefix@/etc/coredns/Corefile"
    coredns_pidfile="@l_prefix@/var/coredns/run/coredns.pid"
    coredns_logfile="@l_prefix@/var/coredns/log/coredns.log"
    coredns_signal () {
        [ -f $coredns_pidfile ] && kill -$1 `cat $coredns_pidfile`
    }

%status -u @l_rusr@ -o
    coredns_usable="unknown"
    coredns_active="no"
    rcService coredns enable yes && \
        coredns_signal 0 && coredns_active="yes"
    echo "coredns_enable=\"$coredns_enable\""
    echo "coredns_usable=\"$coredns_usable\""
    echo "coredns_active=\"$coredns_active\""

%start -u @l_rusr@
    rcService coredns enable yes || exit 0
    rcService coredns active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/coredns \
           -conf="$coredns_cfgfile" \
           -log="$coredns_logfile" \
           -pidfile="$coredns_pidfile" \
           $coredns_flags \
        </dev/null >>$coredns_logfile 2>&1 &
        echo $! >$coredns_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService coredns enable yes || exit 0
    rcService coredns active no  && exit 0
    coredns_signal TERM
    sleep 2
    rm -f $coredns_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_rusr@
    rcService coredns enable yes || exit 0
    shtool rotate -f \
        -n ${coredns_log_numfiles} -s ${coredns_log_minsize} -d \
        -z ${coredns_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${coredns_log_prolog}" \
        -E "${coredns_log_epilog}; rc coredns restart" \
        $coredns_logfile

