#!/bin/bash
#
### BEGIN INIT INFO
# Provides:exabgp
# Required-Start:
# Required-Stop:
# Default-Start:3 5
# Default-Stop:0 1 2 6
# Short-Description:Start exabgp
# Description:The BGP swiss army knife of networking 
### END INIT INFO

config="/etc/exabgp/exabgp.conf"
exabgp="/usr/sbin/exabgp"
prog="exabgp"

. /etc/rc.status

[ -f /etc/sysconfig/exabgp ] && . /etc/sysconfig/exabgp

rc_reset

case "$1" in
	start)
		if [ -n "${ENVDIR}" ] ; then
			PIDDIR=`grep ^pid /usr/share/exabgp/etc/exabgp.env | awk '{print "dirname " $NF}'|sh`
			USER=`grep ^user /usr/share/exabgp/etc/exabgp.env | awk '{print $NF}'|sed -e s@[\'\"]@@g`
			mkdir -p ${PIDDIR}
			chown -R ${USER} ${PIDDIR}
		fi
		echo -n "Starting $prog: "
		$(type -p startproc 2>/dev/null) $exabgp $config ${OPTIONS}
		rc_status -v
	;;
	stop)
		if [ -n "${ENVDIR}" ] ; then
			PIDFILE=`grep ^pid /usr/share/exabgp/etc/exabgp.env | awk '{print $NF}'|sed -e s@[\'\"]@@g`
			if [ -n "${PIDFILE}" ] ; then
				echo -n "Shutting down $prog: "
				kill `cat ${PIDFILE}`
				rc_status -v
			else
				echo "Can't find pidfile from exabgp.env"
			fi
		else
			echo "ENVDIR not defined int /etc/sysconfig/exabgp"
		fi
	;;
	reload)
		if [ -n "${ENVDIR}" ] ; then
			PIDFILE=`grep ^pid /usr/share/exabgp/etc/exabgp.env | awk '{print $NF}'|sed -e s@[\'\"]@@g`
			if [ -n "${PIDFILE}" ] ; then
				/bin/kill -USR1 `cat ${PIDFILE}`
				rc_status -v
			else
				echo "Can't find pidfile from exabgp.env"
			fi
		else
			echo "ENVDIR not defined int /etc/sysconfig/exabgp"
		fi
	;;
	restart)
		$0 stop
		$0 start
		rc_status
	;;
	status)
		echo -n "$prog status:"
		rc_status -v
	;;
	*)
		echo "Usage: $0 {start|stop|restart|status}"
		exit 1
	;;
esac

rc_exit
