#!/bin/sh

### BEGIN INIT INFO
# Provides:		openntpd
# Required-Start:	$remote_fs $network $syslog
# Required-Stop:	$remote_fs $network $syslog
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	Start openntpd at boot time
# Description:		NTP, the Network Time Protocol, is used to keep the
#			computer clocks synchronized.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/ntpd
NAME=ntpd
USER=ntpd
DESC=openntpd

test -e /usr/sbin/openntpd || exit 0

test -f /lib/lsb/init-functions || exit 1
. /lib/lsb/init-functions


# Include openntpd defaults if available
if [ -f /etc/default/openntpd ]; then
	. /etc/default/openntpd
fi

DAEMON_OPTS="${DAEMON_OPTS:-} -p /var/run/openntpd.pid"

set -e

case "$1" in
	start)
		echo -n "Starting $DESC: "
		if status_of_proc "$DAEMON" $DESC > /dev/null; then
			log_begin_msg "Already running."
		else
			start-stop-daemon --start --quiet --exec $DAEMON -- $DAEMON_OPTS
			echo "$NAME."
		fi
		;;

	stop)
		echo -n "Stopping $DESC: "
		start-stop-daemon --stop --oknodo --quiet --user root --exec $DAEMON
		echo "$NAME."
		;;

	restart|force-reload)
		echo -n "Restarting $DESC: "
		start-stop-daemon --stop --oknodo --quiet --user root --exec $DAEMON
		sleep 1
		start-stop-daemon --start --quiet --exec $DAEMON -- $DAEMON_OPTS
		echo "$NAME."
		;;

	status)
		start-stop-daemon --test --stop --quiet --user root --exec $DAEMON && \
			log_success_msg "OpenNTPd daemon is running." || \
			( log_failure_msg "OpenNTPd daemon is NOT running" && exit 1 )
		;;		

	check)
		$DAEMON $DAEMON_OPTS "-n"
		;;

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

exit 0
