#!/bin/sh
# NTP time synchronisation
#
# /src/NTP/REPOSITORY/v3/supportscripts/etc/rc,v 1.11 1993/07/09 13:17:00 kardel Exp
#
# rc,v
# Revision 1.11  1993/07/09  13:17:00  kardel
# local NTPROOT
#
# Revision 1.10  1993/07/09  11:37:29  kardel
# Initial restructured version + GPS support
#
# Revision 1.9  1993/06/23  14:10:36  kardel
# June 21st reconcilation
#
# Revision 1.7  1993/06/02  12:04:43  kardel
# May 28th reconcilation & clenaup
#
#
# non reference clock hosts will try to do an ntpdate on NTPSERVERS
#
NTPSERVERS="ntps1-0 ntps1-1 ntps2-0 ntps2-1"
NTPROOT=/local/NTP

#
# get the initial setup
#
if [ ! -r $NTPROOT/etc/setup ]; then
	echo "ERROR: $NTPROOT/etc/setup missing - incorrect installation."
	exit 1
else
	. $NTPROOT/etc/setup
fi

umask 022                           # SITE SPECIFIC: local policy - watch out for NFS and "root" rights

msg=""
#
# default configuration files are named $NTPROOT/conf/<ARCH>.<KARCH>
#
CF=$NTPROOT/conf/$ARCH.$KARCH     # default configuration file
#
# Host specific config file (reference clocks) have the hostname tagged on
#
CFH="$CF"."$HOSTNAME"             # specific configuration file
#
# where to find the tickadj command
#
KFIX=$NTPROOT/bin/tickadj         # kernel variable fix
#
# where to find special tickadj parameters
#
TC=$NTPROOT/conf/tickconf         # special tickadj parameters
#
# where to find the keys file (if not found $KEY.dumb will be used)
#
KEY=$NTPROOT/conf/ntp.keys        # private key file
#
# the daemon
#
XD=$NTPROOT/bin/xntpd             # NTP daemon
#
# HP adjtimed
#
ADJTIMED=$NTPROOT/bin/adjtimed    # HP special (adjtime() emulation)
#
# ntpdate command
#
NTPDATE=$NTPROOT/bin/ntpdate

#
# secondary timed support
# The word "TIMED" must be in the config file for timed to start
# Note that this times is a special version which does not ever set or
# adjust the time. Ask time@informatik.uni-erlangen.de for patches
#
TIMED=$NTPROOT/bin/timed	  # timed (Berkeley) secondary time service
				  # here used in a *HARMLESS* version
				  # to provide time to "inferior" systems
#
# ISREFHOST is a command that returns exit status 0 for a reference host
# Site specific: sample for dcf77 is given
#
ISREFHOST="[ -f $NTPROOT/.karch.$KARCH/sys/OBJ/parsestreams.o -a -f /dev/refclock-0 ]"
#
# SETUP_REFCLOCK
#
# what to do in order to set up a local reference clock
# usually this will load a STREAMS module or initialize other things
# needed
#
SETUP_REFCLOCK() {
  if modstat | grep -s 'PARSE'; then
	  ECHO "loadable PARSER STREAMS module already loaded."
  else
	  ECHO "attempting to load PARSER STREAMS module..."
	  MDLFILE="/tmp/mdl.$$"
	  if modload $NTPROOT/.karch.$KARCH/sys/OBJ/parsestreams.o -o $MDLFILE 2>&1; then
	    modstat
	  else
	    echo WARNING: load FAILED
	  fi | LOG
	  rm -f $MDLFILE
	  unset MDLFILE
  fi
}

kargs() {
  MATCH=NO
  HOSTID="`(hostid) 2>/dev/null || echo 000000`"
  if [ -r "$TC" ]; then
    exec 0< "$TC"
    while [ "$MATCH" != "YES" ] && read HOST ID PARAM; do
      if [ "$HOST" = "DEFAULT" ]; then
	DEFAULT="$ID $PARAM"
      else
	if [ "$ID" = "$HOSTID" -o "$HOST" = "$HOSTNAME" ]; then
	  echo "$PARAM"
	  MATCH=YES
	fi
      fi
    done
    if [ "$MATCH" != "YES" ]; then
      if [ -z "$DEFAULT" ]; then
	echo "-A -p -s -q";
      else
	echo "$DEFAULT";
      fi
    fi
  else 
    echo "-A -p -s -q";
  fi
}

if [ -x $XD ]; then
  if [ -x "$ADJTIMED" ]; then
     $ADJTIMED && ECHO "adjusttimesupport: adjtimed."
  fi
  #
  # WARNING: check ps command first, or you might kill things you don't want to
  #
  PID="`(ps -efa 2>/dev/null || ps auxww 2>/dev/null || echo "") | grep xntp | grep -v grep | awk '{ print $2 }'`"

  if [ ! -z "$PID" ]; then
    ECHO "killing old NTP daemon (PID=$PID)"
    #
    # enable this after checking for correctness
    # kill $PID
    ECHO "should do a kill $PID, if this is the right PID - check rc script"
  fi
  #
  # try an ntpdate when timeservers are configured
  #
  if [ ! -z "$NTPSERVERS" -a -x $NTPDATE ]; then
	ECHO "NTP initial time setting"
	$NTPDATE -v $NTPSERVERS | LOG
  fi
  #
  # look for reference clock equipment
  #
  if $ISREFHOST; then
	ECHO "REFERENCE CLOCK SUPPORT (initializing...)"
	SETUP_REFCLOCK
  fi

  if [ -r "$CFH" ]; then
    CF="$CFH"
  else
    if [ ! -r "$KEY" ]; then
      KEY="$KEY.dumb"
    fi
  fi

  ECHO "NTP configuration file: $CF"
  ECHO -n "time daemon startup:"

  if [ -r "$CF" ]; then
    if [ -x "$KFIX" ]; then
      KARGS="`kargs`"
      if [ ! -z "$KARGS" ]; then
        $KFIX $KARGS && ECHO -n "tickadj $KARGS"
      fi
    fi
    $XD -c "$CF" -k "$KEY" && ECHO -n ' xntpd'
    if [ -x "$TIMED" ] && grep -s TIMED "$CF"; then
      $TIMED -M -N && ECHO -n ' timed'
    fi
  else
    msg="configuration file ($CF) not present."
  fi
else
  msg="daemon binary ($XD) not present."
fi
ECHO "."

if [ "$msg" ]; then
  NLECHO "WARNING: NO NTP time sychronisation: $msg"
fi
