#!/bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/pxe
NAME=pxe
DESC=pxe
PID=/var/run/$NAME/$NAME.pid

create_pid_dir()
{
	dir=$(dirname "$PID")
	if [ ! -d "$dir" ]
	then
		mkdir "$dir"
	fi
	chown Debian-pxe "$dir"
}

wait_no_pid()
{
	while [ -e "$PID" ]
	do
		sleep 1
	done
}

test -x $DAEMON || exit 0

set -e

case "$1" in
	start)
		echo -n "Starting $DESC: "
		create_pid_dir
		start-stop-daemon --start --quiet --pidfile $PID \
			--oknodo --exec $DAEMON
		echo "$NAME."
		;;

	stop)
		echo -n "Stopping $DESC: "
		start-stop-daemon --stop --quiet --pidfile $PID \
			--oknodo --exec $DAEMON
		wait_no_pid
		echo "$NAME."
		;;

	restart|force-reload)
		echo -n "Restarting $DESC: "
		start-stop-daemon --stop --quiet --pidfile $PID \
			--oknodo --exec $DAEMON
		wait_no_pid
		start-stop-daemon --start --quiet --pidfile $PID \
			--exec $DAEMON
		echo "$NAME."
		;;

	*)
		N=/etc/init.d/$NAME
		echo "Usage: $N {start|stop|restart|force-reload}" >&2
		exit 1
		;;
esac

exit 0
