#!/bin/sh -e

PATH=/sbin:/bin:/usr/bin
FLAGS="defaults 23"

test -f /usr/sbin/ntpd || exit 0
. /lib/lsb/init-functions

RUNASUSER=ntp
UGID=$(getent passwd $RUNASUSER | cut -f 3,4 -d:) || true

if [ -z "$UGID" ]; then
	echo "User $USER does not exist" >&2
	exit 1
fi

case "$1" in
	start)
		log_begin_msg "Starting NTP server..."
  		start-stop-daemon --start --quiet --pidfile /var/run/ntpd.pid --exec /usr/sbin/ntpd -- -p /var/run/ntpd.pid -u $UGID
		log_end_msg $?
  		;;
	stop)
		log_begin_msg "Stopping NTP server..."
  		start-stop-daemon --stop --quiet --pidfile /var/run/ntpd.pid
		log_end_msg $?
  		;;
	restart|force-reload)
		$0 stop
		sleep 1
		$0 start
  		;;
	*)
  		log_success_msg "Usage: /etc/init.d/ntp-server {start|stop|restart|force-reload}"
  		exit 1
		;;
esac

exit 0
