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

bterm=/usr/bin/bterm
uni_font=/opt/drbl/lib/unifont.bgf

# Clean /etc/motd to avoid confusion.
# We need to know ocsroot.
. /opt/drbl/sbin/drbl-conf-functions
. /opt/drbl/sbin/ocs-functions

# load the ocs_live_run
. /etc/ocs/ocs-live.conf
if [ -z "$ocs_live_run" ]; then
  echo "You have to assing the ocs_live_run in /etc/ocs/ocs-live.conf!"
  echo "Program terminated!"
  exit 1
fi

if [ ! -d "/live_media" ]; then
  echo "$0 is run in Clonezilla Live!"
  echo "Program terminated!"
  exit 1
fi

# $ocs_lang is loaded from /etc/ocs/ocs-live.conf
if [ "$ocs_lang" = "tw.UTF-8" ] && \
   [ -x "$bterm" -a -e "$uni_font" ] && \
   ([ -e /dev/fb/0 ] || [ -e /dev/fb0 ]); then 
  if [ -z "$(localedef --list-archive | grep -iw "zh_TW.utf8")" ]; then
    localedef -f UTF-8 -i zh_TW zh_TW.UTF-8
  fi
  # export these variables so that they can be passed to $ocs_live_run in bterm
  export LANG="zh_TW.UTF-8"
  export TERM=bterm
  export CURRENT_TTY="$(tty)"
  # if it's not in /dev/tty1, just exit.
  [ "$CURRENT_TTY" != "/dev/tty1" ] && exit 3
  set +e
  # Do not ask powerer/reboot/choose (-p) in $ocs_live_run (ocs-live-save/ocs-live-restore), just use "-p true" in $ocs_live_run (ocs-live-save/ocs-live-restore). Since it might be in bterm, and Debian live "Press Enter" message when poweroff/shutdown might be coverd by bterm and user will not have any idea what's happening after choose poweroff/reboot. We will ask it when bterm is done, i.e. in the end of this program.
  bterm -l $LANG -f $uni_font $ocs_live_run
  EXIT=$?
  set -e
else
  if [ -z "$(localedef --list-archive | grep -iw "en_US.utf8")" ]; then
    localedef -f UTF-8 -i en_US en_US.UTF-8
  fi
  # export these variables so that they can be passed to $ocs_live_run in bterm
  export LANG="en_US.UTF-8"
  export CURRENT_TTY="$(tty)"
  # if it's not in /dev/tty1, just exit.
  [ "$CURRENT_TTY" != "/dev/tty1" ] && exit 3
  # Do not ask powerer/reboot/choose (-p) in $ocs_live_run (ocs-live-save/ocs-live-restore), just use "-p true" in $ocs_live_run (ocs-live-save/ocs-live-restore). Since it might be in bterm, and Debian live "Press Enter" message when poweroff/shutdown might be coverd by bterm and user will not have any idea what's happening after choose poweroff/reboot. We will ask it when bterm is done, i.e. in the end of this program.
  $ocs_live_run
fi
#
# now it's not in bterm, only text console. Use English. 
ask_and_load_lang_set en
run_post_cmd_when_clone_end choose
