#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL

bterm=/usr/bin/bterm
uni_font=/opt/drbl/lib/unifont.bgf
kbdconf="dpkg-reconfigure console-data"

# We need to know ocsroot and functions in ocs-functions
. /opt/drbl/sbin/drbl-conf-functions
. /opt/drbl/sbin/ocs-functions
# load keymap if it exists
. /etc/ocs/ocs-live.conf

export LANG="en_US.UTF-8"
# generate locale file. Here we only need en_US.UTF-8
if [ -z "$(localedef --list-archive | grep -iw "en_US.utf8")" ]; then
  localedef -f UTF-8 -i en_US en_US.UTF-8
fi

echo "Configuring keyboard..."
# ocs_live_keymap is loaded from ocs-live.conf
if [ -z "$ocs_live_keymap" ]; then
  # keymap not set in ocs-live.conf, use interactive dialog to set it.
  if [ -x "$bterm" -a -e "$uni_font" ] && ([ -e /dev/fb/0 ] || [ -e /dev/fb0 ]); then 
    # If bterm and uni fonts are available, we will use bterm so that Asian fonts can be shown
    export TERM=bterm
    set +e
    bterm -l $LANG -f $uni_font /opt/drbl/sbin/ocs-kbdconf-bterm
    set -e
  else
    $kbdconf
  fi
else
  if [ -e "$ocs_live_keymap" \
       -o "$ocs_live_keymap" = "NONE" \
       -o "$ocs_live_keymap" = "KERNEL" ]; then
    install-keymap $ocs_live_keymap
  else
    echo "File $ocs_live_keymap NOT found!"
    echo "Press Enter to exit..."
    read
    exit 1
  fi
fi
