#!/bin/sh
# Copyright 1995, Patrick Volkerding, Moorhead, MN.
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
if [ ! -d /usr/lib/kbd/keytables ]; then
  dialog --title "Required files are missing" --msgbox "You cannot remap your \
keyboard until you install the package 'keytbls.tgz' from the A series of \
packages.  It contains the keyboard map files required to proceed." 9 40
  exit
fi
cat << EOF > /tmp/keyscript
dialog --title "KEYBOARD MAP SELECTION" --menu "
You may select one of the following keyboard maps. 
You may use the UP/DOWN arrow keys to scroll through
the whole list of choices.
" 20 68 9 \\
EOF
for MAP in /usr/lib/kbd/keytables/*.map ; do
  SHORTMAP=`basename $MAP .map`
  echo "\"$SHORTMAP\" \"\" \\" >> /tmp/keyscript
done
echo "2> /tmp/keymp" >> /tmp/keyscript
while [ 0 ]; do
  . /tmp/keyscript
  if [ ! $? = 0 ]; then rm -f /tmp/keymp /tmp/keyscript; exit 1; fi
  MAPNAME="`cat /tmp/keymp`"
  rm -f /tmp/keymp
  loadkeys $MAPNAME 1> /dev/null 2> /dev/null
  while [ 0 ]; do
    clear
    cat << EOF

TEST YOUR NEW KEYBOARD MAP ($MAPNAME)

Please test your new keyboard map by typing in some characters.  If you're
satisfied with the new map and would like it loaded at boot time, enter 'y'
on a line by itself.  If you don't like the new map and want to choose a
different one, then enter 'n' on a line by itself.

EOF
    for index in 0 1 2 3 4 5 6 7 8 9 ; do
      read JUNK;
      if [ "$JUNK" = "y" -o "$JUNK" = "n" ]; then break ; fi
    done
    if [ "$JUNK" = "n" ]; then
      loadkeys us 1> /dev/null 2> /dev/null 
      break;
    elif [ "$JUNK" = "y" ]; then
      echo "loadkeys $MAPNAME" > /etc/rc.d/rc.keymap
      chmod 754 /etc/rc.d/rc.keymap
      rm -f /tmp/keyscript
      dialog --title "KEYBOARD MAP INSTALLED"  --msgbox "The '$MAPNAME' \
keyboard map will now be loaded automatically at boot time by your \
/etc/rc.d/rc.keymap script." 8 50
      exit 0;
    fi 
  done
done
