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

%config
    djbdns_enable="$openpkg_rc_def"
    djbdns_runsvc="@l_prefix@/libexec/djbdns/bin/djbdnsrunsvc"
    djbdns_bin="@l_prefix@/libexec/djbdns/bin"
    djbdns_var="@l_prefix@/var/djbdns"

%common
    djbdns_signal () {
        local s
        local rc=0
        for s in dnscache tinydns; do
            if [ -f $djbdns_var/$s.pid ]; then
                kill -$1 `cat $djbdns_var/$s.pid` || rc=1
            fi
        done
        [ $rc -eq 0 ]
    }

%status -u @l_susr@ -o
    djbdns_usable="unknown"
    djbdns_active="no"
    rcService djbdns enable yes && \
        rcVarIsYes djbdns_daemon && \
        [ -d $djbdns_var/dnscache ] || \
        [ -d $djbdns_var/tinydns ] && \
        djbdns_usable="yes"
    rcService djbdns enable yes && \
        rcVarIsYes djbdns_daemon && \
        djbdns_signal 0 && djbdns_active="yes"
    echo "djbdns_enable=\"$djbdns_enable\""
    echo "djbdns_usable=\"$djbdns_usable\""
    echo "djbdns_active=\"$djbdns_active\""

%start -u @l_susr@
    rcService djbdns enable yes || exit 0
    rcService djbdns usable yes || exit 0
    rcService djbdns active yes && exit 0
    [ -d $djbdns_var/dnscache ] && $djbdns_runsvc \
        $djbdns_bin/dnscache $djbdns_var/dnscache \
        >$djbdns_var/dnscache.pid
    [ -d $djbdns_var/tinydns ] && $djbdns_runsvc \
        $djbdns_bin/tinydns $djbdns_var/tinydns \
        >$djbdns_var/tinydns.pid

%stop -u @l_susr@
    rcService djbdns enable yes || exit 0
    rcService djbdns active no && exit 0
    djbdns_signal TERM
    rm -f $djbdns_var/dnscache.pid 2>/dev/null || true
    rm -f $djbdns_var/tinydns.pid 2>/dev/null || true

