#!/bin/sh
#
# Walt Reinemer
# last edit: 04 dec 2004 wmr
#
# This script is an interface to netdate
# usage: setdate noargs
#
#http://www.loblolly.net/~rddecker/helppages/MiniHOWTOS/systemclock/systemclock-twoclocks.htm
#
# now to set the system date to the most reliable time server
# netdate 129.6.15.28 132.163.4.102 131.107.1.10 207.200.81.113
# see http://www.bldrdoc.gov/timefreq/service/time-servers.html
#
 if [ $# -ne 0 ]; then
   echo -e "\n     usage: setdate no arguments!\n"
   exit 0
 fi
 xc=0; uzr=$UID

ndate( ){
   /usr/sbin/netdate  129.6.15.28   132.163.4.102131.107.1.10  207.200.81.113
   }

 while [ $xc -ne 99 ]; do
   clear
   trap '' 1 2 3 5 15 24
#  lets see what the current date/time is


#    echo -e "\n       CMOS/Hardware date/time: $hwc "
#    echo -e "      Current System date/time: $sys \n"

   #or if clock is not available
   #hwclock --show

   echo -e "\n             Sync Time \n      =======================\n       1. Yes Set Time\n       2. Check local Time\n       3. Check Network Time\n       4. Compare Both Times\n       5. E X I T  M E N U\n      =======================\n        Enter Valid Number!   \n"

   read xc
   cmb=$(echo "$xc.$uzr")

   case $cmb in
    1.0 )
        ndate

        echo -e "    The Sytem and CMOS/Hardware clock "
        echo -e "    date/time will be set to: "
        date '+     %a %b %d %T %Y'
        /sbin/clock -w     ;;        # set CMOS clock
    1.* ) echo -e "\n     You are not root user!!\n"
        read vv                                      ;;
    2.* )
        hwc=$(/sbin/clock -r)              # hardware date/time
        sys=$(date '+%a %b %d %T %Y')      # system date/time
        echo -e "\n       CMOS/Hardware date/time: $hwc "
        echo -e "      Current System date/time: $sys \n"
        read vv;    xc=0                                      ;;

    3.* )
        ndate;  read vy       ;;
    4.* )
        hwc=$(/sbin/clock -r)              # hardware date/time
        sys=$(date '+%a %b %d %T %Y')      # system date/time
        echo -e "\n       CMOS/Hardware date/time: $hwc "
        echo -e "      Current System date/time: $sys \n"
        ndate
        read vy       ;;

    5.* ) echo -e "\n     BYE !!!"
        xc=99                      ;;
    *.* ) echo -e "\n    NOT a VALID ENTRY!!!\n"
        xc=0                       ;;
   esac

 done

# for finding other IPs to use for netdate see the below
# see http://www.bldrdoc.gov/timefreq/service/time-servers.html
#
#      Name                    IP Address         Location
# time-a.nist.gov      **      129.6.15.28     NIST, Gaithersburg, Maryland
# time-b.nist.gov               129.6.15.29     NIST, Gaithersburg, Maryland
# time-a.timefreq.bldrdoc.gov   132.163.4.101   NIST, Boulder, Co.
# time-b.timefreq.bldrdoc.gov**  132.163.4.102   NIST, Boulder, Co.
# time-c.timefreq.bldrdoc.gov   132.163.4.103   NIST, Boulder, Co.
# utcnist.colorado.edu         128.138.140.44      University of Co., Boulder
# time.nist.gov             192.43.244.18       NCAR, Boulder, Co.
# time-nw.nist.gov  **      131.107.1.10        Microsoft, Redmond, Wa.
# nist1.datum.com           66.243.43.21   Datum, San Jose, California
# nist1-dc.glassey.com         216.200.93.8    Abovenet, Virginia
# nist1-ny.glassey.com      208.184.49.9    Abovenet, New York City
# nist1-sj.glassey.com         207.126.98.204  Abovenet, San Jose, Ca.
# nist1.aol-ca.truetime.com**207.200.81.113 TrueTime, AOL facility, Sunnyvale, Ca.
# nist1.aol-va.truetime.com 205.188.185.33 TrueTime, AOL facility, Virginia
