#!/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
# generate locale files.
[ -z "$(localedef --list-archive | grep -iw "zh_TW.utf8")" ] && localedef -f UTF-8 -i zh_TW zh_TW.UTF-8
[ -z "$(localedef --list-archive | grep -iw "en_US.utf8")" ] && localedef -f UTF-8 -i en_US en_US.UTF-8

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 
    export LANG="zh_TW.UTF-8"
    export TERM=bterm
    set +e
    bterm -l $LANG -f $uni_font /opt/drbl/sbin/kbdconf-bterm
    set -e
  else
    export LANG="en_US.UTF-8"
    $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

# Set locales for later use.
case "$ocs_lang" in
  tw.UTF-8) export LANG="zh_TW.UTF-8" ;;
  *)        export LANG="en_US.UTF-8" ;;
esac
