#! /bin/sh
# Copyright (c) 1995-2000 SuSE GmbH Nuernberg, Germany.
#
# Author:   Burchard Steinbild <bs@suse.de>
#           Werner Fink <werner@suse.de>
#
# /sbin/init.d/kbd
#
#   and symbolic its link
#
# /sbin/rckbd
#
### BEGIN INIT INFO
# Provides:       kbd
# Required-Start: $remote_fs
# Required-Stop:
# Default-Start:  1 2 3 5 S
# Default-Stop:
# Description:    Keyboard settings
### END INIT INFO

. /etc/rc.status
. /etc/rc.config

MACHINE=`/bin/uname -m 2> /dev/null`
if [ "$MACHINE" = "sparc" -o "$MACHINE" = "sparc64" ]; then 
   # Test if we have a serial console. 
   (test -c /dev/tty1 && > /dev/tty1 ) > /dev/null 2>&1 || exit 0
fi

test -n "$KEYTABLE" -a "$KEYTABLE" != YAST_ASK || {
    echo KEYTABLE is not set in /etc/rc.config.  Exit.
    exit 0
}
KEYTABLE=${KEYTABLE%.map*}
#
# first search the wanted keytable.
#
if [ $MACHINE = ppc ]; then
	test -f /proc/cpuinfo || mount -n -t proc proc /proc 2>/dev/null
	while read line; do
	       case "$line" in
		  *MacRISC*)	MACHINE="mac";;
		  *CHRP*)  	MACHINE="chrp";;
		  *PReP*)  	MACHINE="prep" ;;
		esac
	done < /proc/cpuinfo
fi

case "$MACHINE" in
  i?86|ia64|alpha|prep|chrp) KDBLIB="/usr/lib/kbd/keymaps/i386/*/"     ;;
  sparc*)    
     case "$KEYTABLE" in
        sun*) KDBLIB="/usr/lib/kbd/keymaps/sun/"        ;;
        *)    KDBLIB="/usr/lib/kbd/keymaps/i386/*/"     ;;
     esac
     ;;
  mac)       KDBLIB="/usr/lib/kbd/keymaps/mac/" ;;
  *)         echo "No available keymaps for machine $MACHINE found"; exit 1
	;;

esac

KEYMAP=""
if test -e /$KEYTABLE ; then
    KEYMAP=$KEYTABLE
else
    K="${KDBLIB}${KEYTABLE}"
    case "$KEYTABLE" in
	*.gz)  KDBPATH="${K}" ;;
	*.map) KDBPATH="${K} ${K}.gz" ;;
	*)     KDBPATH="${K} ${K}.gz ${K}.map.gz ${K}.map" ;;
    esac
    for K in ${KDBPATH} ; do	
	test -e "$K" || continue
	test -n "$KEYMAP" && KEYMAP="$KEYMAP $K" || KEYMAP=$K
    done
fi

rc_reset
case "$1" in
    start|restart|reload)
	if test -z "$KEYMAP" ; then
	    if test -r /etc/defkeymap.map ; then
		echo "Can not find a keymap for ${KEYTABLE}, trying fallback."
		KEYMAP=/etc/defkeymap.map
	    else
		echo "Can not find a keymap for ${KEYTABLE}, Exit."
		rc_failed
	    fi
	else
	    if test ! -e "$KEYMAP" ; then
		echo "Found more than one keymap ${KEYTABLE}"
		KEYMAP=$K
		echo "Using $KEYMAP."
		echo "If you want another one, please add complete path to"
		echo "KEYTABLE in /etc/rc.config."
	    fi
	fi
	rc_status && retmsg="`loadkeys $KEYMAP 2>&1`"
	if test $? -eq 0 && ( > /var/run/keymap ) > /dev/null 2>&1 ; then
	    echo "$KEYMAP"    > /var/run/keymap
	    if test ! -s /etc/defkeymap.map ; then
		( dumpkeys > /etc/defkeymap.map ) > /dev/null 2>&1
	    fi
	else
	    rc_failed
	fi
	echo "Loading keymap ${retmsg#Loading*/usr/lib/kbd/keymaps/*/}"
	rc_status -v1 -r

	# Load compose tables
	if test ! -z "$COMPOSETABLE"; then
		unset COMPOSELOADED COMPOSECLEAR
		for name in $COMPOSETABLE; do
			# Test for compose clear flag
			if test "$name" = "-c" -o "$name" = "clear"; then
				COMPOSECLEAR=-c
				continue
			fi
			# Handle the first table differently ...
			if test -z "$COMPOSELOADED"; then
				echo -n "Loading compose table $name"
				loadkeys $COMPOSECLEAR compose.$name >/dev/null 2>&1
				rc_check
				if test $name = clear; then rc_reset; fi
				COMPOSELOADED=1
			else
				echo -n " $name"
				loadkeys compose.$name >/dev/null 2>&1
				rc_check
			fi
		done
		if test ! -z "$COMPOSELOADED"; then 
			rc_status -v -r
		fi
	fi
	
	# setfont
	if test -d /usr/lib/kbd/consolefonts -a \
		-n "$CONSOLE_FONT" -a "$CONSOLE_FONT" != "YAST_ASK"
	then
	    if test -x /usr/bin/setfont ; then
		echo Loading console font $CONSOLE_FONT
		/usr/bin/setfont $CONSOLE_FONT
		rc_status -v1 -r
	    fi
	    # unimap
	    if test -n "$CONSOLE_UNICODEMAP" -a \
		    "$CONSOLE_UNICODEMAP" != "none" -a \
		    -x /usr/bin/setfont
	    then
		echo Loading unimap $CONSOLE_UNICODEMAP
		/usr/bin/setfont -u $CONSOLE_UNICODEMAP
		rc_status -v1 -r
	    fi
	    # screenmap
	    if test -n "$CONSOLE_SCREENMAP" -a \
		    "$CONSOLE_SCREENMAP" != "none" -a \
		    -x /usr/bin/setfont
	    then
		echo Loading screenmap $CONSOLE_SCREENMAP
		/usr/bin/setfont -m $CONSOLE_SCREENMAP
		rc_status -v1 -r
	    fi
	    # console magic init string
	    if test -n "$CONSOLE_MAGIC" -a "$CONSOLE_MAGIC" != "none" ; then
		echo Setting up console ttys
		for i in /dev/tty[1-24]; do
		    echo -en "\033$CONSOLE_MAGIC" > $i
		    rc_status
		done
		rc_status -v1 -r
	    fi
	fi

	;;
    stop)
	;;
    check|status)
	if test -r /var/run/keymap ; then
	    read current < /var/run/keymap
	    echo "Keymap $current is loaded."
	else
	    rc_failed
	fi
	;;
    *)
	echo "Usage: $0 {start|stop|restart|reload|status|check}"
	exit 1
	;;
esac
rc_exit