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

### BEGIN INIT INFO
# Provides:          ntp
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start NTP daemon
# Description:       Enable NTP daemon.
### END INIT INFO

WITHOUT_RC_COMPAT=1

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

# Source networking configuration.
SourceIfNotEmpty /etc/sysconfig/network

# Source service configuration.
SourceIfNotEmpty /etc/sysconfig/ntpd

if [ ! -n "$NTPD_USER" ] ; then
	NTPD_USER="ntpd"
	NTPD_GROUP="ntpd"
	NTPD_OPTIONS="$NTPD_OPTIONS --user=$NTPD_USER:$NTPD_GROUP"
fi

NTPD=/usr/sbin/ntpd
PIDFILE=/var/run/ntpd.pid
LOCKFILE=/var/lock/subsys/ntpd
RETVAL=0

start()
{
	is_yes "$NETWORKING" || return 0

	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user "$NTPD_USER" -- ntpd -p "$PIDFILE" $NTPD_OPTIONS
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user "$NTPD_USER" -- ntpd
	RETVAL=$?
	return $RETVAL
}

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 --expect-user "$NTPD_USER" -- ntpd
		NTPTRACE=/usr/sbin/ntptrace
		if [ -f "$NTPTRACE" ]; then
			$NTPTRACE localhost
		fi
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
