#!/bin/sh
#        
#	st - (settime)
#	
#	V1.02-00
#
#	
#	(c) 1994 - T. Marauder (Todd Lawrence), LoD Communications.
#
#	Shellscript to update system time.
#	Requires "gettime.c".
#
#

TIMEZONE="AUTO"			# This is the timezone your system is
				# Configured to. if left to "AUTO". st
				# will attempt to automatically extract
				# the proper parameter by examining the
				# 5th field of the string returned by the
				# "date" function. The systems(s) you will
				# be grabbing the time from MUST be within
				# the same timezone as your system, if you
				# wish to use times returned from systems
				# different than your own, you will have to
				# play with this parameter. Otherwise leave
				# it set to "AUTO".
				

ADMIN="root"			# System Admin account to mail problems to.

MAIL="YES"			# mail sysadmin on updates/problems
				# "YES" if not wanted, "NO" if not.

BINPATH="/usr/local/bin"	# where your settime binary resides.

TIMEHOST1="system1.com"		# System who's time you want to steal.
TIMEHOST2="system2.gov"    	# Second Choice  "   "   "    "   "
TIMEHOST3="system3.edu"     	# Third Choice   "   "   "    "   "

HOSTS='3'			# number of hosts.
WAIT='20'			# Number of minutes to wait before trying 
				# next hostname after a failure.


WAIT=`expr $WAIT \* 60`
HOSTS=`expr $HOSTS + 1`
i=1

# Auto extract TIMEZONE parameter.

if [ "$TIMEZONE" = "AUTO" ]
   then
     TIMEZONE=`date`
     set $TIMEZONE
     TIMEZONE=$5
fi


while [ $i != $HOSTS ] 
    do 
    case $i in
	1) HOST=$TIMEHOST1;;
        2) HOST=$TIMEHOST2;;
        3) HOST=$TIMEHOST3;;
    esac
    ctime=`$BINPATH/gettime "$HOST"`
    if [ "$ctime" != "" ]
    then  	
       set $ctime
       date -s "$4 $TIMEZONE" > /dev/null	
       i=$HOSTS
       if [ $MAIL = "YES" ]
       then	
         echo -e  "\n\nSystem time successfully updated from: $HOST\n " \
          | mail -s "Settime SUCCESSFUL"  $ADMIN
       fi
    else
       sleep $WAIT	
       i=`expr $i + 1`
    fi
    done
#
# Send mail to Sysadmin, since all three sites failed.
#
if [ "$ctime" = "" ]
   then
	if [ $MAIL = "YES" ]
        then
	   echo -e "\n\n*** Gettime Failed - Check hosts/connection \n\n" \
            | mail -s "Settime: FAILURE"  $ADMIN
        fi 
fi

#end script: st





