#!/bin/sh
### BEGIN INIT INFO
# Provides:            hwclock
# Required-Start:      mountvirtfs
# Required-Stop:       $local_fs
# Should-Start:        modules
# Should-Stop:         $syslog
# Default-Start:       S
# Default-Stop:        0 6
# X-Start-Before:      checkroot
# Short-Description:   Synchronize the system and hardware clocks
# Description:         During boot sync the hardware clock to the
#                      system clock if udev is not running. During
#                      shutdown do the reverse
# X-Required:          true
### END INIT INFO

. /lib/lsb/init-functions
[ -r /etc/default/rcS ] && . /etc/default/rcS
[ -z $container ] || exit 0

case "$UTC" in
  0|n|no)
    ZONE="--localtime"
    ;;
  *)
    ZONE="--utc"
    ;;
esac

case "$1" in
  start)
    if [ ! -f /etc/adjtime ]; then
      printf "0.0 0 0.0\n0\nUTC\n" > /etc/adjtime
    fi

    if [ -d /run/udev ] || [ -d /dev/.udev ]; then
      exit 0;
    fi

    log_init_msg "Setting the system clock"
    hwclock --hctosys $ZONE >/dev/null && log_success_msg || log_failure_msg
    ;;
  stop|restart)
    log_init_msg "Setting hardware clock"
    hwclock --systohc $ZONE >/dev/null && log_success_msg || log_failure_msg
    ;;
  status)
    echo -n "System clock:   "
    date +'%Y-%m-%d %H:%M:%S'
    echo -n "Hardware clock: "
    hwclock --show $ZONE
    ;;
  *)
    echo "Usage: $0 [start|stop|restart]"
    exit 1
    ;;
esac

exit 0
