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

%config
    avahi_enable="$openpkg_rc_def"
    avahi_daemons="avahi-daemon avahi-dnsconfd"
    avahi_log_prolog="true"
    avahi_log_epilog="true"
    avahi_log_numfiles="10"
    avahi_log_minsize="1M"
    avahi_log_complevel="9"

%common
    avahi_etcdir="@l_prefix@/etc/avahi"
    avahi_vardir="@l_prefix@/var/avahi"
    avahi_check () {
        local retcode=0
        local daemons=
        for daemon in $avahi_daemons; do
            @l_prefix@/sbin/$daemon -c
            retcode=$(($retcode+$?))
        done
        return $retcode
    }

%status -u @l_susr@ -o
    avahi_usable="unknown"
    avahi_active="no"
    rcService avahi enable yes && \
        avahi_check && avahi_active="yes"
    echo "avahi_enable=\"$avahi_enable\""
    echo "avahi_usable=\"$avahi_usable\""
    echo "avahi_active=\"$avahi_active\""

%start -u @l_rusr@
    rcService avahi enable yes || exit 0
    rcService avahi active yes && exit 0
    for daemon in $avahi_daemons; do
        @l_prefix@/sbin/$daemon -D
    done

%stop -u @l_susr@
    rcService avahi enable yes || exit 0
    rcService avahi active no  && exit 0
    for daemon in $avahi_daemons; do
        @l_prefix@/sbin/$daemon -k
    done

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

%reload -u @l_susr@
    rcService avahi enable yes || exit 0
    rcService avahi active no  && exit 0
    for daemon in $avahi_daemons; do
        @l_prefix@/sbin/$daemon -r
    done

%daily -u @l_susr@
    rcService avahi enable yes || exit 0
    rcTmp -i
    hintfile=`rcTmp -f -n hint`
    for daemon in $avahi_daemons; do
        if [ -f $avahi_vardir/log/$daemon.log ]; then
            shtool rotate -f \
                -n $avahi_log_numfiles -s $avahi_log_minsize -d \
                -z $avahi_log_complevel -m 664 -o @l_rusr@ -g @l_rgrp@ \
                -P "$avahi_log_prolog" \
                -E "$avahi_log_epilog; echo 1 >$hintfile" \
                $avahi_vardir/log/$daemon.log
        fi
    done
    if [ -s $hintfile ]; then
        rc avahi restart
    fi
    rcTmp -k

