#!/bin/sh

# Euro test
# Tests for the availability of euro sign in GNU/Linux systesm
#
# (c) 2001 Javier Fernndez-Sanguino Pea <jfs@debian.org>
#
# TODO
# - Join final information on X and console (it's more or less similar)
# - add UTF support?
# - Check (if in console) if the user has either 
#   'console-data (>= 1999.08.29-11.1) | fonty' installed

ECHO=/bin/echo

print_euro_iso8859_15() {
	gettext "Printing the euro character (ISO8859-15): "
	/usr/bin/printf "\xa4\n"
}

print_euro_utf () { 
	gettext "Printing the euro character (UTF): "
	/usr/bin/printf "\xe2\x82\xac\n"
}

read_answer () {
	gettext "Can you see a euro character in the previous line (y/N) "
	read sino
	case "$sino" in
	s*|S*|y*|Y*)	answer=1;;
	*) 	answer=0;;
	esac
}
test_euro_font () {
	print_euro_iso8859_15
	read_answer
	[ $answer = 1 ] && { 
		gettext "Your font has euro support (ISO8859-15)"; $ECHO
		eurofont="iso8859"
		return 0
	}
	print_euro_utf
	read_answer
	[ $answer = 1 ] && { 
		gettext "Your font has euro support (UTF-8)"; $ECHO
		eurofont="utf"
		return 0
	}
	gettext "Your font does not have euro support."; $ECHO
	eurofont=""
	return 1
}

test_euro_key () {
	keycomb="Altgr+E"
	[ ! -z "`locale |grep en_`" ] && keycomb="AltGr+4"
	[ ! -z "`locale |grep lv_`" ] && keycomb="AltGr+4"
	[ ! -z "`locale |grep en_US`" ] && keycomb="AltGr+5"
	[ ! -z "`locale |grep pl_`" ] && keycode="AltGr+U"
	[ ! -z "`locale |grep hu_`" ] && keycode="AltGr+U"
	gettext  "Press the keyboard combination $keycomb: "
	read test
	read_answer
	[ $answer = 1 ] && {
		gettext "Your keyboard is euro-ready."; $ECHO
		eurokey=1
		return 0
	}
	gettext "Your keyboard is not configured for euro support."; $ECHO
	eurokey=0
	return 1
}

set_console_euro_font () {
	gettext "Installing a font with euro support"; $ECHO
	if [ -f /usr/bin/consolechars ] 
	then
		/usr/bin/consolechars -f lat0-16 || {
# Test with other lat0/lat9 fonts??
# This part takes the first one available (might be too ugly)
			for i in /usr/share/consolefonts/lat0*; do
			consolechars -f $i && break
			done
		}
	elif [ -f /usr/bin/setfont ]
	then
		/usr/bin/setfont -f lat0-1
	else
		gettext "Cannot install a new font, consolechars or setfont program not found"; $ECHO
	fi
}

set_console_euro_key () {
	gettext "Reconfiguring the keyboard"; $ECHO
	if [ -f /bin/loadkeys ] ; then
	loadkeys euro.inc.gz || {
		gettext "Error while reconfiguring the keyboard"; $ECHO
		# TODO: This is only valid for these keymaps (not all european
		# languages)
		if [ ! -f /usr/share/keymaps/i386/include/euro.inc.gz ]
		then
			gettext "Your system does not have a euro definition installed"; $ECHO
		else
			gettext "Your euro definition is not correct or could not be installed"; $ECHO
		fi
		set_console_euro_key
	}
	else
		gettext "Cannot find loadkeys, it is not possible to reconfigure the keyboard"; $ECHO
	fi
		
}

