#!/bin/sh
#
# bird		Starts the Internet Routing Daemon.
#
# chkconfig: - 32 75
# description:  BIRD is an Internet Routing Daemon designed to support all
#              the routing technologies with a clean extensible architecture
#              allowing new routing protocols to be easily incorporated.
# processname: bird
# config: /etc/bird.conf
# pidfile: /var/run/bird.pid
### BEGIN INIT INFO
# Provides:          bird
# Required-Start:    $syslog $network
# Required-Stop:     $syslog $network
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: BIRD Internet Routing Daemon
# Description:       BIRD Internet Routing Daemon
#	with support of BGP, OSPF and other routing protocols
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

# Source networking configuration.
SourceIfNotEmpty /etc/sysconfig/network

RUN_USER=_bird
RUN_GROUP=_bird
RUNTIMEDIR=bird
PIDFILE=/var/run/bird/bird.pid
LOCKFILE=/var/lock/subsys/bird
RETVAL=0

ensure_rundir()
{
	runtimedir="$1"; shift
	mkdir -p "/run/$runtimedir"
}

ensure_no_rundir()
{
	runtimedir="$1"; shift
	rm -rf "/run/$runtimedir"
}

check()
{
	echo -n "Checking configuration of bird: "
	if bird -p > /dev/null 2>&1 ; then
		success "bird check"
		RETVAL=0
	else
		failure "bird check"
		RETVAL=1
	fi
	echo
	return $RETVAL
}

start()
{
	is_yes "$NETWORKING" || return 0
	check || return
	ensure_rundir "$RUNTIMEDIR"
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" \
		--expect-user "$RUN_USER" \
		-- bird -P "$PIDFILE"
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -- bird
	ensure_no_rundir "$RUNTIMEDIR"
	RETVAL=$?
	return $RETVAL
}

restart()
{
	bird -p &>/dev/null
	RETVAL=$?
	if [ $RETVAL -ne 0 ]; then
		echo "Service bird is not restarted: config's test failed. Try 'bird -p'."
		exit $RETVAL
	fi

	stop
	start
}

reload()
{
	bird -p &>/dev/null
	RETVAL=$?
	if [ $RETVAL -ne 0 ]; then
		echo "Service bird is not reloaded: config's test failed. Try 'bird -p'."
		exit $RETVAL
	fi

	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -HUP -- bird
	RETVAL=$?
	return $RETVAL
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	status)
		status --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -- bird
		RETVAL=$?
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	check)
		check
		;;
	*)
		msg_usage "${0##*/} {start|stop|check|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
