#!/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()
{
   variant=$(find /usr/share/kbd/keymaps/i386/ -name $MAPNAME.map.gz|cut -d'/' -f7)
   echo $variant
   ## Change KEYBOARD_MAP
   sed -i s"!	Option \"XkbLayout\".*!	Option \"XkbLayout\" \"$MAPNAME\"!" $1
   sed -i s"!qwerty!$variant!" $1
}


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)" \
"us" "USA" \
"ad" "Andorra" \
"af" "Afghanistan" \
"ara" "Arabic" \
"al" "Albania" \
"am" "Armenia" \
"az" "Azerbaijan" \
"by" "Belarus" \
"be" "Belgium" \
"bd" "Bangladesh" \
"in" "India" \
"ba" "Bosnia" \
"and" "Herzegovina" \
"br" "Brazil" \
"bg" "Bulgaria" \
"ma" "Morocco" \
"mm" "Myanmar" \
"ca" "Canada" \
"cd" "Congo, Democratic Republic of the" \
"cn" "China" \
"hr" "Croatia" \
"cz" "Czechia" \
"dk" "Denmark" \
"nl" "Netherlands" \
"bt" "Bhutan" \
"ee" "Estonia" \
"ir" "Iran" \
"iq" "Iraq" \
"fo" "Faroe Islands" \
"fi" "Finland" \
"fr" "France" \
"gh" "Ghana" \
"gn" "Guinea" \
"ge" "Georgia" \
"de" "Germany" \
"gr" "Greece" \
"hu" "Hungary" \
"is" "Iceland" \
"il" "Israel" \
"it" "Italy" \
"jp" "Japan" \
"kg" "Kyrgyzstan" \
"kh" "Cambodia" \
"kz" "Kazakhstan" \
"la" "Laos latam Latin American" \
"lt" "Lithuania" \
"lv" "Latvia" \
"mao" "Maori" \
"me" "Montenegro" \
"mk" "Macedonia" \
"mt" "Malta" \
"mn" "Mongolia" \
"no" "Norway" \
"pl" "Poland" \
"pt" "Portugal" \
"ro" "Romania" \
"ru" "Russia" \
"rs" "Serbia" \
"si" "Slovenia" \
"sk" "Slovakia" \
"es" "Spain" \
"se" "Sweden" \
"ch" "Switzerland" \
"sy" "Syria" \
"tj" "Tajikistan" \
"lk" "Sri Lanka" \
"th" "Thailand" \
"tr" "Turkey" \
"ua" "Ukraine" \
"gb" "United Kingdom" \
"uz" "Uzbekistan" \
"vn" "Vietnam" \
"kr" "Korea, Republic of" \
"nec_vndr/jp" "Japan (PC-98xx Series)" \
"ie" "Ireland" \
"pk" "Pakistan" \
"mv" "Maldives" \
"za" "South Africa" \
"epo" "Esperanto" \
"np" "Nepal" \
"ng" "Nigeria" \
"et" "Ethiopia" \
"sn" "Senegal" \
"brai" "Braille" \
"tm" "Turkmenistan" \
 2> $freply || exit $?

MAPNAME="`cat $freply`"

if [ "$MAPNAME" = "DEFAULT" ]; then
    infobox "Setting keymap to default" "KEYMAP" 30
    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
 
## 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
