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

%config
    skydns_enable="$openpkg_rc_def"
    skydns_listen="127.0.0.1:53"
    skydns_etcd_peers="127.0.0.1:2379"
    skydns_flags=""
    skydns_log_prolog="true"
    skydns_log_epilog="true"
    skydns_log_numfiles="10"
    skydns_log_minsize="1M"
    skydns_log_complevel="9"

%common
    skydns_pidfile="@l_prefix@/var/skydns/run/skydns.pid"
    skydns_logfile="@l_prefix@/var/skydns/log/skydns.log"
    skydns_signal () {
        [ -f $skydns_pidfile ] && kill -$1 `cat $skydns_pidfile`
    }

%status -u @l_susr@ -o
    skydns_usable="unknown"
    skydns_active="no"
    rcService skydns enable yes && \
        skydns_signal 0 && skydns_active="yes"
    echo "skydns_enable=\"$skydns_enable\""
    echo "skydns_usable=\"$skydns_usable\""
    echo "skydns_active=\"$skydns_active\""

%start -u @l_susr@
    rcService skydns enable yes || exit 0
    rcService skydns active yes && exit 0
    (   nohup @l_prefix@/sbin/skydns \
           -addr="$skydns_listen" \
           -machines="$skydns_etcd_peers" \
           $skydns_flags \
        </dev/null >>$skydns_logfile 2>&1 &
        echo $! >$skydns_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService skydns enable yes || exit 0
    shtool rotate -f \
        -n ${skydns_log_numfiles} -s ${skydns_log_minsize} -d \
        -z ${skydns_log_complevel} -m 664 -o @l_susr@ -g @l_sgrp@ \
        -P "${skydns_log_prolog}" \
        -E "${skydns_log_epilog}; rc skydns restart" \
        $skydns_logfile

