#!/bin/bash
#
# openntpd	Time synchronisation daemon
#
# chkconfig: 2345 90 60
# description: openntpd is a time synchronisation daemon
# processname: ntpd
# config: /etc/ntpd.conf
#
### BEGIN INIT INFO
# Provides:          ntp ntpd openntpd xntpd
# Required-Start:    $remote_fs $syslog $named
# Required-Stop:     $remote_fs $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
#                    synchronised.
### END INIT INFO

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

# Source settings.
DAEMON_OPTS=
REFERENCE=
test -f /etc/default/openntpd && . /etc/default/openntpd

RETVAL=0

# See how we were called.

prog=ntpd

start() {
	echo -n "Starting $prog: "
	daemon ntpd $DAEMON_OPTS
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/openntpd || :
	return $RETVAL
}

stop() {
	echo -n "Stopping $prog: "
	killproc ntpd 2>/dev/null
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/openntpd || :
	return $RETVAL
}

rhstatus() {
	status ntpd
}

reload() {
	echo -n "Reloading ntpd daemon configuration: "
	killproc ntpd -HUP
	retval=$?
	echo
	return $RETVAL
}

case $1 in
(start)
	# log to both stdio and syslog
	refsync=$(if test x"$REFERENCE" = x"NO"; then
		echo "Not synchronising first."
	else
		REFERENCE=${REFERENCE:-192.53.103.103}
		echo "Synchronising with $REFERENCE first."
		(rdate -nv "$REFERENCE" || :)
	fi 2>&1)
	# no, can’t pass this as argument
	printf '%s\n' "$refsync" | logger -t "$prog"
	printf '%s\n' "$refsync"
	start
	;;
(stop)
	stop
	;;
(restart)
	stop
	start
	;;
(reload)
	reload
	;;
(status)
	rhstatus
	;;
(condrestart)
	[ -f /var/lock/subsys/openntpd ] && restart || :
	;;
(*)
	echo "Usage: $0 {start|stop|status|reload|restart|condrestart}"
	exit 1
esac

exit $?
