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

%config
    dnsmasq_enable="$openpkg_rc_def"
    dnsmasq_flags=""
    dnsmasq_log_prolog="true"
    dnsmasq_log_epilog="true"
    dnsmasq_log_numfiles="10"
    dnsmasq_log_minsize="1M"
    dnsmasq_log_complevel="9"

%common
    dnsmasq_pidfile="@l_prefix@/var/dnsmasq/run/dnsmasq.pid"
    dnsmasq_logfile="@l_prefix@/var/dnsmasq/log/dnsmasq.log"
    dnsmasq_signal () {
        [ -f $dnsmasq_pidfile ] && kill -$1 `cat $dnsmasq_pidfile`
    }

%status -u @l_susr@ -o
    dnsmasq_usable="unknown"
    dnsmasq_active="no"
    rcService dnsmasq enable yes && \
        dnsmasq_signal 0 && dnsmasq_active="yes"
    echo "dnsmasq_enable=\"$dnsmasq_enable\""
    echo "dnsmasq_usable=\"$dnsmasq_usable\""
    echo "dnsmasq_active=\"$dnsmasq_active\""

%start -u @l_susr@
    rcService dnsmasq enable yes || exit 0
    rcService dnsmasq active yes && exit 0
    @l_prefix@/sbin/dnsmasq ${dnsmasq_flags}

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

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

%daily -u @l_susr@
    rcService dnsmasq enable yes || exit 0
    shtool rotate -f \
        -n ${dnsmasq_log_numfiles} -s ${dnsmasq_log_minsize} -d \
        -z ${dnsmasq_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${dnsmasq_log_prolog}" \
        -E "${dnsmasq_log_epilog}; rc dnsmasq restart" \
        $dnsmasq_logfile

