#!/bin/sh
#

if [ ! "$UID" = "0" ]; then
echo
echo "[$LOGNAME] You need to run this script as root."
echo "Try one of these to run it as root: # sudo kmapset"
echo "                                    # su -c kmapset"
echo
exit 
fi

if ! type -all dialog >/dev/null 2>&1 ; then
echo "Can't find 'dialog', i can't run without 'dialog' on your system."
exit 
fi

if [ ! $DISPLAY = "" ]; then
DCMD="Xdialog --wrap" 
CMD="rxvt -e "
else
DCMD="dialog"
CMD=""
fi


TMP=/tmp

if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi

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

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

if [ -x /usr/bin/loadkeys ]; then
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
}

$DCMD --title "CHANGE KEYBOARD MAP?" --yesno "You should only run this \
script if you want to change the\nkeymap, the new keymap will be \
added to the /etc/rc.d/\nrc.keymap file and loaded at boot time. \n
Continue?" 9 65

if [ $? = 1 ]; then
echo
exit
fi

while [ 0 ]; do
$DCMD --title "KEYBOARD MAP SELECTION" --menu "You may select one \
of the following keyboard maps.\n\
Use the UP/DOWN arrow keys to scroll." \
17 55 9 \
"ANSI-dvorak" "" \
"azerty" "" \
"be-latin1" "" \
"be2-latin1" "" \
"bg" "Bulgarian" \
"bg-cp1251" "" \
"br-abnt" "" \
"br-abnt2" "" \
"br-latin1-abnt2" "" \
"br-latin1-us" "" \
"by" "" \
"cf" "French Canadian" \
"croat" "" \
"cz-cp1250" "" \
"cz-lat2-prog" "" \
"cz-lat2" "Czech" \
"cz-us-qwertz" "" \
"de-latin1-nodeadkeys" "German" \
"de-latin1" "" \
"de" "" \
"defkeymap" "" \
"defkeymap_V1.0" "" \
"dk-latin1" "Danish" \
"dk" "" \
"dvorak-l" "Left-handed" \
"dvorak-r" "Right-handed" \
"dvorak" "" \
"emacs" "" \
"emacs2" "" \
"es" "Spanish" \
"es-cp850" "" \
"et-nodeakeys" "" \
"et" "" \
"fi-latin1" "Finnish" \
"fi-latin9" "" \
"fi" "" \
"fr_CH-latin1" "French" \
"fr_CH" "" \
"fr-latin0" "" \
"fr-latin1" "" \
"fr-pc" "" \
"fr" "" \
"gr-pc" "Greek" \
"gr" "" \
"hebrew" "" \
"hu101" "Hungarian" \
"hu" "" \
"hypermap.m4" "for the adventurous soul..." \
"il-heb" "hebrew?..." \
"il-phonetic" "" \
"is-latin1" "Icelandic" \
"it-ibm" "Italian" \
"it" "" \
"it2" "" \
"jp106" "Japanese" \
"la-latin1" "" \
"lt-baltic" "" \
"lt" "Lithuanian" \
"lt.l4" "" \
"mk" "Macedonian" \
"mk-cp1251" "" \
"mk-utf" "" \
"mk0" "" \
"nl" "IBM 85 key" \
"nl2" "IBM 102 key Eurosign no-deadkeys" \
"no-latin1" "Norwegian" \
"no" "" \
"pc110" "" \
"pc-dvorak-latin1" "" \
"pl" "Polish" \
"pl2" "" \
"pt-latin1" "" \
"pt-latin9" "" \
"pt-old" "" \
"ro" "" \
"ru-cp1251" "Russian" \
"ru-ms" "" \
"ru-yawerty" "" \
"ru" "" \
"ru1" "" \
"ru2" "" \
"ru3" "" \
"ru4" "" \
"ru_win" "" \
"se-latin1" "Swedish" \
"se-fi-ir209" "" \
"se-fi-lat6" "" \
"se-ir209" "" \
"se-lat6" "" \
"sf-latin1" "" \
"sf" "" \
"sg-latin1-lk450" "" \
"sg-latin1" "" \
"sg" "" \
"sk-prog-qwerty" "Slovakian" \
"sk-prog-qwertz" "" \
"sk-prog" "" \
"sk-qwerty" "" \
"sk-qwertz" "" \
"slovene" "" \
"speakupmap" "keymap for the JFW screenreader" \
"sr-cy" "" \
"sv-latin1" "" \
"tralt" "Turkish" \
"tr_f-latin5" "" \
"trf" "" \
"tr_q-latin5" "" \
"tralt" "" \
"trq" "" \
"ua-utf-ws" "Ukrainian" \
"ua-utf" "" \
"ua-ws" "" \
"ua" "" \
"uk" "United-Kingdom" \
"unicode" "" \
"us-acentos" "US-hispanic? beats me..." \
"us" "USA" \
"wangbe" "Belgium" \
"wangbe2" "" \
"windowkeys" "" \
 2> $TMP/SeTkeymap

 if [ $? -gt 0 ]; then
  rm -f $TMP/SeTkeymap
  echo
  exit
 fi

MAPNAME="`cat $TMP/SeTkeymap`"
loadkeys $MAPNAME 1>/dev/null 2>$TMP/error || ERROR=YES 

 if [ "$ERROR" = "YES" ]; then
 echo "There was a error loading the $MAPNAME keymap." > $TMP/error.msg
 echo "You can chose another keymap or cancel." >> $TMP/error.msg
 echo >> $TMP/error.msg
 $DCMD --title "LOADKEYS ERROR!" --msgbox "`cat $TMP/error.msg $TMP/error`" 10 62

   unset ERROR
   rm -f $TMP/SeTkeymap
   rm -f $TMP/error*
   continue;
fi
 
 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> $TMP/keytest
 
  if [ $? = 1 ]; then
   rm -f $TMP/keytest
   rm -f $TMP/SeTkeymap
   rm -f $TMP/error*
   break;
  fi
 
  REPLY="`cat $TMP/keytest`"

  if [ "$REPLY" = "1" ]; then
   RC > /etc/rc.d/rc.keymap
   echo "XKEYBOARD=\"$MAPNAME\"" >> /etc/sysconfig/vector
   chmod 755 /etc/rc.d/rc.keymap
   rm -f $TMP/keytest
   rm -f $TMP/SeTkeymap
   rm -f $TMP/error*    
   echo
   exit 
   else
   rm -f $TMP/keytest
   rm -f $TMP/SeTkeymap
   rm -f $TMP/error*
   break;
   fi
 done
done
