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

# Load DRBL setting and functions
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"

. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
. /etc/drbl/drbl-ocs.conf
. $DRBL_SCRIPT_PATH/sbin/ocs-functions

# Load keymap if it exists
[ -e /etc/ocs/ocs-live.conf ] && . /etc/ocs/ocs-live.conf

# Stop plymouth if running
#if [ -n "$(LC_ALL=C ps -efw | grep plymouth | grep -v grep)" ]; then
#  plymouth quit
#fi

#
fb_term=""
get_fb_term
[ "$fb_term" = "bterm" -a ! -e "$uni_font" ] && exit 1

export LANG="en_US.UTF-8"
gen_locale_if_not_found en_US en_US.UTF-8

#
export CURRENT_TTY="$(tty)"  # e.g. /dev/tty1, /dev/ttyS0...
# By default we will run "ocs-lang-kbd-conf" (same vc as $"$ocs_live_run_tty") in /dev/tty1 if ocs_live_run_tty is not specified. When "live-getty console=ttyS0,38400n81" is assigned, $ocs_live_run_tty is /dev/ttyS0. However, "ocs-lang-kbd-conf" might be run in runlevel 2 or systemd (e.g., called by /etc/rc2.d/S23start-ocs-live or /lib/systemd/system/start-ocs-live.service. In this case it's /dev/console) or after entering shell, it's the assigned console "$ocs_live_run_tty" or if it's not assigned, it's /dev/tty1.
# SUDO_TTY is assigned by "sudo -i SUDO_TTY="$(tty) ocs-live-run-menu" in ~user/.bash_profile.
# This is due to sudo >= 1.9.8p2-1 will new a pts (e.g., /dev/pts/0) instead of keeping the tty from SUDO_USER.
# Hence we use "sudo -i SUDO_TTY="$(tty)" ocs-live-run-menu" in .bash_profile. Then SUDO_TTY will be shown here.
if [ -n "$ocs_live_run_tty" ]; then
  # tty is specified, e.g. /dev/ttyS0. Check if it is the current tty
  if [ "$CURRENT_TTY" != "/dev/console" -a \
       "$CURRENT_TTY" != "$ocs_live_run_tty" -a \
       "$SUDO_TTY" != "/dev/console" -a \
       "$SUDO_TTY" != "$ocs_live_run_tty" ]; then
    echo "Not in /dev/console or \"$ocs_live_run_tty\", exiting $0..."
    exit 3
  fi
else
  # No tty is specified to run "ocs-lang-kbd-conf". Default to run only on /dev/console (runlevel 2) or /dev/tty1 (after entering shell).
  # If it's not in /dev/tty1 or /dev/console (during rc), just exit.
  if [ "$CURRENT_TTY" != "/dev/console" -a \
       "$CURRENT_TTY" != "/dev/tty1" -a \
       "$SUDO_TTY" != "/dev/console" -a \
       "$SUDO_TTY" != "/dev/tty1" ]; then
    echo "Not in /dev/console or /dev/tty1, exiting $0..."
    exit 3
  fi
fi

# ocs_live_keymap and live_keyboard_layouts are loaded from ocs-live.conf
if [ -z "$live_keyboard_layouts" ]; then
  echo "Configuring keyboard..."
  # keymap not set in ocs-live.conf, use interactive dialog to set it.
  # Bterm/jfbterm can only be used (it's better since Asian fonts can be shown) when:
  # (1) If bterm and uni fonts or jfbterm are available
  # (2) If it's not in serial console
  # (3) frambebuffer device exists
  # (4) CPU arch is x86/x86-64 since jfbterm does not work on aarch64.
  if [ -n "$fb_term" ] && \
     [ -z "$(grep -E "console=.*ttyS.*" /proc/cmdline)" ] && \
     ([ -e /dev/fb/0 ] || [ -e /dev/fb0 ]) && \
     [ -n "$(LC_ALL=C uname -m | grep -iE "(x86_64|i[3-6]86)")" ]; then 
     case "$fb_term" in
       "bterm")
          export TERM=bterm
          set +e
          # bterm need full path command even it's in the PATH already.
          bterm -l $LANG -f $uni_font `command -v ocs-langkbdconf-bterm`
          set -e
          ;;
       "jfbterm")
          export TERM=jfbterm
          set +e
          jfbterm -q -e ocs-langkbdconf-bterm
          set -e
          ;;
     esac
  else
    # ocs-langkbdconf-bterm does keyboard config and language config. Here we have do the same 2 jobs. Otherwise locales will be empty and a lot of warnings will be shown when ruuning commands.
    force_TERM_as_linux_if_necessary
    # (1) language
    [ -z "$ocs_lang" ] && ocs_lang=en_US.UTF-8
    # put the language setting so that later program can reuse
    if [ -z "$(grep -E "^[[:space:]]*ocs_lang=" /etc/ocs/ocs-live.conf 2>/dev/null)" ]; 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
    # (2) keyboard
    drbl-kbdchooser
  fi
fi

# Here we disable setting the system locale, since most of the cases, e.g. for Chinese, the terminal won't show the characters, unless it's in bterm/jfbterm.
# Varialble ocs_lang will be created in ocs-langkbdconf-bterm, and write to /etc/ocs/ocs-live.conf.
# Load ocs_lang if it exists
#[ -e /etc/ocs/ocs-live.conf ] && . /etc/ocs/ocs-live.conf
#if [ -n "${ocs_lang}" ]; then
#  echo "Setting locale in /etc/default/locale... "
#  cat <<-LOCALE_END > /etc/default/locale
#LC_ALL="${ocs_lang}"
#LANG="${ocs_lang}"
#LOCALE_END
#fi
