#!/bin/sh
# hwclock.sh	Set and adjust the CMOS clock.
#
# Version:	@(#)hwclock.sh  2.00  14-Dec-1998  miquels@cistron.nl
#
# Patches:
#		2000-01-30 Henrique M. Holschuh <hmh@rcm.org.br>
#		 - Minor cosmetic changes in an attempt to help new
#		   users notice something IS changing their clocks
#		   during startup/shutdown.
#		 - Added comments to alert users of hwclock issues
#		   and discourage tampering without proper doc reading.
#               2012-02-16 Roger Leigh <rleigh@debian.org>
#                - Use the UTC/LOCAL setting in /etc/adjtime rather than
#                  the UTC setting in /etc/default/rcS.  Additionally
#                  source /etc/default/hwclock to permit configuration.

### BEGIN INIT INFO
# Provides:          hwclock
# Required-Start:    mountdevsubfs
# Required-Stop:     mountdevsubfs
# Should-Stop:       umountfs
# Default-Start:     S
# X-Start-Before:    checkroot
# Default-Stop:      0 6
# Short-Description: Sync hardware and system clock time.
### END INIT INFO

# These defaults are user-overridable in /etc/default/hwclock
setvar BADYEAR = 'no'
setvar HWCLOCKACCESS = 'yes'
setvar HWCLOCKPARS = ''
setvar HCTOSYS_DEVICE = 'rtc0'

# We only want to use the system timezone or else we'll get
# potential inconsistency at startup.
unset TZ

proc hwclocksh {
    test ! -x /sbin/hwclock && return 0
    test ! -r /etc/default/rcS || source /etc/default/rcS
    test ! -r /etc/default/hwclock || source /etc/default/hwclock

    source /lib/lsb/init-functions
    proc verbose_log_action_msg { test $VERBOSE = no || log_action_msg @ARGV; }

    case (BADYEAR) {
       no|"" {	setvar BADYEAR = """" }
       yes {	setvar BADYEAR = ""--badyear"" }
       * {	log_action_msg "unknown BADYEAR setting: \"$BADYEAR\""; return 1 }
    }

    case (1) {
	start {
	    # If the admin deleted the hwclock config, create a blank
	    # template with the defaults.
	    if test -w /etc && test ! -f /etc/adjtime && test ! -e /etc/adjtime {
	        printf "0.0 0 0.0\n0\nUTC\n" > /etc/adjtime
	    }

	    if test -d /run/udev || test -d /dev/.udev {
		return 0
	    }

	    if test $HWCLOCKACCESS != no {
		log_action_msg "Setting the system clock"

		# Just for reporting.
		if sed '3!d' /etc/adjtime | grep -q '^UTC$' {
		    setvar UTC = ""--utc""
		} else {
		    setvar UTC = ''
		}
		# Copies Hardware Clock time to System Clock using the correct
		# timezone for hardware clocks in local time, and sets kernel
		# timezone. DO NOT REMOVE.
		if /sbin/hwclock --rtc=/dev/$HCTOSYS_DEVICE --hctosys $HWCLOCKPARS $BADYEAR {
		    #	Announce the local time.
		    verbose_log_action_msg "System Clock set to: $(date $UTC)"
		} else {
		    log_warning_msg "Unable to set System Clock to: $(date $UTC)"
		}
	    } else {
		verbose_log_action_msg "Not setting System Clock"
	    }
	    }
	stop|restart|reload|force-reload {
	    #
	    # Updates the Hardware Clock with the System Clock time.
	    # This will *override* any changes made to the Hardware Clock.
	    #
	    # WARNING: If you disable this, any changes to the system
	    #          clock will not be carried across reboots.
	    #

	    if test $HWCLOCKACCESS != no {
		log_action_msg "Saving the system clock"
		if /sbin/hwclock --rtc=/dev/$HCTOSYS_DEVICE --systohc $HWCLOCKPARS $BADYEAR {
		    verbose_log_action_msg "Hardware Clock updated to $(date)"
		}
	    } else {
		verbose_log_action_msg "Not saving System Clock"
	    }
	    }
	show {
	    if test $HWCLOCKACCESS != no {
		/sbin/hwclock --rtc=/dev/$HCTOSYS_DEVICE --show $HWCLOCKPARS $BADYEAR
	    }
	    }
	* {
	    log_success_msg "Usage: hwclock.sh {start|stop|reload|force-reload|show}"
	    log_success_msg "       start sets kernel (system) clock from hardware (RTC) clock"
	    log_success_msg "       stop and reload set hardware (RTC) clock from kernel (system) clock"
	    return 1
	    }
    }
}

hwclocksh @ARGV