#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# This program is used in Debian.

# Settings
bterm=/usr/bin/bterm
uni_font=/opt/drbl/lib/unifont.bgf
kbdconf=/usr/bin/kbd-config

#
[ ! -e "$bterm" ] && exit 1
[ ! -e "$uni_font" ] && exit 1

# We need to know ocsroot and functions in ocs-functions
. /opt/drbl/sbin/drbl-conf-functions
. /opt/drbl/sbin/ocs-functions

ask_language_if_supported_with_bterm
[ -z "$ocs_lang" ] && ocs_lang=en

# put the language setting so that later program can reuse
if [ -z "$(grep -Ew "^[[:space:]]*ocs_lang=" /etc/ocs/ocs-live.conf)" ]; then
  # append it
  echo "ocs_lang=$ocs_lang" >> /etc/ocs/ocs-live.conf
else
  # modify it
  perl -pi -e "s|^[[:space:]]*ocs_lang=.*|ocs_lang=$ocs_lang|g" /etc/ocs/ocs-live.conf
fi
# 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

case "$ocs_lang" in
  tw.UTF-8)
     export LANG="zh_TW.UTF-8"
     export TERM=bterm
     set +e
     bterm -l $LANG -f $uni_font $kbdconf
     set -e
     ;;
  *)
     export LANG="en_US.UTF-8"
     $kbdconf
     ;;
esac
