#!/bin/sh
#
# init system time from RTC value
#
# NOTES:
# To adjust for the drift of the RTC, you have to issue the command
#   hwclock --set --date=<exacttime> 
# manually 2 times with a considerable time (several days) in between.
# The more drift you let accumulate the better hwclock can compute the
# drift.

#locate and source functions script
source `PATH=$INIT_D/:/sbin/init.d:/etc/init.d:/etc/rc.d type -p functions`

#in sysconfig, env. variable UTC is set to yes/true/1 if RTC set to GMT
source $sysconfig_clock

case "$1" in
  start)
     #need to write /etc/adjtime
     #I guess setting clock will work even if /etc/adjtime can't be written,
     #so don't exit_if_any_error
     need $mounted_filesystems
  
     echo 'Adjusting RTC and setting system time...'
     CLOCKPARAMS="--hctosys"
     case "$UTC" in
         yes|true|1) 
             CLOCKPARAMS="$CLOCKPARAMS --utc"
             ;;
         *) 
             CLOCKPARAMS="$CLOCKPARAMS --localtime"
             ;;
     esac 
     hwclock --adjust && hwclock $CLOCKPARAMS
     up_evaluate_retval || exit_if_any_error
     ;;
  stop)
     ;;
  *)
     exit 1
     ;;
esac
    
exit 0
