#!/bin/bash

#Comprobe if X11 keymap was selected
code="$(grep XKBMAP= .codecheck | cut -d '=' -f 2)"
keymaps_list="$(sed -e '/! layout/,/^$/!d;/! layout/d;s/ /_/g;s/__/ /g;s/ _//' /usr/share/X11/xkb/rules/evdev.lst | sort)"

if [[ $code = "0" ]]; then
	# shellcheck disable=SC2086
	keymap="$(zenity --list --title='Select your keymap' --column='Code Name' --column='Keymap' --hide-column=1 $keymaps_list)"
	setxkbmap "$keymap"

	#Save 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
