#!/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_LOCALES with pica's value 
				LIVE_LOCALES="${_PARAMETER#*pica=}"
			;;
		esac
		# Specific Pica locales
		case "${_PARAMETER}" in
			live-config.locales.pica=*|live-config.l.pica=*|live-config.lpica=*|locales.pica=*|locale.pica=*|local.pica=*|l.pica=*|lpica=*)
				# Set variable LIVE_LOCALES with pica's value 
				LIVE_LOCALES="${_PARAMETER#*pica=}"
			;;
		esac
	done
}

Init ()
{
	# Checking if a locale package is installed
	if [ ! -e /var/lib/dpkg/info/locales.list ] && \
	   [ ! -e /var/lib/dpkg/info/locales-all.list ]
	then
		exit 0
	fi
#	# Checking if the locale has already been configured
#	if [ -e /var/lib/live/config/locales ]
#	then
#		exit 0
#	fi

	echo -n " locales.pica"
}

Config ()
{
	for _PRE_LOCALE in $(echo ${LIVE_LOCALES} | sed 's|,| |g')
	do
		
		_LOCALE1=$(echo ${_PRE_LOCALE} | cut -d '.' -f 1 | cut -d '@' -f 1 | cut -d '_' -f 1 | tr "[A-Z]" "[a-z]")
		_LOCALE2=$(echo ${_PRE_LOCALE} | cut -d '.' -f 1 | cut -d '@' -f 1 | cut -d '_' -f 2 | tr "[a-z]" "[A-Z]")
		_LOCALE3=$(echo ${_PRE_LOCALE} | cut -s -d '.' -f 2 | cut -d '@' -f 1)
		_LOCALE4=$(echo ${_PRE_LOCALE} | cut -s -d '@' -f 2 | cut -d '.' -f 1)

		if [ -z "${_LOCALE3}" ]
		then
		_LOCALE3="UTF-8"
		fi

		_LOCALE3m="$(echo ${_LOCALE3} | sed 's|-||g' | tr "[A-Z]" "[a-z]")"
		
#		_PRE_LOCALE_m="$(echo ${_PRE_LOCALE} | sed 's|\.utf8|.UTF-8|g')"

		if [ -z "${_LOCALE2}" ]
		then
		_LOCALE2=$(echo ${_LOCALE1} | tr "[a-z]" "[A-Z]")
		fi

#		if [ -e "/usr/lib/locale/${_PRE_LOCALE}" ]
#		then
#			_LOCALE="${_PRE_LOCALE}"
#		elif [ -e "/usr/lib/locale/${_PRE_LOCALE_m}" ]
#		then
#			_LOCALE="${_PRE_LOCALE}"
		if [ -e "/usr/lib/locale/${_LOCALE1}_${_LOCALE2}.${_LOCALE3m}@${_LOCALE4}" ]
		then
			_LOCALE="${_LOCALE1}_${_LOCALE2}.${_LOCALE3}@${_LOCALE4}"
		elif [ -e "/usr/lib/locale/${_LOCALE1}_${_LOCALE2}.${_LOCALE3m}" ]
		then
			_LOCALE="${_LOCALE1}_${_LOCALE2}.${_LOCALE3}"
		elif [ -e "/usr/lib/locale/${_LOCALE1}_${_LOCALE2}@${_LOCALE4}" ]
		then
			_LOCALE="${_LOCALE1}_${_LOCALE2}@${_LOCALE4}"
		elif [ -e "/usr/lib/locale/${_LOCALE1}_${_LOCALE2}" ]
		then
			_LOCALE="${_LOCALE1}_${_LOCALE2}"
		elif [ -e "/usr/lib/locale/${_LOCALE1}.${_LOCALE3m}" ]
		then
			_LOCALE="${_LOCALE1}.${_LOCALE3}"
		elif [ -e "/usr/lib/locale/${_LOCALE1}" ]
		then
			_LOCALE="${_LOCALE1}"
		elif ls /usr/lib/locale/ | grep -a "^${_LOCALE1}_" | grep -aqs "${_LOCALE3m}"
		then
			_LOCALE="$(ls /usr/lib/locale/ | grep -a "^${_LOCALE1}_" | grep -a "${_LOCALE3m}" | sed 's|\.utf8|.UTF-8|g' | head -n 1)"
		elif ls /usr/lib/locale/ | grep -aqs "^${_LOCALE1}_"
		then
			_LOCALE="$(ls /usr/lib/locale/ | grep -a "^${_LOCALE1}_" | head -n 1)"
		elif [ -e "/usr/share/i18n/SUPPORTED" ]
		then
#			if grep -aqs "^${_PRE_LOCALE} " /usr/share/i18n/SUPPORTED
#			then
#				_LOCALE="${_PRE_LOCALE}"
#			elif grep -aqs "^${_PRE_LOCALE_m} " /usr/share/i18n/SUPPORTED
#			then
#				_LOCALE="${_PRE_LOCALE}"
			if grep -aqs "^${_LOCALE1}_${_LOCALE2}.${_LOCALE3}@${_LOCALE4} " /usr/share/i18n/SUPPORTED
			then
				_LOCALE="${_LOCALE1}_${_LOCALE2}.${_LOCALE3}@${_LOCALE4}"
			elif grep -aqs "^${_LOCALE1}_${_LOCALE2}.${_LOCALE3} " /usr/share/i18n/SUPPORTED
			then
				_LOCALE="${_LOCALE1}_${_LOCALE2}.${_LOCALE3}"
			elif grep -aqs "^${_LOCALE1}_${_LOCALE2}@${_LOCALE4}" /usr/share/i18n/SUPPORTED
			then
				_LOCALE="${_LOCALE1}_${_LOCALE2}@${_LOCALE4}"
			elif grep -aqs "^${_LOCALE1}_${_LOCALE2}" /usr/share/i18n/SUPPORTED
			then
				_LOCALE="${_LOCALE1}_${_LOCALE2}"
			elif grep -aqs "^${_LOCALE1}.${_LOCALE3}" /usr/share/i18n/SUPPORTED
			then
				_LOCALE="${_LOCALE1}.${_LOCALE3}"
			elif grep -aqs "^${_LOCALE1} " /usr/share/i18n/SUPPORTED
			then
				_LOCALE="${_LOCALE1}"
			elif grep -a "^${_LOCALE1}_" /usr/share/i18n/SUPPORTED | grep -aqs "${_LOCALE3}"
			then
				_LOCALE="$(grep -a "^${_LOCALE1}_" /usr/share/i18n/SUPPORTED | grep -a "${_LOCALE3}" | head -n 1 | cut -d ' ' -f 1)"
			elif grep -aqs "^${_LOCALE1}_" /usr/share/i18n/SUPPORTED
			then
				_LOCALE="$(grep -a "^${_LOCALE1}_" /usr/share/i18n/SUPPORTED | head -n 1 | cut -d ' ' -f 1)"
			else
				_LOCALE=""
			fi
		else
			_LOCALE=""
		fi
		
		if [ -n "${_LOCALE}" ]
		then
			break
		fi
		
	done

	if [ -z "${_LOCALE}" ] && [ ! -e /var/lib/live/config/locales ]
	then
		_LOCALE="en_US.UTF-8"
	fi

	if [ -n "${_LOCALE}" ]
	then
		
		echo "LANG=${_LOCALE}" > /etc/default/locale
		export "LANG=${_LOCALE}"
		set "LANG=${_LOCALE}"
		if command -v systemctl >/dev/null 2>/dev/null; then
			systemctl set-environment "LANG=${_LOCALE}"
		fi

		# Generate locale if it does not exist
		if [ ! -e "/usr/lib/locale/$(echo ${_LOCALE} | sed 's|\.UTF-8|.utf8|g')/LC_MESSAGES/" ]
		then
			if [ -e /etc/locale.gen ]
			then
#				for _LOCALE in $(echo ${LIVE_LOCALES} | sed 's|,| |g')
#				do
					_CHARSET="$(echo ${_LOCALE} | cut -s -d '.' -f 2 | cut -d '@' -f 1)"
					if [ -z "${_CHARSET}" ]
					then
						_CHARSET="UTF-8"
					fi
					sed -i -e "s|# ${_LOCALE} ${_CHARSET}|${_LOCALE} ${_CHARSET}|" /etc/locale.gen
#				done
			fi
			locale-gen --keep-existing > /dev/null 2>&1
		fi

		# Creating state file
		touch /var/lib/live/config/locales
		
	fi
}

Cmdline
Init
Config
