#!/bin/sh
# Set keymap and xkeymap

usage() {
cat<< EOF
vkmapset - set keymap for console and x-window
usage: vkmapset [keymap_name]

EOF
}

vdir=`dirname $0`

. $vdir/vasm-functions

check_root

set_keymap() {
cat << EOF > /etc/rc.d/rc.keymap
#!/bin/sh
# /etc/rc.d/rc.keymap
# Made by vkmapset on `date '+%a %b %e %Y'`
# This file is called from: /etc/rc.d/rc.M


if [ -x /usr/bin/loadkeys ]; then

  # You may load another keyboard keymap e.g. 'loadkeys fr' = french etc.
  # The keymaps are in the /usr/lib/kbd/keymaps/i386 directory.
  loadkeys $MAPNAME

  # This is so you can open other consoles using the left-[Alt] & [Up]-arrow key.
  echo "alt keycode 103 = Spawn_Console" | /usr/bin/loadkeys
fi

EOF
chmod +x /etc/rc.d/rc.keymap
}

unset_keymap()
{
    chmod -x /etc/rc.d/rc.keymap
}

set_xorg()
{
   ## Change KEYBOARD_MAP
if [ "$MAPNAME" == "uk" ];then
   sed -i s"!	Option \"XkbLayout\".*!	Option \"XkbLayout\" \"gb\"!" $1
else
   sed -i s"!	Option \"XkbLayout\".*!	Option \"XkbLayout\" \"$MAPNAME\"!" $1
fi
}


set_xkeymap() {
    echo "XKEYBOARD=\"$MAPNAME\"" >> /etc/sysconfig/vlocity
	cat << EOF > /etc/X11/xorg.conf.d/90-keyboard-layout.conf
Section "InputClass"
        Identifier "keyboard-all"
        Driver "evdev"
	Option "XkbLayout" "us"
        Option "XkbModel" "pc104"
        Option "XkbRules" "xorg"
	Option "XkbOptions" "terminate:ctrl_alt_bksp"
        MatchIsKeyboard "on"
EndSection
EOF
        setxkbmap -model pc104 -layout $MAPNAME 
	set_xorg /etc/X11/xorg.conf.d/90-keyboard-layout.conf
	
}

menu_kmap() {
while [ 0 ]; do
$DCMD --backtitle "$BACKTITLE" --title "KEYBOARD MAP SELECTION" --menu "\n
This menu set keyboard mapping for the text console.\n
Please select one of the following keyboard maps:" \
20 60 11 \
"DEFAULT" "system default (US)" \
"bg" "Bulgarian" \
"by" "Belarus" \
"de" "German" \
"dk" "Danish" \
"dvorak" "Dvorak" \
"es" "Spanish" \
"et" "" \
"fi" "Finnish" \
"fr" "French" \
"gr" "Greek" \
"hu" "Hungarian" \
"it" "Italian" \
"lt" "Lithuanian" \
"mk" "Macedonian" \
"nl" "IBM 85 key" \
"no" "Norwegian" \
"pl" "Polish" \
"ro" "Romanian" \
"ru" "Russian" \
"ua" "Ukrainian" \
"uk" "United-Kingdom" \
"us" "USA" \
 2> $freply || exit $?

MAPNAME="`cat $freply`"

if [ "$MAPNAME" = "DEFAULT" ]; then
    infobox "Setting keymap to default" "KEYMAP"
    MAPNAME="us"
    unset_keymap
    set_xkeymap
    sleep 2
    clean_exit 0
fi

if ! loadkeys $MAPNAME 1>/dev/null 2>$fmenu; then
  $DCMD --backtitle "$BACKTITLE" --title "LOADKEYS ERROR" --msgbox \
  "`cat $fmenu`" 10 62 2>/dev/null
   continue;
fi
 
if [ "$MAPNAME" == "uk" ];then
	setxkbmap -model pc104 -layout gb 
else
	setxkbmap -model pc104 -layout $MAPNAME 
fi

## Testing ..

while [ 0 ]; do
  $DCMD --title "KEYBOARD TEST" --inputbox \
  "OK, the new map is now installed. You may now test it by typing \
  anything you want. To quit testing the keyboard, enter 1 on a \
  line by itself to accept the map and exit, or 2 on a line by \
  itself to reject the current keyboard map and select a new one." \
  11 70 2> $freply
  
  if [ $? = 1 ]; then
    break;
  fi
 
  REPLY="`cat $freply`"

  if [ "$REPLY" = "1" ]; then
    infobox "Setting keymap to $MAPNAME" "KEYMAP"
    set_keymap
    set_xkeymap
    clean_exit 0
  else
    break;
  fi
 done
done

}


#################################################################
# Main program

case $1 in
    -h|--help)	usage
    ;;
    "") menu_kmap
    ;;
    *)
    MAPNAME=$1
    if [ -f /usr/share/kbd/keymaps/i386//qwerty/$MAPNAME.map.gz ] || \
	[ -f /usr/share/kbd/keymaps/i386/dvorak/$MAPNAME.map.gz ] || \
    	[ -f /usr/share/kbd/keymaps/i386/azerty/$MAPNAME.map.gz ]; then
	infobox "Setting up keymap $MAPNAME" "KEYMAP"
	set_keymap
	set_xkeymap
	sleep 2
    else
	menu_kmap
    fi
    ;;
esac

clean_exit 0
