#!/bin/sh -e

PROG="${0##*/}" #program name

ckeys_file=/usr/share/predator/consolekeys
keyboard_config=/etc/sysconfig/keyboard
kbd_config=/etc/sysconfig/kbd

. /etc/sysconfig/i18n
[ -s "$kbd_config" ] && . "$kbd_config"

print_version()
{
	cat <<EOF
$PROG version 0.0.1

Written by Stanislav Ievlev

Copyright (C) 2004 ALT Linux Team
EOF
	exit
}

print_usage()
{
	[ "$1" = 0 ] || exec >&2
	cat <<EOF
Usage: $PROG [options]

utility to setup console keyboard layout according current global system settings

Valid options are:
  -h, --help	display help screen
  -v, --version	display version information

Report bugs to <inger@altlinux.org>
EOF
	[ -n "$1" ] && exit "$1" || exit
}


TEMP=`getopt -n $PROG -o h,v -l help,version -- "$@"` || print_usage
eval set -- "$TEMP"

while :; do
	case "$1" in
		-h|--help) print_usage 0
			;;
		-v|--version) print_version
			;;
		--) shift; break
			;;
		*) Fatal "unrecognized option: $1"
			;;
	esac
	shift
done

charmap=$(LC_ALL=$LANG locale charmap)
layout=${LAYOUT##.=}

layout=$(cat $ckeys_file|egrep "^$layout[[:space:]]"|cut -f2)

#hack for ugly kbd layouts: we must fix it in the future console-data versions
[ -f /usr/lib/kbd/keymaps/*/*/$layout-$charmap.* ] && layout=$layout-$charmap

echo "KEYTABLE=$layout">$keyboard_config
echo "KBCHARSET=$charmap">>$keyboard_config