set_X_euro_font () {
	gettext "Trying installed X fonts"; $ECHO
	X_fonts=`xlsfonts | grep iso8859-15$`
	if [ ! -z "$X_fonts" ] 
		then
# We could take the first font here and launch a new xterm with it
		gettext "The following fonts with euro support are installed on your system:"; $ECHO
		$ECHO $X_fonts
		if [ "$ARG" = "X" ] 
		then
 			gettext "\
It has not been possible to execute a terminal with a euro font
(second try)."; $ECHO
		else
			gettext "\
This program will be executed again with a terminal that loads a euro font
(using 'x-terminal-emulator -fn \"name_of_font\")"; $ECHO
			xfont=`$ECHO $X_fonts |  sed 's/\(-iso8859-15\) .*/\1/'`
			x-terminal-emulator -fn "$xfont" -e $0 X
		fi 
			exit 0
	else
		gettext "\
There are no fonts with the euro character.
Install the following packages: xfonts-base-transcoded,
xfonts-100dpi-transcoded and xfonts-75dpi-transcoded."; $ECHO
	fi
}

# Setting the euro key

set_console_euro_key () {
	gettext "Trying manual loading"; $ECHO
# TODO: get mapping for other languages
#	[ ! -z "`locale |grep en_`" ] && keycode="keycode 13 = 4 dollar currency cent" 
#	[ ! -z "`locale |grep lv_`" ] && keycode="keycode 13 = 4 dollar currency cent" 
#	[ ! -z "`locale |grep en_US`" ] && keycode="keycode 14 = 5 percent currency cent"
#	[ ! -z "`locale |grep pl_`" ] && keycode="keycode 30 = u U currency cent"
# Not known:
#	[ ! -z "`locale |grep gr_`" -o ! -z "`locale |grep hu_`" -o \
#	! -z "`locale |grep az_`" -o ! -z "`locale |grep lt_`"  ] && {
#		gettext  -s "euro-test cannot determine the mapping for your language..sorry"
#		exit 1
#	}
 	keycode="altgr keycode 18 = currency \n altgr keycode 46 = cent "	
	$ECHO -e $keycode | loadkeys -
}

set_X_euro_key () {
	gettext "Reconfiguring the keyboard in X"; $ECHO
	keycode="keycode 26 = e E currency cent" 
	[ ! -z "`locale |grep en_`" ] && keycode="keycode 13 = 4 dollar currency cent" 
	[ ! -z "`locale |grep lv_`" ] && keycode="keycode 13 = 4 dollar currency cent" 
	[ ! -z "`locale |grep en_US`" ] && keycode="keycode 14 = 5 percent currency cent"
	[ ! -z "`locale |grep pl_`" ] && keycode="keycode 0x1E = U U	currency cent"
	[ ! -z "`locale |grep hu_`" ] && keycode="keycode 30 = u U	currency cent"
# Not known:
	[ ! -z "`locale |grep gr_`" -o ! -z "`locale |grep hu_`" -o \
	! -z "`locale |grep az_`" -o ! -z "`locale |grep lt_`"  ] && {
		gettext "euro-test cannot determine the mapping for your language"; $ECHO
		exit 1
	}

	if [ -x "/usr/X11R6/bin/xdpyinfo" ]
	then
		XFREE=`/usr/X11R6/bin/xdpyinfo | grep "^XFree86 version:"|cut -d " " -f 3`
	else
		XFREE=`dpkg -l "xfree86-common" |grep ^ii |cut -d " " -f 4`
	fi
# or
#	XFREE=`dpkg --status xfree86-common |grep Version: |cut -d : -f 2`
	case $XFREE in
		3* )
		xmodmap -e "$keycode"
		;;
		4* )
		keycode=`$ECHO $keycode | sed -e s/currency/EuroSign/`
		xmodmap -e "$keycode"
		;;
		*)
		gettext "Cannot determine Xfree version properly"; $ECHO
		exit 1
		;;
	esac
}

