#!/bin/bash

restart_session=0 #If change locale the system need restart session to see the changes

#Comprobe if language was selected
code=$(cat ~/.codecheck | grep LANG= | tail --bytes 2)
if [ "$code" = "0" ]; then
	lang=$(zenity --list --title="Select your locale (optional)" \
	              --column="Locale" --column="Language" \
		"en_US.UTF-8" "English" \
		"gl_ES.UTF-8" "Galego" \
		"es_ES.UTF-8" "Spanish" \
		"pt_BR.UTF-8" "Brazilian Portuguese")
	[ "$lang" ] || lang="en_US.UTF-8"
	
	#Copy locale in locale.conf
	sudo echo "LANG=$lang" > /etc/locale.conf

	#Put a new line confirm that language was selected
	sed -i '/LANG=./d' ~/.codecheck
	echo "LANG=1" >> ~/.codecheck

	#Copy install scripts in that language
	sudo cp -a /root/.scriptsInstallation/language/${lang/_*/}/* /root/.scriptsInstallation/

	restart_session=1
fi

#Comprobe if X11 keymap was selected
code=$(cat .codecheck | grep XKBMAP= | cut -d '=' -f 2)
if [ "$code" = "0" ]; then
	keymap=$(zenity --list --title="Select your keymap (optional)" \
	                --column="Keymap" $(localectl list-x11-keymap-layouts))
	[ "$keymap" ] || keymap="us"

	setxkbmap $keymap

	#Sae XKBMAP in .codecheck to use in other time. For example if you install X11 with scripts
	sed -i '/XKBMAP=./d' ~/.codecheck
	echo "XKBMAP=$keymap" >> ~/.codecheck
fi

if [ $restart_session -eq 1 ]; then
	#Restart session
	pkill -KILL -u $(whoami)
fi
