#!/bin/sh
#
# Load keytable
#
# This must be executed *after* /usr is mounted.
# This means is /usr is NFS-mounted, it needs to
# run after networking and NFS mounts are up.
#
# chkconfig: 2345 75 05
# description: This package loads the selected keyboard map as set in \
#   /etc/sysconfig/keyboard.  This can be selected using the kbdconfig \
#   utility.  You should leave this enabled for most machines.
# config: /etc/sysconfig/keyboard

[ -f /etc/sysconfig/keyboard ] || exit 0

[ -f /usr/bin/loadkeys ] || exit 0

# Source function library.
. /etc/rc.d/init.d/functions

case "$1" in
	start)
		# Read the keyboard config
		. /etc/sysconfig/keyboard
		if [ "${KEYTABLE:-bogus}" != "bogus" ]; then

			# load the keyboard map
			action "Lade Zeichentabelle: $KEYTABLE" \
			loadkeys $KEYTABLE < /dev/tty0 > /dev/tty0
		fi

		# search the charset used
		if [ -z "$KBCHARSET" -a -r "$KEYTABLE" ]; then
		   FILETOGREP="$KEYTABLE"
		else
		   FILETOGREP="/usr/lib/kbd/keymaps/i386/*/$KEYTABLE*map.gz"
		fi

		if [ -z "$KBCHARSET" -a -r "$FILETOGREP" ]; then
		   KBCHARSET=`zgrep ^charset $FILETOGREP 2> /dev/null |\
			 cut -d' ' -f2 | \
			 tr '\"' ' ' | tr -d ' '`
		fi

		if [ -z "$KBCHARSET" ]; then
		   KBCHARSET=`echo $KEYTABLE | cut -d'-' -f2 | cut -d'.' -f1`
		fi
		
	    	# load compose definitions
		case $KBCHARSET in
			iso-8859-1) KBCHARSET=latin1;;
			iso-8859-2) KBCHARSET=latin2;;
			iso-8859-3) KBCHARSET=latin3;;
			iso-8859-4) KBCHARSET=latin4;;
			iso-8859-7) KBCHARSET=8859_7;;
			iso-8859-8) KBCHARSET=8859_8;;
			iso-8859-9*) KBCHARSET=latin5;;
			iso-8859-13) KBCHARSET=latin7;;
			iso-8859-14) KBCHARSET=latin8;;
			iso-8859-15) KBCHARSET=latin9;;
			latin0) KBCHARSET=latin9;;
			tcvn*) KBCHARSET=tcvn;;
			viscii*) KBCHARSET=viscii;;
			*) KBCHARSET=latin;;
		esac
		action "Lade Compose-Zeichenstze: compose.$KBCHARSET.inc" \
		loadkeys compose.${KBCHARSET}.inc

		if [ "$BACKSPACE" = "BackSpace" ]; then
			echo -n "Die BackSpace-Taste sendet: ^H" 
			loadkeys backspace.inc 2> /dev/null \
			&& success || failure
		else
			echo -n "Die BackSpace-Taste sendet: ^?"
			loadkeys delete.inc 2> /dev/null \
			&& success || failure
		fi
		echo
		touch /var/lock/subsys/keytable
		;;
	stop)
		rm -f /var/lock/subsys/keytable
		;;
	restart|reload)
		$0 start
		;;
	status)
		echo "Status nicht verfgbar."
		exit 0
		;;
	*)
		echo "Benutzung: keytable {start|stop|restart|reload|status}"
		exit 1
esac

exit 0
