#!/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
. /opt/drbl/conf/drbl-ocs.conf

# Load the options from config file
[ -e /etc/ocs/ocs-live.conf ] && . /etc/ocs/ocs-live.conf

# Get options from kernel parameter if available.
# A normal bootparam in /proc/cmdline for clonezilla live is like:
# initrd=initrd-pxe.img devfs=nomount drblthincli=off selinux=0 ip=frommedia ocs_live_run="ocs-live-general"
param_2_be_parsed="ocs_live_run ocs_live_extra_param ocs_live_keymap ocs_live_batch ocs_lang"
for i in $param_2_be_parsed; do
  eval $i="$(grep $i /proc/cmdline | sed -e "s/.*$i=\([^ ]\+\).*$/\1/")"
done

# Set initial value if not set
[ -z "$ocs_live_run" ] && ocs_live_run="ocs-live-general"
[ -z "$ocs_live_keymap" ] && ocs_live_keymap="NONE"
[ -z "$ocs_live_batch" ] && ocs_live_batch="no"
[ -z "$ocs_lang" ] && ocs_lang="en_US.UTF-8"

#
if [ -z "$ocs_live_run" ]; then
  echo "No \$ocs_live_run was assigned (Either from /etc/ocs/ocs-live.conf or command kernel parameters). Skip Clonezilla-related action."
  exit 3
fi

# Get the live media mount point.
get_live_media_mnt_point

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

use_bterm=""
# Ex. zh_TW.UTF-8 -> zh_TW
locale_region="$(echo "$ocs_lang" | sed -e "s|\..*||g")"
if `locale_required_bterm_or_not "$locale_region"` && \
   [ -x "$bterm" -a -e "$uni_font" ] && \
   ([ -e /dev/fb/0 ] || [ -e /dev/fb0 ]); then 
   use_bterm="yes"
else
   use_bterm="no"
fi

# Generate locale files.
# The outputs for localedef --list-archive are like:
# en_US.utf8
# zh_TW.utf8
# While we use en_US.UTF-8 or zh_TW.UTF-8 as the locale.
if [ -z "$(localedef --list-archive | sed -e "s|\.utf8|.UTF-8|g" | grep -iw "$ocs_lang")" ]; then
  echo -n "Generating locale $ocs_lang by: localedef -f UTF-8 -i $locale_region $ocs_lang... "
  localedef -f UTF-8 -i $locale_region $ocs_lang
  echo "done!"
fi

# export these variables so that they can be passed to $ocs_live_run in bterm
export LANG="$ocs_lang"
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.
if [ "$use_bterm" = "yes" ];then
  export TERM=bterm
  set +e
  bterm -l $LANG -f $uni_font `which $ocs_live_run`
  EXIT=$?
  set -e
else
  $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_clonezilla_live_end
