#!/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 35 65
# 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

WITHOUT_RC_COMPAT=1

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

KEYBOARD=/etc/sysconfig/keyboard
[ -s "$KEYBOARD" ] || exit

type -t loadkeys >/dev/null || exit

LOCKFILE=/var/lock/subsys/keytable

case "$1" in
    condrestart|condreload)
      # Nothing to do on condrestart because otherwise we could 
      # damage a custom keytable loaded by the admin.
      :
      ;;
	start|restart|reload)
		# Read the keyboard config
		. "$KEYBOARD"
		if [ "${KEYTABLE:-bogus}" != "bogus" ]; then
			# load the keyboard map
			action "Loading keymap: $KEYTABLE" \
			loadkeys "$KEYTABLE"
		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="${KEYTABLE#*-}"
			KBCHARSET="${KBCHARSET%%.*}"
		fi
		
		KBCHARSET="$(echo "$KBCHARSET" | tr '[[:upper:]]' '[[:lower:]]')"
	    	# 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;;
		    koi*|cp1251|iso-8859-5) KBCHARSET=cyrillic;;
			*) KBCHARSET=latin;;
		esac
        if [[ "$KBCHARSET" != "cyrillic" ]]; then
		  FILE="compose.$KBCHARSET.inc"
		  action "Loading compose keys: $FILE" \
		    loadkeys "$FILE"
	      FILE="windowkeys-compose.inc"
        else
          FILE="windowkeys-console_switch.inc"
        fi

        if [[ "$PC105" != "off" ]]; then
	      action "Binding extra Win keys, loading: $FILE" \
	        loadkeys "$FILE"
        fi

		if [ "$BACKSPACE" = "BackSpace" ]; then
			KEY="^H"
			FILE="backspace.inc"
		else
			KEY="^?"
			FILE="delete.inc"
		fi
		action "The BackSpace key sends: $KEY loading $FILE" \
		  loadkeys "$FILE"

		if [ -n "$GRP_TOGGLE" ]; then
			action "Loading toggle definition: $GRP_TOGGLE.inc" \
			loadkeys ${GRP_TOGGLE}.inc
		fi

		touch "$LOCKFILE"
		;;
	stop|condstop)
        # Nothing to do when stoping (perhaps copy some files into /etc/sysconfig/console/?).
		rm -f "$LOCKFILE"
		;;
	status)
		if [ -f "$LOCKFILE" ]; then
			echo "This service was last time (re-)started at $(LANG=C LANGUAGE=C /bin/ls -l "$LOCKFILE" --full-time |tr -s ' ' |cut --fields 6-10 -d' ')."
			echo "No other status information available for this package."
			echo "All this doesn't mean that there have not been perfomed any other (not init-forced) keytable changes since the given time."
		else
			echo "This service hasn't been started since stopped last time."
			echo "This does mean nothing at all (stopping the service doesn't unload the keytable)."
		fi
		exit 0
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|reload|status|condrestart|condreload|condstop}"
		exit 1
esac

exit 0
