#!/bin/sh

. /usr/lib/live/config.sh

## live-config(7) - System Configuration Components
## Copyright (C) 2016-2020 The Debian Live team
## Copyright (C) 2006-2015 Daniel Baumann <mail@daniel-baumann.ch>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.


#set -e

Cmdline ()
{
	# Reading kernel command line
	for _PARAMETER in ${LIVE_CONFIG_CMDLINE}
	do
		case "${_PARAMETER}" in
			live-config.utc=*|utc=*)
				LIVE_UTC="${_PARAMETER#*utc=}"
				;;
		esac
	done
}

Init ()
{
	# Checking if package is installed
	if ! pkg_is_installed "util-linux" || \
	   component_was_executed "util-linux"
	then
		exit 0
	fi

	echo -n " util-linux"
}

Config ()
{
	rm -f /etc/rc?.d/*hwclock*
	rtc_create_flag=0

	if [ -n "${LIVE_UTC}" ]
	then
		if [ ! -c /dev/rtc ]; then
			# A workaround to make hwclock can read the time from system.
			# Somehow /dev/rtc is not created yet in Debian, but it's created
			# in Ubuntu.
			# Ref: https://sourceforge.net/p/clonezilla/discussion/Clonezilla_live/thread/cc9923ad
			mknod -m 640 /dev/rtc0 c 254 0
			ln -fs /dev/rtc0 /dev/rtc
			rtc_create_flag=1
		fi

		case "${LIVE_UTC}" in
			yes)

cat > /etc/adjtime << EOF
0.0 0 0.0
0
UTC
EOF

				# Make it effect now
				hwclock -s --utc
				;;

			no)

cat > /etc/adjtime << EOF
0.0 0 0.0
0
LOCAL
EOF

				# Make it effect now
				hwclock -s --localtime
				;;
		esac
	fi

	# Revert to the original status
	if [ "$rtc_create_flag" = 1 ]; then
          rm -f /dev/rtc0 /dev/rtc
	fi

	# Creating state file
	touch /var/lib/live/config/util-linux
}

Cmdline
Init
Config
