#!/bin/sh

# Copyright (c) 2013-2026: Alexis Puente Montiel   < pica (a) picalibre.org >
#
# Licensed according to GNU AGPL version 3.0.
#
# It is libre/free software; you can use, redistribute and/or modify it according to the terms of GNU AGPL as published by GNU, version 3.0, 19 November 2007.
#
# It is distributed in the hope that it will be useful, but without any warranty. Read GNU AGPL version 3.0 for additional details.
#
# A copy of GNU AGPL version 3.0 is available at /usr/share/doc/<software-package-name>/agpl-3.0.txt (additionally on Internet as text at https://www.gnu.org/licenses/agpl-3.0.txt and as HTML at https://www.gnu.org/licenses/agpl-3.0-standalone.html ).
#
# Note: Additionally to the official e-mails, picalibre.org is strictly the only official site for this software project, please consider using it to download, report bugs and contribute.
#
# Depends: live-config


#set -e

Cmdline ()
{
	# Reading kernel command line
	for _PARAMETER in ${LIVE_CONFIG_CMDLINE}
	do
		# General Pica
		case "${_PARAMETER}" in
			live-config.pica=*|pica=*)
				# Set variable LIVE_KEYBOARD_LAYOUTS with pica's value 
				LIVE_KEYBOARD_LAYOUTS="${_PARAMETER#*pica=}"
			;;
		esac
		# Specific Pica keyboard-layouts
		case "${_PARAMETER}" in
			live-config.keyboard-layouts.pica=*|live-config.keyboard.pica=*|live-config.k.pica=*|live-config.kpica=*|keyboard-layouts.pica=*|keyboard.pica=*|k.pica=*|kpica=*|teclado.pica=*|t.pica=*|tpica=*)
				# Set variable LIVE_KEYBOARD_LAYOUTS with pica's value 
				LIVE_KEYBOARD_LAYOUTS="${_PARAMETER#*pica=}"
			;;
		esac
	done
}

Init ()
{
	# Checking if package is installed
	if [ ! -e /var/lib/dpkg/info/keyboard-configuration.list ]
	then
		exit 0
	fi
	# Checking if package is already configured   # If a explicity keyboard-configuration value is set (default is no value and do not create /var/lib/live/config/keyboard-configuration), keyboard-configuration-pica should not set anything.
	if [ -e /var/lib/live/config/keyboard-configuration ]
	then
		exit 0
	fi

	echo -n " keyboard-configuration.pica"
}

Config ()
{
	for PRE_LIVE_KEYBOARD_LAYOUT in $(echo ${LIVE_KEYBOARD_LAYOUTS} | sed 's|,| |g')
	do
		
		if [ -n "$(echo ${PRE_LIVE_KEYBOARD_LAYOUT} | grep -ai _vndr)" ]
		then
			LIVE_KEYBOARD_LAYOUT=$(echo ${PRE_LIVE_KEYBOARD_LAYOUT} | tr "[A-Z]" "[a-z]")
		elif [ -n "$(echo ${PRE_LIVE_KEYBOARD_LAYOUT} | grep -ai _)" ]
		then
			LIVE_KEYBOARD_LAYOUT=$(echo ${PRE_LIVE_KEYBOARD_LAYOUT} | cut -d '.' -f 1 | cut -d '@' -f 1 | cut -d '_' -f 2 | tr "[A-Z]" "[a-z]")
		else
			LIVE_KEYBOARD_LAYOUT=$(echo ${PRE_LIVE_KEYBOARD_LAYOUT} | cut -d '.' -f 1 | cut -d '@' -f 1 | tr "[A-Z]" "[a-z]")
		fi

		if [ -n "${LIVE_KEYBOARD_LAYOUT}" ]
		then
			break
		fi

	done

	if [ -z "${LIVE_KEYBOARD_LAYOUT}" ] && [ ! -e /var/lib/live/config/keyboard-configuration ]
	then
		LIVE_KEYBOARD_LAYOUT="us"
	fi

	if [ -n "${LIVE_KEYBOARD_LAYOUT}" ]
	then
		
		echo "keyboard-configuration keyboard-configuration/layoutcode select ${LIVE_KEYBOARD_LAYOUT}" >> /tmp/live-config.cfg
		# xserver-xorg:
		echo "xserver-xorg xserver-xorg/config/inputdevice/keyboard/layout select ${LIVE_KEYBOARD_LAYOUT}" >> /tmp/live-config.cfg
		
		sed -i -e "s|^XKBLAYOUT=.*$|XKBLAYOUT=\"${LIVE_KEYBOARD_LAYOUT}\"|g" /etc/default/keyboard
		
#		if command -v loadkeys >/dev/null 2>/dev/null  # loadkeys uses another keymap nomenclature
#		then
#			loadkeys ${LIVE_KEYBOARD_LAYOUT}
#		fi
		
		if command -v setxkbmap >/dev/null 2>/dev/null
		then
			setxkbmap ${LIVE_KEYBOARD_LAYOUT} 2>/dev/null
		fi
		
	fi

	if [ -e /tmp/live-config.cfg ]
	then
		debconf-set-selections < /tmp/live-config.cfg
		rm -f /tmp/live-config.cfg
		
		if [ -x $(command -v setupcon 2>/dev/null) ]; then
			setupcon -k
		fi

		# udev already started, tell it to pick up the new config
		udevadm trigger --subsystem-match=input --action=change

		# Creating state file
		touch /var/lib/live/config/keyboard-configuration
	fi
}

Cmdline
Init
Config
