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

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

# Prepare drbl/clonezilla runtime programs and dir
tar -xzf /live_media/pkg/opt_drbl.tgz -C /
ln -fs /live_media/etc/ocs /etc/
mkdir -p /opt/lib/; ln -fs /live_media/pkg/unifont.bgf /opt/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 [ -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 LANG="zh_TW.UTF-8"
  export TERM=bterm
  set +e
  bterm -l $LANG -f $uni_font $ocs_live_run
  EXIT=$?
  set -e
  case "$ocs_live_run" in
    *ocs-live-save-for-rw|*ocs-live-restore)
    # Since ocs-sr is run in ocs-live-save-for-rw or ocs-live-restore, we should ask the next step
    # For ocs-live-save-for-rw
    ask_and_load_lang_set en
    run_post_cmd_when_clone_end choose
  esac
else
  export LANG=C.UTF-8
  $ocs_live_run
  case "$ocs_live_run" in
    *ocs-live-save-for-rw|*ocs-live-restore)
    # Since ocs-sr is run in ocs-live-save-for-rw or ocs-live-restore, we should ask the next step
    # For ocs-live-save-for-rw
    ask_and_load_lang_set en
    run_post_cmd_when_clone_end choose
  esac
fi
