#!/bin/bash
# Enables Unicode processing in the current console.
#

if [ ! -e /proc/self/fd/0 ] ; then
   echo "Won't set unicode mode: Can't determine console type;" >&2
   echo " Please ensure that /proc is mounted." >&2
   exit 1
fi

readlink /proc/self/fd/0 | grep -q -e /dev/vc -e '/dev/tty[^p]' -e /dev/console
if [ $? -eq 1 ]; then
    echo  "Won't set unicode mode: not a VT." >&2
    exit 1
fi

# 1. The input side: the keyboard driver.

# Set the keyboard driver in Unicode mode. (Default is ASCII mode.)
# This really does nothing with the way normal keys are handled in
# the kernel. All it does is:
# - It is necessary for `dumpkeys' in order to not drop U+XXXX
#   entries from the keymaps.
# - It is necessary for `loadkeys' in order to avoid warnings.
# - Unicode characters typed as Alt-x1 ... Alt-xn (where x1,...,xn
#   are digits on the numeric keypad) will be emitted in UTF-8.

kbd_mode -u

# Change the keyboard mapping in such a way that the non-ASCII keys
# produce UTF-8 encoded multibyte sequences, instead of single bytes
# >= 0x80 in a legacy 8-bit encoding.

dumpkeys | loadkeys --unicode

# 2. The output side: the console screen.

# Tell the console output driver that the bytes arriving are UTF-8
# encoded multibyte sequences.

if test -t 1 -a -t 2 ; then
	echo -n -e '\033%G'
fi

