#!/bin/sh
#
# $Id: openlldp-lldp.init,v 1.5 2008-01-23 08:46:44 glen Exp $
#
# openlldp	OpenLLDP - Open Source LLDP implimentation
#
# chkconfig:	345 55 55
# description:	The OpenLLDP project aims to provide a comprehensive implementation 
#    		of the IEEE standard 802.1AB Link Layer Discovery Protocol.
# processname:	lldpd
#
# Ilja Bobkevic <ilja.bobkevic@delfi.lt>

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

# Get network config
. /etc/sysconfig/network

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

# 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 "OpenLLDP Daemon"
		exit 1
	fi
else
	exit 0
fi

start() {
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/lldpd ]; then
		msg_starting "OpenLLDP Daemon"
		# Default debug switch "n". Default log file /var/log/lldpd.log
		# For now use start-stop-daemon. Need to patch code for daemonizing
		start-stop-daemon \
			--start \
			--background \
			--pidfile /var/run/lldpd.pid \
			--make-pidfile \
			--exec /usr/sbin/lldpd -- \
			-d ${LLDP_DEBUG:-n} \
			>> ${LLDP_LOG_FILE:-/var/log/lldpd.log}
		RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
		   	touch /var/lock/subsys/lldpd
			ok
		else
			fail
		fi
	else
		msg_already_running "OpenLLDP Daemon"
	fi
}

stop() {
	# Stop daemons.
	if [ -f /var/lock/subsys/lldpd ]; then
		msg_stopping "OpenLLDP Daemon"
		start-stop-daemon --stop --pidfile /var/run/lldpd.pid --name lldpd
		ok
		rm -f /var/lock/subsys/lldpd /var/run/lldpd.pid
	else
		msg_not_running "OpenLLDP Daemon"
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
	;;
  status)
	status lldpd
	RETVAL=$?
	;;
  *)
	msg_usage "$0 {start|stop|restart|status}"
	exit 3
	;;
esac

exit $RETVAL
