#!/bin/sh
#
# zebra		Starts the Dynamic Route Daemon
#
# chkconfig:	345 13 85
#
# description:	Dynamic Route Daemon for IPv4 and IPv6 routers
#
# processname:	zebra
# config:	/etc/zebra/zebra.conf


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

# Get network config
. /etc/sysconfig/network

# Get service config
[ -f /etc/sysconfig/zebra ] && . /etc/sysconfig/zebra

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down zebra
		exit 1
	fi
else
	exit 0
fi

RETVAL=0
# See how we were called.
case "$1" in
  start)
	# Check if the services are already running?
	if [ ! -f /var/lock/subsys/zebra ]; then
                FLAGS="--daemon"
                [ -n "$VTY_ADDR" ] && FLAGS="$FLAGS --vty_addr $VTY_ADDR"
                [ -n "$VTY_PORT" ] && FLAGS="$FLAGS --vty_port $VTY_PORT"
		msg_starting zebra
		daemon zebra $FLAGS
		RETVAL=$?
		if [ $RETVAL -eq 0 ] ; then
			touch /var/lock/subsys/zebra
			if [ -f /etc/zebra/Quagga.conf ] ; then
				run_cmd "Loading zebra configuration" vtysh -b
			fi
		fi
	else
		msg_already_running "zebra"
	fi
	;;
  stop)
	if [ -f /var/lock/subsys/zebra ]; then
		# Stop daemons.
 		msg_stopping "zebra"
		killproc zebra
		rm -f /var/lock/subsys/zebra
	else
		msg_not_running zebra
	fi
	;;
  status)
	status zebra
	exit $?
	;;
  restart|force-reload)
	$0 stop
	$0 start
	R=$?
	if [ $R -eq 0 ]; then
		if [ -f /var/lock/subsys/bgpd ]; then
			/etc/rc.d/init.d/bgpd "$1"
		fi
		if [ -f /var/lock/subsys/isisd ]; then
			/etc/rc.d/init.d/isisd "$1"
		fi
		if [ -f /var/lock/subsys/ospfd ]; then
			/etc/rc.d/init.d/ospfd "$1"
		fi
		if [ -f /var/lock/subsys/ospf6d ]; then
			/etc/rc.d/init.d/ospf6d "$1"
		fi
		if [ -f /var/lock/subsys/ripd ]; then
			/etc/rc.d/init.d/ripd "$1"
		fi
		if [ -f /var/lock/subsys/ripngd ]; then
			/etc/rc.d/init.d/ripngd "$1"
		fi
	fi
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
