#!/bin/sh
#
# Tazkeymap - SliTaz GNU/Linux keymap config using loadkeys and dialog boxes.
# Configuration file is : /etc/kbd.conf
#
# (C) SliTaz GNU/Linux - 20080427 <pankso@slitaz.org> - GNU gpl.
#
: ${DIALOG=tazdialog}
export ICON="accessories-character-map"

# Script functions.
status()
{
	local CHECK=$?
	echo -en "\\033[70G[ "
	if [ $CHECK = 0 ]; then
		echo -en "\\033[1;33mOK"
	else
		echo -en "\\033[1;31mFailed"
	fi
	echo -e "\\033[0;39m ]"
}

# Check if user is root.
#
if test $(id -u) != 0; then
  echo ""
  echo "You must be root to run `basename $0`!"
  echo "Type 'su' and root password to become super-user."
  echo ""
  exit 1
fi

# Get current keymap if it exists.
if [ -s /etc/keymap.conf ]; then
	CUR=`cat /etc/keymap.conf`
else
	CUR="none"
fi

# Dialog menu.
#
exec 3>&1
value=`$DIALOG  --clear \
    --title " SliTaz keymap configuration " \
    --menu "\nPlease select your keymap, current config: $CUR" 15 70 5 \
"fr_CH-latin1"  "Suisse Romande" \
"fr-latin1"     "France" \
"be-latin1"     "Belgique" \
"br-abnt2"      "Brazil" \
"cf"            "Canada/Quebec" \
"croat"         "Croat" \
"cz-lat2"       "Czech" \
"de_CH-latin1"  "Schweizer Deutsch" \
"de-latin1"     "Deutchland" \
"dk-latin1"     "Danemark" \
"dvorak"        "Dvorak" \
"dvorak-r"      "Dvorak (right-hand)" \
"dvorak-l"      "Dvorak (left-hand)" \
"es"            "Spain/Mexico" \
"fi-latin1"     "Finland" \
"hu"            "Hungria" \
"it"            "Italia" \
"is-latin1"     "Island" \
"jp106"         "Japan" \
"nl2"           "Netherlands" \
"no-latin1"     "Norway" \
"pl2"           "Poland" \
"pt-latin1"     "Portugal" \
"ru"            "Russia" \
"se-lat6"       "Sweden" \
"sg-latin1"     "Singapore " \
"uk"            "United Kingdom" \
"us-acentos"    "USA Acentos" \
"us"            "USA" \
2>&1 1>&3`
retval=$?
exec 3>&-

case $retval in
	0)
		continue ;;
	1)
		echo "Cancel pressed."
		exit 0 ;;
	255)
		if test -n "$value" ; then
			echo "$value"
		else
			echo "ESC pressed."
			exit 0
		fi ;;
esac

# If it's a reconfiguration give an info message.
if [ -s /etc/keymap.conf ]; then
	$DIALOG --clear \
		--title " Keyboard mapping information\n" \
		--msgbox "
Please logout of your current session and login again to use $value
keyboard.\n" 16 70
fi

# Now we can load the selected kmap file from /usr/share/kbd/keymaps.
#
echo "$value" > /etc/keymap.conf
if [ -x /bin/loadkeys ]; then
	loadkeys $value
else
	loadkmap < /usr/share/kmap/$value.kmap
fi

exit 0
