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

%config
    zebra_enable="$openpkg_rc_def"
    zebra_protocols="rip ospf bgp"
    zebra_flags=""
    zebra_bind="127.0.0.1"
    zebra_port="2601"
    zebra_rip_flags=""
    zebra_rip_bind="${zebra_bind}"
    zebra_rip_port="2602"
    zebra_ospf_flags=""
    zebra_ospf_bind="${zebra_bind}"
    zebra_ospf_port="2604"
    zebra_bgp_flags=""
    zebra_bgp_bind="${zebra_bind}"
    zebra_bgp_port="2605"
    zebra_log_prolog="true"
    zebra_log_epilog="true"
    zebra_log_numfiles="10"
    zebra_log_minsize="1M"
    zebra_log_complevel="9"

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

%start -u @l_susr@
    rcService zebra enable yes || exit 0
    rcService zebra active yes && exit 0
    cd @l_prefix@/etc/zebra
    for i in zebra $zebra_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 \\\$zebra_${midfix}flags`;
            extra=`eval echo \\\$zebra_${midfix}bind`;
            echo $flags | grep -- -A >/dev/null
            if [ $? -ne 0 -a ".$extra" != . ]; then
                flags="$flags -A $extra"
            fi
            extra=`eval echo \\\$zebra_${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 zebra enable yes || exit 0
    rcService zebra active no  && exit 0
    if [ -f @l_prefix@/var/zebra/zebra.pid ]; then
        kill -TERM `cat @l_prefix@/var/zebra/zebra.pid` >/dev/null 2>&1 || true
    fi
    for protocol in $zebra_protocols; do
        if [ -f @l_prefix@/var/zebra/${protocol}d.pid ]; then
            kill -TERM `cat @l_prefix@/var/zebra/${protocol}d.pid` >/dev/null 2>&1 || true
        fi
    done
    sleep 1
    rm -f @l_prefix@/var/zebra/*.pid >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService zebra enable yes || exit 0
    shtool rotate -f \
        -n ${zebra_log_numfiles} -s ${zebra_log_minsize} -d \
        -z ${zebra_log_complevel} -m 640 -o @l_susr@ -g @l_mgrp@ \
        -P "${zebra_log_prolog}" \
        -E "${zebra_log_epilog}; rc zebra restart" \
        @l_prefix@/var/zebra/zebra.log