test_euro_console () {
	gettext "Trying text environment (console)"; $ECHO
	test_euro_font || set_font=1
	[ $set_font = 1 ] && { 
		set_console_euro_font 
		test_euro_font 
	}
	test_euro_key ||  set_key=1
	[ $set_key = 1 ] &&  { 
		set_console_euro_key
		test_euro_key
	}
# TODO: Is this necessary? (restore the console font)
	[ $set_font = 1 ] && {
# Might need superuser priviledges
		gettext "Restoring the console font (if it does not work run the commands as root):"; $ECHO
		gettext "Running /etc/init.d/keymaps-lct.sh reload..."; $ECHO
		/etc/init.d/keymaps-lct.sh reload
		gettext "...done."; $ECHO
		gettext "Running /etc/init.d/console-screen.sh reload..."; $ECHO
		/etc/init.d/console-screen.sh reload
		gettext "...done."; $ECHO
	}
	if [ ! -z "$eurofont" -a $eurokey = 1 -a $set_font = 0 -a $set_key = 0 ] 
	then
		gettext "Congratulations! Your system is ready to display the euro char (in console)"; $ECHO
	else
		gettext "In order to display the euro char on your system you will need to:"; echo
		[ $set_font = 1  -a -z "$eurofont" ] && gettext "* Install a console font with the euro char."; $ECHO
		[ $set_font = 1 -a ! -z "$eurofont" ] && gettext "* Set as your console font one with euro support."; $ECHO
		[ $set_key = 1 -a $eurokey = 1 ] && gettext "* Configure your keyboard so that it generates the euro symbol."; $ECHO
		[ $set_key = 1 -a $eurokey = 0 ] && gettext "* Install support to your keyboard to generate the euro combination."; $ECHO
		gettext "Please read the \"Debian Euro Manual\" to see how to properly configure this"; $ECHO
	fi
}

test_euro_X () {
	gettext "Trying graphic environment (X)"; $ECHO
	test_euro_font || set_font=1
	[ $set_font = 1 ] && { 
		set_X_euro_font 
		test_euro_font 
	}
	test_euro_key ||  set_key=1
	[ $set_key = 1 ] &&  { 
		set_X_euro_key
		test_euro_key
	}
	if [ ! -z "$eurofont" -a $eurokey = 1 -a $set_font = 0 -a $set_key = 0 ] 
	then
 		gettext "Congratulations! Your system is ready to display the euro char (in X)"; $ECHO
	else
		gettext "In order to display the euro char on your X system you will need to:"; $ECHO
		[ $set_font = 1  -a -z "$eurofont" ] && gettext "* Install an X font with the euro char."; $ECHO
		[ $set_font = 1 -a ! -z "$eurofont" ] && gettext "* Set as your terminal/programs font one with euro support."; $ECHO
		[ $set_key = 1 -a $eurokey = 1 ] && gettext "* Configure your keyboard in X so that it generates the euro symbol."; $ECHO
		[ $set_key = 1 -a $eurokey = 0 ] && gettext "* Install support to your keyboard in X to generate the euro combination."; $ECHO
		gettext "Please read the \"Debian Euro Manual\" to see how to properly configure this."
	fi
# TODO: Add info on X packages with euro keys
	if [ "$ARG" = "X" ] ; then
		gettext "Press <ENTER> to close this terminal"; $ECHO
		read a
	fi
}

