#!/bin/sh

if [ `/usr/bin/id -u` -ne 0 ]
then
	echo -e "\nNeed to run as root or using sudo.\n"
	exit 1
fi

NAME="lldpd"
DAEMON="/usr/local/sbin/$NAME"
DAEMON_ARGS=""

RUNNING=$(ps aux | grep -v "grep" | grep "$DAEMON")

start()
{
	[ -n "$RUNNING" ] && echo -e "\n$NAME is already running.\n" && exit 2
	start-stop-daemon -S -x "$DAEMON" -- "$DAEMON_ARGS"
}

stop()
{
	start-stop-daemon -q -K -x "$DAEMON" 2>/dev/null
	for DELAY in $(seq 1 30)
	do
	RUNNING=$(ps aux | grep -v "grep" | grep "$DAEMON")
	[ -z "$RUNNING" ] && break
	done
	[ -n "$RUNNING" ] && echo -e "\n$NAME was not stopped.\n" && exit 3
	rm -rf /var/run/"$NAME"*
}



status()
{
	[ -n "$RUNNING" ] && echo -e "\n$NAME is running.\n" && exit 0
	echo -e "\n$NAME is not running.\n"
	exit 1
}

case $1 in
	start) start
		;;
	stop) stop
		;;
	status) status
		;;
	restart) stop; start
		;;
	*) echo -e "\n$0 [start|stop|restart|status]\n"
		;;
esac
