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

%config
    quagga_enable="$openpkg_rc_def"
    quagga_protocols="rip ospf bgp"
    quagga_flags=""
    quagga_bind="127.0.0.1"
    quagga_port="2601"
    quagga_rip_flags=""
    quagga_rip_bind="${quagga_bind}"
    quagga_rip_port="2602"
    quagga_ospf_flags=""
    quagga_ospf_bind="${quagga_bind}"
    quagga_ospf_port="2604"
    quagga_bgp_flags=""
    quagga_bgp_bind="${quagga_bind}"
    quagga_bgp_port="2605"
    quagga_log_prolog="true"
    quagga_log_epilog="true"
    quagga_log_numfiles="10"
    quagga_log_minsize="1M"
    quagga_log_complevel="9"

%status -u @l_susr@ -o
    quagga_usable="unknown"
    quagga_active="no"
    if [ -f @l_prefix@/var/quagga/zebra.pid ]; then
        kill -0 `cat @l_prefix@/var/quagga/zebra.pid` >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            quagga_active="yes"
        fi
    fi
    echo "quagga_enable=\"$quagga_enable\""
    echo "quagga_usable=\"$quagga_usable\""
    echo "quagga_active=\"$quagga_active\""

%start -u @l_susr@
    rcService quagga enable yes || exit 0
    rcService quagga active yes && exit 0
    cd @l_prefix@/etc/quagga
    for i in zebra $quagga_protocols; do
        if [ ".$i" = .zebra ]; then
            midfix=""
            daemon="${i}";
        else
            midfix="${i}_";
            daemon="${i}d";
        fi
        if [ -x @l_prefix@/sbin/${daemon} ]; then
            flags=`eval echo \\\$quagga_${midfix}flags`;
            extra=`eval echo \\\$quagga_${midfix}bind`;
            echo $flags | grep -- -A >/dev/null
            if [ $? -ne 0 -a ".$extra" != . ]; then
                flags="$flags -A $extra"
            fi
            extra=`eval echo \\\$quagga_${midfix}port`;
            echo $flags | grep -- -P >/dev/null
            if [ $? -ne 0 -a ".$extra" != . ]; then
                flags="$flags -P $extra"
            fi
            @l_prefix@/sbin/${daemon} -d ${flags}
        fi
    done
    @l_prefix@/bin/vtysh -b

%stop -u @l_susr@
    rcService quagga enable yes || exit 0
    rcService quagga active no  && exit 0
    if [ -f @l_prefix@/var/quagga/zebra.pid ]; then
        kill -TERM `cat @l_prefix@/var/quagga/zebra.pid` >/dev/null 2>&1 || true
    fi
    for protocol in $quagga_protocols; do
        if [ -f @l_prefix@/var/quagga/${protocol}d.pid ]; then
            kill -TERM `cat @l_prefix@/var/quagga/${protocol}d.pid` >/dev/null 2>&1 || true
        fi
    done
    sleep 1
    rm -f @l_prefix@/var/quagga/*.pid >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService quagga enable yes || exit 0
    shtool rotate -f \
        -n ${quagga_log_numfiles} -s ${quagga_log_minsize} -d \
        -z ${quagga_log_complevel} -m 640 -o @l_susr@ -g @l_mgrp@ \
        -P "${quagga_log_prolog}" \
        -E "${quagga_log_epilog}; rc quagga restart" \
        @l_prefix@/var/quagga/zebra.log

