#!/bin/bash
# Written for the installer of Dragora, Copyright 2010 by Matas A. Fonzo.
# Under the terms of the GNU General Public License.

TMP=/tmp

clean_tmp() {
  ( cd $TMP ; rm -f keymap rc.keymap return-keymaps show_keymaps )
}

# Cabecera del men:
cat << EOF > ${TMP}/show_keymaps
dialog \\
 --backtitle "Keyboard configuration tool" \\
 --title "KEYBOARD MAP" \\
 --default-item "qwerty/us.bmap" \\
 --menu \\
"Select the keyboard map that you use:" 20 50 11 \\
EOF

# Escanea los mapas de teclado desde el tarball:
lzip -cd /etc/keymaps.tar.lz | tar tf - | grep map | sort | while read line ; do
  echo "\"${line}\" \"\" \\" >> ${TMP}/show_keymaps
done
echo " 2> \${TMP}/return-keymaps" >> ${TMP}/show_keymaps

. ${TMP}/show_keymaps
if (( $? != 0 )); then
  clean_tmp
  exit 1;
fi

KEYMAP=$(cut -f 1 -d ' ' ${TMP}/return-keymaps)

lzip -cd /etc/keymaps.tar.lz | tar xOf - $KEYMAP | loadkmap

KEYMAP=${KEYMAP%.bmap}.map.gz

# Prueba de mapa de teclado:
cat << EOF
                         TESTING FOR KEYBOARD

          Write or type something to test your keyboard map.
          If the keyboard map is fine, press '1' to save or
                press '2' to test another keyboard map.

EOF
read -rp ': '
case "$REPLY" in
  *2)
    clean_tmp
    "$0"
    ;;
  *)
    REPLY=1;
esac

if [[ $REPLY = 1 ]]; then
  # Generamos los archivos de configuracin:
  cat << EOF > ${TMP}/keymap
#
# keymap:  File configuration for the keymap.
#

KEYMAP=$KEYMAP

EOF
    cat << EOF > ${TMP}/rc.keymap
#!/bin/bash
#
# rc.keymap:  Load the keyboard map.
#
# Generated by keyconfig.

. /etc/sysconfig/keymap || exit 1

# Load the keymap:
if [[ -x /bin/loadkeys ]]; then
  /bin/loadkeys \$KEYMAP
fi

EOF
fi

rm -f ${TMP}/{return-keymaps,show_keymaps}

exit;

