#! /bin/sh
# Copyright (c) 1995-2000 SuSE GmbH Nuernberg, Germany.
#
# Author: Michael Andres <feedback@suse.de>
#
### BEGIN INIT INFO
# Provides:       xntpd ntpd
# 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

# Source SuSE config
test -f /etc/sysconfig/xntp && . /etc/sysconfig/xntp

# Determine the base and follow a runlevel link name.
base=${0##*/}
link=${base#*[SK][0-9][0-9]}

NTPD_BIN=/usr/sbin/ntpd
test -x $NTPD_BIN || exit 5

# 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

NTP_CONF=/etc/ntp.conf
NTPDATE_BIN=/usr/sbin/ntpdate

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

function initial_ntpdate () {
    case "$XNTPD_INITIAL_NTPDATE" in
    auto|Auto|AUTO)
    	MAX_AUTO=-1;
    	;;
    auto-*|Auto-*|AUTO-*)
    	MAX_AUTO=${XNTPD_INITIAL_NTPDATE#*-};
    	;;
    *)
    	echo $XNTPD_INITIAL_NTPDATE
    	return 0
    	;;
    esac
    # AUTO: Try to get server from config file
    if [ -r $NTP_CONF ] ; then
        cat $NTP_CONF | awk -v MAX_AUTO=$MAX_AUTO '
	    /^server[[:space:]]+127.127/ {
	    	next
	    }
	    /^(server|peer)[[:space:]]/ {
	    	if ( MAX_AUTO ) {
		    printf " %s", $2
		    if ( --MAX_AUTO == 0 )
		    	exit 0
		}
	    }
    	'
    fi

}

case "$1" in
    start)
        NTPDATE_FROM=$(initial_ntpdate)
    	if [ -n "$NTPDATE_FROM" -a -x $NTPDATE_BIN ]; then
    	    ntpd_is_running || {
    		echo -n "Try to get initial date and time via NTP from $NTPDATE_FROM"
    		# -b: Set time on runlevel change, otherwise let 
    		#     ntpdate decide whether to slew or step.
    		test $link = $base \
    		  && FORCE_STEP="" \
    		  || FORCE_STEP="-b"
    		# -u: Use an unprivileged port for outgoing packets,
    		#     may be we have to synchronise with hosts beyond 
    		#     a firewall.
    		$NTPDATE_BIN -su $FORCE_STEP $NTPDATE_FROM \
    		  && echo $rc_done \
    		  || echo $rc_failed
    		# error here is reported but not propagated.
	    }
    	fi
    	
	echo -n "Starting network time protocol daemon (NTPD)"
	startproc $NTPD_BIN $XNTPD_OPTIONS
	rc_status -v
	;;
    stop)
	echo -n "Shutting network time protocol daemon (NTPD)"
	killproc -TERM $NTPD_BIN
	rc_status -v
	;;
    try-restart)
	ntpd_is_running && $0 restart
	rc_status
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    force-reload)
	# Does not support signalling to reload
	$0 stop  &&  $0 start
	rc_status
	;;
    reload)
	echo -n "Reload network time protocol daemon (NTPD)"
	# Does not support signalling to reload
	rc_failed 3
	rc_status -v
	;;
    status)
	echo -n "Checking for network time protocol daemon (NTPD): "
	checkproc $NTPD_BIN
	rc_status -v
	;;
    probe)
	# test /etc/ntp.conf -nt /var/run/ntp.pid && echo restart
	rc_failed 3
	;;
    ntptimeset)
        NTPDATE_FROM=$(initial_ntpdate)
    	if [ -n "$NTPDATE_FROM" -a -x $NTPDATE_BIN ]; then
    	    if ntpd_is_running; then
    	       echo -n "Can't set time while ntpd is running"
	       rc_failed 2
	       rc_status -v
    	    else
    		echo -n "Try to get initial date and time via NTP from $NTPDATE_FROM"
    		# -b: Set time on runlevel change, otherwise let 
    		#     ntpdate decide whether to slew or step.
    		test $link = $base \
    		  && FORCE_STEP="" \
    		  || FORCE_STEP="-b"
    		# -u: Use an unprivileged port for outgoing packets,
    		#     may be we have to synchronise with hosts beyond 
    		#     a firewall.
    		$NTPDATE_BIN -su $FORCE_STEP $NTPDATE_FROM \
    		  && echo $rc_done \
    		  || echo $rc_failed
    		# error here is reported but not propagated.
    	    fi
    	fi
    	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe|ntptimeset}"
	exit 1
	;;
esac
rc_exit
