#! /bin/sh
#
# 'timehosts'
#
# adjusts system time and hardware clock to a www-based time server.
# See 'man netdate' for technical details.
#
# Of course, this only works if you have an internet connection
# established. You need to run this as root, or may want to call it
# from your /etc/ppp/ip-up file.
#
# Option "-l" allows to run this against a local network computer.
#
# If you have a permanent network connection, install xntp.
#
# -------------------------------------------------------------------
# Creation: 2000-12-31, JHa.
# Revision: 2001-01-07, added entry in syslog file (JHa)
#	    2001-01-20, added basename in syslog file (JHa)
#	    2001-04-30, added comments (JHa)
#	    2002-10-28, replaced 'basename $0' by '${0##*/}'
#	                (JHa, thanks to Roberto Nibali!)
#	    2004-10-03, added option for $LOCALSERVER. Added command
#                       line parser.
#	    2004-10-21, small bugfix in the latest changes.
#	    2005-01-10, license now GPL *v2*.
#       2005-04-14, added $PROTOCOL
#       2007-03-17, changed NTP server according to NTP.org (HST)
# -------------------------------------------------------------------
# Copyright (c) 2000...2007 Joerg Hau <joerg.hau (at) dplanet.ch>.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public 
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# -------------------------------------------------------------------


# Please adjust SERVER to an ntp server matching your timezone.
# Please adjust SERVER to an ntp server matching your timezone.
# It is recommended to use *.pool.ntp.org, where * is the TLD of 
# your country, e.g. "de" for Germany, "ch" for Switzerland, etc.
#
SERVER="ch.pool.ntp.org"

# Protocol is tcp or udp (may depend on your firewall)
#
PROTOCOL="tcp"

# This is for computers on my home network
#
LOCALSERVER="gx.localnet"


# ------------------------------------------------------------
# subroutine to print usage mode
# ------------------------------------------------------------
function usage()
{
cat << eof

${0##*/} -  Synchronize system time to network timeserver.

Copyright (c) 2000...2007 Joerg Hau <joerg.hau(at)dplanet.ch>.

This program is free software; you can redistribute it and/or
modify it under the terms of version 2 of the GNU General Public
License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Usage: ${0##*/} [ -l | --local ] [ -h | --help ]

    Invoking '${0##*/}' with no arguments will synchronize
    system time to '$SERVER'. Otherwise:

    -l, --local       synchonize to '$LOCALSERVER'
    -? | -h | --help  This help.

Notes:

    o Of course, this only works if you have an internet connection
      established.
    o If you have a permanent network connection, install xntp.
    o You must be root to run this script. You may even want
      to call it from your /etc/ppp/ip-up file ;-)
    o Successful synchronisation will be logged to the system log.
eof
}


################## script starts here ;-) ####################

# ------------------------------------------------------------
# handle command line parameters (flags)
# ------------------------------------------------------------
while [ "$1" != "" ]; do
    case $1 in
        -l | --local)   SERVER=$LOCALSERVER
                        shift
                        ;;
        -h | --help )   usage
                        exit 2
                        ;;
        * )             usage
                        exit 1
    esac
    shift
done

# ------------------------------------------------------------
# after all these lines, now really do the time sync
# ------------------------------------------------------------
if
  /usr/sbin/netdate -v $PROTOCOL $SERVER
#  $PROTOCOL swisstime.ethz.ch\
#  $PROTOCOL ptbtime2.ptb.de
then
  /sbin/hwclock --systohc && \
  logger -t ${0##*/} "Hardware clock adjusted to $SERVER."
else
  echo "Clock NOT adjusted."
fi

exit 0
