#!/bin/ash

tweak_system() {
	if [ "$mediadetected" = "cd" ]; then
		echo -e "#!/bin/sh\neject $livedevice\nsleep 5" > /mnt/usr/lib/systemd/system-shutdown/live-system.shutdown
		chmod +x /mnt/usr/lib/systemd/system-shutdown/live-system.shutdown
	fi

}

enable_persistent_wifi() {
	cat > /mnt/etc/systemd/system/live-system-start-wifi.service << EOF
[unit]
Description="Live System WiFi start"
After=NetworkManager.service
Requires=NetworkManager.service
After=home.mount

[Service]
Type=oneshot
ExecStart=live-system-wifi.sh start
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF
	ln -sf ../live-system-start-wifi.service /mnt/etc/systemd/system/multi-user.target.wants

	cat > /mnt/etc/systemd/system/live-system-stop-wifi.service << EOF
[unit]
Description="Live System WiFi stop"

[Service]
Type=oneshot
ExecStop=live-system-wifi.sh stop
RemainAfterExit=yes
Before=home.mount

[Install]
WantedBy=multi-user.target
EOF
	ln -sf ../live-system-stop-wifi.service /mnt/etc/systemd/system/multi-user.target.wants
}

setup_locale() {
	if [ ! -z "$locale" ]; then
		echo "Setting up locale ($locale)"
		echo "LANG=$locale" > /mnt/etc/default/locale
	fi
}

setup_time() {
	if [ ! -z "$tz" ]; then
		if [ -f /mnt/usr/share/zoneinfo/$tz ]; then
			echo "Setting up timezone ($tz)"
			ln -sf /usr/share/zoneinfo/$tz /mnt/etc/localtime
			echo "$tz" > /mnt/etc/timezone
		fi
	fi
	if [ ! -z "$hwc" ]; then
		if [ "$hwc" = "UTC" ] || [ "$hwc" = "localtime" ]; then
			echo "Setting up hardware clock ($hwc)"
			if [ "$hwc" == "UTC" ]; then
				rm -f /mnt/etc/adjtime
			else
				echo -e "0.0 0 0\n0\nLOCAL" > /mnt/etc/adjtime
			fi
		fi
	fi
}

setup_console_keymap() {
	if [ ! -z "$keymap" ]; then
		echo "Setting up keymap ($keymap)"
		sed s/XKBLAYOUT=.*/XKBLAYOUT=\"$keymap\"/ -i /mnt/etc/default/keyboard
	fi
}
