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

# Since at the time this script is executed, there is no clonezilla files in the system. Therefore we can not use functions from Clonezilla. hardcoded here.
# Decide the path for live media.
# casper 1.77+debian-7 uses /live_media, 1.110 uses /cdrom
# live-initramfs uses /live/image.
possible_path="/live_media /cdrom /live/image"
# Possible kernel/initrd paths are /casper (created by casper) or /live (created by live-initramfs)
live_sys_files_dir_list="casper live isolinux"

LIVE_MEDIA=""
# It's too slow to use find to search the whole system... Therefore we use this method.
for i in $possible_path; do
  for j in $live_sys_files_dir_list; do
    if [ -f "$i/$j/filesystem.squashfs" ]; then
      LIVE_MEDIA="$i"
      break
    fi
  done
  [ -n "$LIVE_MEDIA" ] && break
done

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
  # We do not put custom-ocs in /opt/drbl/sbin/, we put it in /opt/local/sbin/ so that when we use clonezilla live to create a custom clonezilla live, it won't be put in the custom clonezilla live. It should be put by ocs-iso, not in the tarball.
  echo "Preparing customized Clonezilla program /opt/local/sbin/custom-ocs..."
  mkdir -p /opt/local/sbin/
  cp -f /$LIVE_MEDIA/pkg/custom-ocs /opt/local/sbin/
  chmod 755 /opt/local/sbin/custom-ocs
fi

echo -n "Updating /etc/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"
parse_tmp="$(mktemp /tmp/cmdtmp.XXXXXX)"
for ik in $param_2_be_parsed; do
  # the parameter maybe like: ocs_lang=en_US.UTF-8 or ocs_lang="en_US.UTF-8"
  grep -oE "$ik=[\"]*[ 0-9A-Za-z-]*[\"]*[[:space:]]+" /proc/cmdline > $parse_tmp
  # now we can get the variable $ocs_live_keymap, $ocs_lang...
  . $parse_tmp
done
[ -f "$parse_tmp" ] && rm -f $parse_tmp

# tune the param in /etc/ocs-live.conf
for ik in $param_2_be_parsed; do
  eval real_var=\$$ik
  if [ -n "$real_var" ]; then
    if [ -z "$(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!"

# Parse the cmdline then add options for uvesafb
if grep -q "mode=" /proc/cmdline; then
  echo "Updating /etc/modprobe.d/options based on mode option in kernel parameter... "
  mode_option="$(grep mode /proc/cmdline | sed -e "s/.*mode=\([^ ]\+\).*$/\1/")"
  if [ -z "$(grep -E "^[[:space:]]*options uvesafb" /etc/modprobe.d/options 2>/dev/null)" ]; then
    # append it
    echo "options uvesafb mode=\"$mode_option\"" >> /etc/modprobe.d/options
  else
    # modify it
    perl -pi -e "s|^[[:space:]]*options uvesafb mode=.*|options uvesafb mode=\"$mode_option\"|g" /etc/modprobe.d/options
  fi
  echo "done!"
  if [ -n "$(lsmod | grep uvesafb)" ]; then
    echo "Unload uvesafb and load it again to make mode=\"$mode_option\" work... "
    rmmod uvesafb
    modprobe uvesafb
    echo "done!"
  fi
fi
