#!/bin/sh
set -e
. /usr/share/debconf/confmodule
which () {
local IFS
IFS=:
for i in $PATH; do
if [ -x "$i/$1" ]; then
echo "$i/$1"
return 0
fi
done
return 1
}
if [ -f /usr/share/console-setup/keyboard-configuration.config ]; then
/usr/share/console-setup/keyboard-configuration.config || exit $?
fi
keyboard_present () {
local kern kbdpattern class subclass protocol
kern=`uname -r`
case "$kern" in
1*|2.0*|2.1*|2.2*|2.3*|2.4*|2.5*)
return 0; 
;;
esac
[ -d /sys/bus/usb/devices ] || return 0
for d in /sys/bus/usb/devices/*:*; do
[ -d "$d" ] || continue
class=$(cat "$d/bInterfaceClass") # 03 = Human Interface Device
subclass=$(cat "$d/bInterfaceSubClass") # 01 = Boot Interface Subclass
protocol=$(cat "$d/bInterfaceProtocol") # 01 = Keyboard
case "$class:$subclass:$protocol" in
03:01:01)
return 0
;;
esac
done
[ -f /proc/bus/input/devices ] || return 0
kbdpattern="AT Set \|AT Translated Set\|AT Raw Set"
kbdpattern="$kbdpattern\|Atari Keyboard"
kbdpattern="$kbdpattern\|Amiga Keyboard"
kbdpattern="$kbdpattern\|HIL keyboard"
kbdpattern="$kbdpattern\|ADB keyboard"
kbdpattern="$kbdpattern\|Sun Type"
kbdpattern="$kbdpattern\|bluetooth.*keyboard"
if grep -i "$kbdpattern" /proc/bus/input/devices >/dev/null; then
return 0
fi
return 1
}
CONFIGFILE=/etc/default/keyboard
if [ "$1" = "configure" -a ! -L "$CONFIGFILE" ]; then
db_get keyboard-configuration/modelcode
model="$RET"
db_get keyboard-configuration/layoutcode
layout="$RET"
db_get keyboard-configuration/variantcode
variant="$RET"
db_get keyboard-configuration/optionscode
options="$RET"
if [ ! -e $CONFIGFILE ]; then
cat /usr/share/console-setup/keyboard \
2>/dev/null >$CONFIGFILE || true
fi
cp -a -f $CONFIGFILE $CONFIGFILE.tmp
for var in XKBMODEL XKBLAYOUT XKBVARIANT XKBOPTIONS BACKSPACE; do
if ! grep "^ *${var}=" $CONFIGFILE >/dev/null; then
echo "${var}=" >>$CONFIGFILE
fi
done    
sed \
-e "s|^ *XKBMODEL=.*|XKBMODEL=\"$model\"|" \
-e "s|^ *XKBLAYOUT=.*|XKBLAYOUT=\"$layout\"|" \
-e "s|^ *XKBVARIANT=.*|XKBVARIANT=\"$variant\"|" \
-e "s|^ *XKBOPTIONS=.*|XKBOPTIONS=\"$options\"|" \
-e "s|^ *BACKSPACE=.*|BACKSPACE=\"${BACKSPACE:-guess}\"|" \
<$CONFIGFILE >$CONFIGFILE.tmp
mv -f $CONFIGFILE.tmp $CONFIGFILE
db_set keyboard-configuration/store_defaults_in_debconf_db true
fi
if type plymouth >/dev/null && plymouth --ping; then
if type setupcon >/dev/null 2>&1; then
setupcon --save-only
fi
elif keyboard_present; then
if [ "$DISPLAY" ] && which setxkbmap >/dev/null; then
setxkbmap -option '' -model "$model" "$layout" "$variant" "$options" || true
if type setupcon >/dev/null 2>&1; then
setupcon --save-only
cat >&2 <<EOF
Your console font configuration will be updated the next time your system
boots. If you want to update it now, run 'setupcon' from a virtual console.
EOF
fi    else
case "`uname`" in
GNU)
[ ! -f /var/run/hurd-console.pid ] || kill `cat /var/run/hurd-console.pid`
;;
*)
if type setupcon >/dev/null 2>&1; then
setupcon --force --save
fi
;;
esac
fi
fi
if ! [ -d /lib/debian-installer.d ]; then
dpkg-maintscript-helper rm_conffile \
/etc/init.d/keyboard-setup 1.138~ -- "$@"
dpkg-maintscript-helper rm_conffile \
/etc/init.d/console-setup 1.138~ -- "$@"
fi
if type update-initramfs >/dev/null 2>&1; then
update-initramfs -u
fi
exit 0