info_system () {

	gettext "System information, please send to euro-support@packages.debian.org"; $ECHO
	if [ -f /etc/debian_version ]
		then
		version=`cat /etc/debian_version`
		gettext "Debian GNU/Linux Version: $version"; $ECHO
	fi
	case $1 in
	"console" )
		gettext "The test is running under console."; $ECHO
		gettext "Information of console-related packages, can take some seconds:"; $ECHO
		dpkg -l kbd |grep ^i 
		dpkg -l console-tools |grep ^i 
		dpkg -l console-data |grep ^i 
		dpkg -l fonty |grep ^i 
		if [ -f /etc/console-tools/config ] ; then
			$ECHO "------- /etc/console-tools/config ------------"
			cat /etc/console-tools/config
			$ECHO "------- /etc/console-tools/config ------------"
		fi
		if [ -f /etc/kbd/config ] ; then
			$ECHO "------- /etc/kbd/config ------------"
			cat /etc/kbd/config
			$ECHO "------- /etc/kbd/config ------------"
		fi
		if [ -x /usr/bin/showcfont ]; then
			$ECHO "Output of /usr/bin/showcfont:"
			$ECHO "------- showcfont ------------"
			/usr/bin/showcfont
			$ECHO "------- showcfont ------------"
		fi 
		;;
	"X" )
		gettext "The test is running under X graphic environment."; $ECHO
		gettext "Information of X-related packages, can take some seconds:"; $ECHO
		dpkg -l xfree* |grep ^i
		dpkg -l xbase* |grep ^i
		dpkg -l xfonts* |grep ^i
		for config in /etc/X11/XF86Config-4 /etc/X11/XF86Config
		do
			if [ -f $config ] ; then
			$ECHO "------- $config ------------"
			grep -2 -i xkb $config
			$ECHO "------- $config ------------"
		fi
		if [ -x /usr/X11R6/bin/xmodmap ]; then
			$ECHO "Output of /usr/X11R6/bin/xmodmap:"
			$ECHO "------- showcfont ------------"
			/usr/X11R6/bin/xmodmap -pke
			$ECHO "------- showcfont ------------"
		fi 
		done
	;;
	esac
}

# Program starts here
export TEXTDOMAIN="euro-support"
ARG=$1

OUTFILE="./euro-test.log"
set_font=0
set_key=0
eurokey=0
eurofont=""
# This file indicates we are running in a Debian GNU/Linux system
DPKG=/usr/bin/dpkg

if [ ! -f $DPKG ]
then	
	gettext "WARN: This system is not a Debian GNU/Linux system"; $ECHO
	gettext "WARN: This program might not work correctly"; $ECHO
fi

if [ -z "$DISPLAY" ] 
then
	test_euro_console
else
	test_euro_X
fi

gettext "\
NOTE: More information regarding euro support is available at
/usr/share/doc/euro-support/ or through the doc-base/dwww/doc-central
interface"; $ECHO

if [ -f $DPKG ]
then
	gettext "Do you wish to generate a file with the test results (y/N)? "
	read sino
	case "$sino" in
	s*|S*|y*|Y*)	
		gettext "The result generation can take some seconds."; $ECHO
		gettext "Starting result generation..."
		date=`date`
		$ECHO "Results generated on $date" >>$OUTFILE
		if [ ! -z "$eurofont" -a $eurokey = 1 -a $set_font = 0 -a $set_key = 0 ] 
		then
			$ECHO "RES: The system is configured for euro support" >>$OUTFILE
		else
			$ECHO "RES: The system is not properly configured" >>$OUTFILE
			[ $set_font = 1  -a -z "$eurofont" ] && $ECHO -e "\tFont not installed" >>$OUTFILE
			[ ! -z "$eurofont" ] && $ECHO -e "\tUsing font $eurofont " >>$OUTFILE
			[ $set_font = 1 -a ! -z "$eurofont" ] && $ECHO -e "\tFont not configured" >>$OUTFILE
			[ $set_key = 1 -a $eurokey = 1 ] && $ECHO -e "\tKeyboard not configured properly" >>$OUTFILE
			[ $set_key = 1 -a $eurokey = 0 ] && $ECHO -e "\tThere is no keyboard support" >>$OUTFILE
			[ $set_key = 0 -a $eurokey = 1 ] && $ECHO -e "\tKeyboard properly configured" >>$OUTFILE
		fi
		if [ -z "$DISPLAY" ] 
		then
			info_system console >>$OUTFILE
		else
			info_system X >>$OUTFILE
		fi
		gettext "...done."; $ECHO
		gettext "\
Please send the output file $OUTFILE to the e-mail address:
  euro-support@packages.debian.org
Your system information will be used in order to provide an automatic mechanism
for euro support."; $ECHO
		;;
	*) 	;;
	esac
fi
