#!/bin/sh
########################################################################
# Begin setclock
#
# Description : Setting Linux Clock
#
# Authors     : Gerard Beekmans - gerard@linuxfromscratch.org
#               DJ Lucas - dj@linuxfromscratch.org
# Update      : Bruce Dubbs - bdubbs@linuxfromscratch.org
#
# Version     : LFS 7.0
#
########################################################################

### BEGIN INIT INFO
# Provides:            setclock
# Required-Start:
# Should-Start:		modules
# Default-Start:	2 3 4 5
# Default-Stop:		0 6
# Short-Description:   Stores and restores time from the hardware clock
# Description:         On boot, system time is obtained from hwclock.  The
#                      hardware clock can also be set on shutdown.
# X-LFS-Provided-By:   LFS BLFS
### END INIT INFO

. /lib/lsb/init-functions

[ -r /etc/sysconfig/clock ] && . /etc/sysconfig/clock


case "${UTC}" in
   yes|true|1)
      CLOCKPARAMS="${CLOCKPARAMS} --utc"
      ;;

   no|false|0)
      CLOCKPARAMS="${CLOCKPARAMS} --localtime"
      ;;

esac

# To detect if we are on container
if [ -n "$(grep 'kthreadd' /proc/2/status 2>/dev/null)" ]; then
  case ${1} in
     start)
        log_info_msg "Getting hardware clock..."
        hwclock --hctosys ${CLOCKPARAMS} >/dev/null
        evaluate_retval
        ;;

     stop)
        log_info_msg "Setting hardware clock..."
        hwclock --systohc ${CLOCKPARAMS} >/dev/null
        evaluate_retval
        ;;

     restart)
        $0 stop
        sleep 0.2
        $0 start
        ;;

     *)
        echo "Usage: ${0} {start|stop}"
        exit 1
        ;;

    esac
  fi

exit 0
