#!/sbin/sh
#
# xntpd $Revision: 1.4.212.2 $ $Date: 95/12/26 10:09:43 $
#
#           Copyright (C) 1993,1994 Hewlett-Packard Company
#
# NOTE:    This script is not configurable!  Any changes made to this
#          script will be overwritten when you upgrade to the next
#          release of HP-UX.
#
#

PATH=/sbin:/usr/sbin:/usr/bin
export PATH

rval=0
set_return() {
        x=$?
        if [ $x -ne 0 ]; then
                echo "EXIT CODE: $x"
                rval=1  # always 1 so that 2 can be used for other reasons
        fi
}

case $1 in
start_msg)
	echo "Start time synchronization"
	;;

stop_msg)
	echo "Stopping time synchronization"
	;;

'start')
	if [ -f /etc/rc.config ] ; then
		. /etc/rc.config
	else
		echo "ERROR: /etc/rc.config defaults file MISSING"
	fi

	if [ "$XNTPD" -eq 1 -a -x /usr/sbin/xntpd ]; then

		if [ -z "${NTPDATE_SERVER}" ]; then
			NTPDATE_SERVER=`/usr/bin/dcnodes -br`
		fi

		/usr/sbin/ntpdate ${NTPDATE_SERVER}

		/usr/sbin/xntpd $XNTPD_ARGS && echo "xntpd  \c"
		set_return
	else
		rval=2
	fi
	;;

'stop')
	if [ -f /etc/rc.config ] ; then
		. /etc/rc.config
	else
		echo "ERROR: /etc/rc.config defaults file MISSING"
	fi

	#
	# Determine PID of process(es) to stop
	#
	if [ "$XNTPD" -ne 1 ]; then
	    rval=2
        fi
		# Don't grab the pid of the KILL script "grep -v K"
		# We only want the daemon
#	    pid=`ps -e | grep -v K | awk '$NF~/xntpd/ {print $1}'`
            pid=`ps -el | grep -v K | awk '( ($NF ~ /xntpd/) && ($4 != mypid) && ($5 != mypid)) { print $4 }' mypid=$$ `
	    if [ "X$pid" != "X" ]; then
		if kill $pid; then
		    echo "xntpd stopped"
		else
		    rval=1
		    echo "Unable to stop xntpd"
		fi
	    else
		rval=1
		echo "ERROR: Unable to stop xntpd (cannot find pid)"
	    fi
	;;
*)
	echo "usage: $0 {start|stop}"
	rval=1
	;;
esac

exit $rval
