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

# Load functions
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/opt/drbl/}"
. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
. $DRBL_SCRIPT_PATH/conf/drbl-ocs.conf
. $DRBL_SCRIPT_PATH/sbin/ocs-functions

LIVE_MEDIA=""
get_live_media_mnt_point

if [ -z "$LIVE_MEDIA" ]; then
  echo "///WARNING/// filesystem.squashfs not found! No idea where is LIVE_MEDIA!!!"
fi
echo "Live media is in $LIVE_MEDIA"

# Prepare drbl/clonezilla runtime programs and dir
if [ -e $LIVE_MEDIA/pkg/custom-ocs ]; then
  echo "Preparing customized Clonezilla program $DRBL_SCRIPT_PATH/sbin/custom-ocs..."
  install -m 755 $LIVE_MEDIA/pkg/custom-ocs $DRBL_SCRIPT_PATH/sbin/
fi

echo -n "Updating /etc/ocs/ocs-live.conf based on kernel parameters if found... "
# 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 ocs_daemonon ocs_daemonoff ocs_numlk ocs_capslk ocs_live_run_tty ocs_batch_mode ocs_live_type ocsroot_src ocs_sr_save_extra_opt ocs_sr_restore_extra_opt"
parse_cmdline_option "$param_2_be_parsed"

# Before writting these parameters to /etc/ocs/ocs-live.conf, first we deal with console parameter. The ocs_live_run_tty will be overwritten by console=.* boot parameter if console=.* is set.
# i.e. if live-getty and console=... (e.g. live-getty console=ttyS0,38400n81) are assigned in boot parameters, we make it as the default for ocs_live_run_tty.
# The priority for the boot parameters: (1) console=.* parameter (2) ocs_live_run_tty parameter
ocs_live_run_tty_set_in_cmdline=""
[ -n "$ocs_live_run_tty" ] && ocs_live_run_tty_set_in_cmdline="yes"
if [ -n "$(LC_ALL=C grep -iw live-getty /proc/cmdline)" -a \
     -n "$(LC_ALL=C grep -io "console=.*" /proc/cmdline)" ]; then
  # The following is borrowed from live-initramfs
  for ARGUMENT in $(cat /proc/cmdline); do
    case "${ARGUMENT}" in
      console=*) DEFCONSOLE="${ARGUMENT#*=}" ;;
    esac
  done
  ocs_live_run_tty="/dev/$(echo "${DEFCONSOLE}" | sed -e 's%,.*%%')"
  if [ "$ocs_live_run_tty_set_in_cmdline" = "yes" ]; then
    echo "//NOTE// Boot parameters live-getty and $DEFCONSOLE were found in /proc/cmdline! Make ocs_live_run_tty=$ocs_live_run_tty! The original assignment of ocs_live_run_tty in the boot parameter is ignored!"
    echo -n "Press \"Enter\" to continue..."
    read
  fi
fi

# tune the param in /etc/ocs/ocs-live.conf
for ik in $param_2_be_parsed; do
  eval real_var=\$$ik
  if [ -n "$real_var" ]; then
    if [ -z "$(LANG=C grep -E "^[[:space:]]*$ik=" /etc/ocs/ocs-live.conf 2>/dev/null)" ]; then
      # append it
      echo "$ik=\"$real_var\"" >> /etc/ocs/ocs-live.conf
    else
      # modify it
      perl -pi -e "s|^[[:space:]]*$ik=.*|$ik=\"$real_var\"|g" /etc/ocs/ocs-live.conf
    fi
  fi
done
echo "done!"

# Forget about this. From Ubuntu Karmic, we use the /usr/share/initramfs-tools/scripts/init-top/framebuffer comes with initramfs-tools to deal with this. i.e. we use the format mode_option:<xres>x<yres>[-<bpp>][@<refresh>] in the boot parameter to start uvesafb. //NOTE// video=uvesafb:1024x768 won't work in Karmic. We have to use something like video=uvesafb:mode_option=1024x768. Ref: https://bugs.launchpad.net/ubuntu/+source/initramfs-tools/+bug/437871
# # Parse the cmdline then add options for uvesafb
# if LANG=C grep -q "mode_option=" /proc/cmdline; then
#   echo "Getting mode_option from kernel parameter... "
#   mode_option="$(LC_ALL=C grep -Ew mode_option /proc/cmdline | sed -e "s/.*mode_option=\([^ ]\+\).*$/\1/")"
#   if [ -n "$(LC_ALL=C lsmod | grep -Ew uvesafb)" ]; then
#     echo "Unloading uvesafb and load it again to make mode_option=\"$mode_option\" work... "
#     rmmod uvesafb
#     echo "done!"
#   fi
#   echo "Loading uvesafb module..."
#   modprobe uvesafb mode_option=$mode_option
# fi

# Load the config
[ -e "/etc/ocs/ocs-live.conf" ] && . /etc/ocs/ocs-live.conf

# Process the daemon on and off
for i in $ocs_daemonon; do
  /etc/init.d/$i start
done

for i in $ocs_daemonoff; do
  /etc/init.d/$i stop
done

# Process the number lock and capslock
case "$ocs_numlk" in
  on|ON) setleds +num ;;
  off|OFF) setleds -num ;;
esac
	
case "$ocs_capslk" in
  on|ON) setleds +caps;;
  off|OFF) setleds -caps ;;
esac

# Append start clonezilla in auto login account's (with sudo privilege) bash profile
get_live_autologin_account
if [ -z "$live_autologin_account" ]; then
   echo "No account with NOPASSWD sudo privilege was found!"
   echo "Program terminated!"
   exit 1
fi
get_live_auto_login_id_home
if [ -z "$(LANG=C grep -E "^[^#][[:space:]]*sudo -i ocs-live-run-menu" $live_auto_login_id_home/.bash_profile 2>/dev/null)" ]; then
  if [ -z "$(LC_ALL=C grep -iE "ocs_server" /proc/cmdline)" ]; then
    # If the job is from Clonezilla SE, i.e. ocs_server is found in /proc/cmdline, we won't put the "clear" command in auto account's ~/.bash_profile. It's easier to debug in verbose for Clonezilla-live based Clonezilla SE.
    cat <<-PROFILE_END >> $live_auto_login_id_home/.bash_profile
# Added by Clonezilla live
clear
PROFILE_END
  fi
  cat <<-PROFILE_END >> $live_auto_login_id_home/.bash_profile
# By default we will start clonezilla on /dev/tty1 (no more /dev/ttyS0) only when \$ocs_live_run_tty is not specivied. If you want to use ttyS0, add live-getty and console=ttyS0,38400n81 in the boot parameter. The control is inside ocs-live-run-menu.
if type ocs-live-run-menu &>/dev/null; then
 sudo -i ocs-live-run-menu
fi
PROFILE_END
  chown $live_autologin_account.$live_autologin_account $live_auto_login_id_home/.bash_profile
fi

# ocs_live_keymap and ocs_lang will be processed later in another service

# Prepare mounting point for $ocsroot
[ -n "$ocsroot" ] && mkdir -p $ocsroot
