#!/bin/sh
#
# Tazlocale: SliTaz GNU/Linux locale setting using dialog boxes.
# Configuration file is : /etc/locale.conf
#
# 20080417 <pankso@slitaz.org> - GNU gpl.
#
: ${DIALOG=tazdialog}
export ICON="preferences-desktop-locale"

# 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 -e "\nYou must be root to run `basename $0`!"
	echo -e "Type su and root password to become super-user.\n"
	exit 1
fi

# Locale name displayed.
get_locale_name()
{
	for i in `locale -a | grep "[a-z]_"`
	do
		name=`locale -a -v | grep -A 2 "locale: $i" | grep "title" | \
			cut -d " " -f 7`
		echo "$i $name"
	done
}

# Dialog menu.
#
exec 3>&1
value=`$DIALOG  --clear \
	--title " SliTaz locale configuration " \
	--menu \
"Select your language - Slctionnez votre langue" 15 70 5 \
"en" "English" \
$(get_locale_name) \
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

# Default: C = English
if [ "$value" = "en" ]; then
	value='C'
fi

# If it's a reconfiguration give an info message.
if [ -f /etc/locale.conf ]; then
	$DIALOG --clear \
		--title " Locale setting information " \
		--msgbox "\n
Please logout you current session and login again to use $value
locale.\n" 16 70
fi

# System configuration
echo "LANG=$value" > /etc/locale.conf
echo "LC_ALL=$value" >> /etc/locale.conf

. /etc/locale.conf
export LANG LC_ALL

exit 0
