#!/bin/sh

# Description:       The purpose of fll-locales is to calculate required
#                    strings to configure the locale settings of system
#                    according to given lang= string.

###
# F.U.L.L.S.T.O.R.Y
#
# Copyright: (C) 2007 - 2024 Kel Modderman <kelvmod@gmail.com>
#            (C) 2008 Michael Deelwater <michael.deelwater@googlemail.com>
#            (C) 2016 Niall Walsh <niallwalsh@celtux.org>
#            (C) 2017 - 2024 Stefan Lippers-Hollmann <s.l-h@gmx.de>
# License:   GPLv2
#
# F.U.L.L.S.T.O.R.Y Project Homepage:
# https://github.com/fullstory
###

PATH=/usr/sbin:/usr/bin
NAME="fll-locales"

###
# source distro-defaults if executed explcitly (e.g. by calamares)
###
[ "${1}" = "localize" ] && . /etc/default/distro

###
# source LANG functions
###
. /usr/share/fll-live-initscripts/locales

XKBMODEL="pc105"
XKBLAYOUT="us"
XKBVARIANT=""
XKBOPTIONS=""

###
# cheatcode handling
###
if [ -f /proc/cmdline ]; then
	for param in $(cat /proc/cmdline); do
		case "${param}" in
			lang=*)
				LANGUAGE=$(awk 'BEGIN{ print tolower("'"${param#lang=}"'") }')
				;;
			noaptlang)
				NOAPTLANG="yes"
				;;
			xkboptions=*)
				KBOPTIONS="${param#xkboptions=}"
				;;
			keytable=*)
				KEYTABLE="${param#keytable=}"
				;;
			xkbmodel=*)
				KBMODEL="${param#xkbmodel=}"
				;;
			xkbvariant=*)
				KBVARIANT="${param#xkbvariant=}"
				;;
		esac
	done
fi

###
# lang cheatcode can optionally be made of two dash-separated parts ll-cc
# ll -> language code
# cc -> demographic code
###
LANG_CODE=${LANGUAGE%%[-_]*}
DEMO_CODE="$(echo ${LANGUAGE##*[-_]} | awk '{print toupper($1)}')"

LANG=""

# First check if they entered a built in lang
for LOCALE in ${LOCALES}; do
	[ "${LOCALE}" = "${LANG_CODE}_${DEMO_CODE}.utf8" ] && LANG="${LOCALE}" && break

	# match the language to find the default and possible locales
	case "${LOCALE}" in
		${LANG_CODE}_*)
			[ -z "${LANG_POSS}" ] && LANG_POSS="${LOCALE}"
			fll_locale_default ${LOCALE} && LANG_DEF="${LOCALE}"
			;;
	esac
done

# See if we have some knowledge on how to setup the requested locale
if [ -z "${LANG}" ]; then
	for LOCALE in fll_locale_cheats; do
		[ "${LOCALE}" = "${LANG_CODE}_${DEMO_CODE}.utf8" ] && \
			fll_locale_lang ${LOCALE} && break
	done
fi

# Ok just fallback to the default language, or whatever we know about or en_US
if [ -z "$LANG" ]; then
	if [ -n "${LANG_DEF}" ]; then
		LANG="${LANG_DEF}"
	else
		if [ -n "${LANG_POSS}" ]; then
			LANG="${LANG_POSS}"
		else
			LANG="en_US.utf8"
		fi
	fi
fi
export LANG

###
# if demographic code was ommitted, extract default demo_code from LANG
###
if [ -z "${DEMO_CODE}" ]; then
	LANG_CHECK="${LANG%%.*}"
	DEMO_CODE="${LANG_CHECK##*_}"
fi

#set tz, mirror, xkb via fll_locale_demo
if [ "${LANG}" = "${LANG_CODE}_${DEMO_CODE}.utf8" ]; then
	fll_locale_demo ${LANG}
else
	# We've altered their LANG
	fll_locale_cheats
	for LOCALE in ${FLL_LOCALE_CHEATS}; do
		# if we have their locale
		if [ "${LOCALE}" = "${LANG_CODE}_${DEMO_CODE}.utf8" ]; then
			DEMO="${LOCALE}"
			break
		fi

		# if we have a locale in their country
		if [ "${LOCALE#*_}" = "${DEMO_CODE}.utf8" ]; then
			[ -z "${DEMO_POSS}" ] && DEMO_POSS="${LOCALE}"
		fi
	done

	# if it's not a country we know about fallback to default 00_00
	if [ -z "${DEMO}" ]; then
		if [ -n "${DEMO_POSS}" ]; then
			DEMO="${DEMO_POSS}"
		else
			DEMO="00_00.utf8"
		fi
	fi

	fll_locale_demo ${DEMO}
fi

###
# allow KEYTABLE to update above XKBLAYOUT settings
###
if [ -n "${KEYTABLE}" ]; then
	XKBLAYOUT="${KEYTABLE}"
fi

###
# allow KBOPTIONS to update above XKBOPTIONS settings
###
if [ -n "${KBOPTIONS}" ]; then
	XKBOPTIONS="${KBOPTIONS}"
fi

###
# allow KBMODEL to update above XKBMODEL settings
###
if [ -n "${KBMODEL}" ]; then
	XKBMODEL="${KBMODEL}"
fi

###
# allow KBVARIANT to update above XKBVARIANT settings
###
if [ -n "${KBVARIANT}" ]; then
	XKBVARIANT="${KBVARIANT}"
fi

set_locale()
{
	if grep -q "^LANG=${LANG}" /etc/locale.conf; then
		return 0
	fi

	echo "${NAME}: configuring locales for '${LANG}'"
	echo "locales locales/default_environment_locale select ${LANG}" | \
		debconf-set-selections
	update-locale "LANG=${LANG}"
}

set_keytable()
{
	if ! grep -q "XKBLAYOUT=\"${XKBLAYOUT%%,*}\"" /etc/default/keyboard; then
		# generate new keyboard-configuration conffile
		rm -f /etc/default/keyboard
		DEBCONF=$(mktemp)
		cat >"${DEBCONF}" <<EOF
keyboard-configuration keyboard-configuration/layoutcode string ${XKBLAYOUT%%,*}
keyboard-configuration keyboard-configuration/modelcode string ${XKBMODEL}
keyboard-configuration keyboard-configuration/variantcode string ${XKBVARIANT}
keyboard-configuration keyboard-configuration/optionscode string ${XKBOPTIONS}
EOF
		debconf-set-selections < "${DEBCONF}"
		rm -f "${DEBCONF}"
		dpkg-reconfigure -fnoninteractive keyboard-configuration

		if ! ischroot; then
			udevadm trigger --property-match=ID_INPUT_KEYBOARD=1
		fi
	fi
}

case "${1}" in
	start)
		set_locale
		set_keytable
		;;
	localize)
		[ -n "${2}" ] && LANG="${2}"
		# localize
		set_locale
		set_keytable
		;;
	*)
		echo "Usage: ${NAME} {start|localize}" >&2
		exit 3
		;;
esac

:
