#! /bin/sh
# Copyright (c) 1995-2003 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Dirk Mueller
# based on script by Michael Andres
#
# /etc/init.d/ntpd
#   and its symbolic link
# /usr/sbin/rcntpd
#
### BEGIN INIT INFO
# Provides:       ntp ntpd xntpd 
# Required-Start: $remote_fs $syslog $named
# Required-Stop:  $remote_fs $syslog
# Default-Start:  2 3 5
# Default-Stop:   0 1 6
# Description:    Start network time protocol daemon (NTPD).
### END INIT INFO

# First reset status of this service
. /etc/rc.status
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running

# set default options
NTP_CONF="/etc/ntpd.conf"
if [ ! -f ${NTP_CONF} ]; then
	echo -n "Time server configuration file, ${NTP_CONF} does not exist."
	# Tell the user this has skipped
	rc_status -s
	exit 6
fi

NTPD_BIN="/usr/sbin/ntpd"
if [ ! -x ${NTPD_BIN} ]; then
        echo -n "Time server, ${NTPD_BIN} not installed!"
        rc_status -s
        exit 5
fi

[ -f /etc/sysconfig/openntpd ] \
	&& . /etc/sysconfig/openntpd

function ntpd_is_running() {
    $0 status >/dev/null
}

case "$1" in
    start)
	echo -n "Starting network time protocol daemon (NTPD)"
	startproc $NTPD_BIN $NTPD_OPTS
	rc_status -v
	;;
    stop)
	echo -n "Shutting down network time protocol daemon (NTPD)"
	killproc -TERM $NTPD_BIN
	rc_status -v
	;;
    try-restart)
        $0 status
        if test $? = 0; then
                $0 restart
        else
                rc_reset        # Not running is not a failure.
        fi
        # Remember status and be quiet
        rc_status
	;;
    restart)
	$0 stop
	$0 start
	rc_status
        ;;
    status)
	echo -n "Checking for network time protocol daemon (NTPD): "
	checkproc -p `pidof $NTPD_BIN |awk '{print $1}'` $NTPD_BIN
	rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|status|restart|try-restart}"
	exit 1
	;;
esac
rc_exit
