#!/bin/sh
#
# ntpd		This shell script takes care of starting and stopping
#		ntpd (NTPv4 daemon).
#
# chkconfig: 35 55 10
# description: ntpd is the NTPv4 daemon.

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

# Source networking configuration.
# Check that networking is up.
SourceIfNotEmpty /etc/sysconfig/network &&
	[ "$NETWORKING" != no ] ||
	exit

# Get config.
SourceIfNotEmpty /etc/sysconfig/ntpd

NTPDATE=/usr/sbin/ntpdate
NTPD=/usr/sbin/ntpd
[ -x "$NTPDATE" -a -x "$NTPD" -a -s /etc/ntp.conf ] || exit

LOCKFILE=/var/lock/subsys/ntpd
RETVAL=0
PROG=ntpd

start()
{
	# Adjust time to make life easy for $PROG
	if grep -qvs '^#' /etc/ntp/step-tickers; then
		action $"Synchronizing with time server:" \
			"$NTPDATE" -s -b -p 8 $NTPDATE_OPTIONS -u "`/bin/sed -e 's/#.*//' /etc/ntp/step-tickers`"
	fi

	# Start daemon.
	echo -n $"Starting $PROG: "
	daemon "$NTPD" $NTPD_OPTIONS
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch "$LOCKFILE"
}

stop()
{
	# Stop daemon.
	echo -n $"Shutting down $PROG: "
	killproc "$NTPD"
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f "$LOCKFILE"
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart|reload)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		status $PROG
		RETVAL=$?
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
