#!/bin/bash
#
# Startup script for the zebra OSPF daemon
#
# chkconfig: 345 99 01 
# description: Handle OSPF protocol
# processname: ospfd
#
# Copyright (C) 2004 Networks Associates Technology Inc. All rights reserved.
#
#

# Source function library.
. /etc/init.d/functions

if [ -f $NETAWSS/.profile.vars ]; then 
 . $NETAWSS/.profile.vars 
else 
 . /var/NAIENV/.profile.vars 
fi 
PATH=$wsPATH:$PATH
jail=/var/zebra.jail
lbin=/usr/sbin
letc=/etc/zebra

# should be able to use daemon --check to ensure things don't get started
# twice, but it's broken
start_zebra()
{
    killall -q -0 $jail$lbin/zebra ||
	daemon /usr/sbin/chroot $jail/ $lbin/zebra -d -P 65536 -k -r
}
start_ospf()
{
    killall -q -0 $jail$lbin/ospfd ||
	daemon /usr/sbin/chroot $jail/ $lbin/ospfd -d -P 65536
}
start_rip()
{
    killall -q -0 $jail$lbin/ripd ||
	daemon /usr/sbin/chroot $jail/ $lbin/ripd -d -P 0 -r
}
start_bgp()
{
    killall -q -0 $jail$lbin/bgpd ||
	daemon /usr/sbin/chroot $jail/ $lbin/bgpd -d -P 0
}


start()
{
    test -d ${jail} 
    if [ $? = 0 ]
    then
    	start_zebra
	for PROTOCOL in rip ospf bgp
        do
            if [ -f $jail$letc/${PROTOCOL}d.conf ]
            then
		start_${PROTOCOL}
            fi
        done
        touch /var/lock/subsys/zebra_jail
    fi
    echo
}

stop()
{
    /usr/bin/killall -q ripd 
    /usr/bin/killall -q ospfd 
    /usr/bin/killall -q bgpd
    /usr/bin/killall -q zebra
 
    rm -f /var/lock/subsys/zebra_jail
}


# See how we were called.
case "$1" in
  start)
	echo -n "Starting routing daemon: "
        start
        echo
	;;
  stop)
        stop
	;;
  restart)
	$0 stop
	sleep 2
	$0 start
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit 0
