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

# To avoid there is no input/output in rc 1 running, we add this one. (This is specially in Ubuntu 6.10 with upstart)
# Two cases:
# (1) If it's in runlevel 1 or S (actually S19ocs-run is in rc1.d, but S20single will change to S), we assume it's in clonezilla mode and client is booting. In this case, we redirect the input/output to /dev/console
# (2) If not in runlevel 1 and not S, it's in debuging when in rc [2-5], and maybe we use ssh to login to debug. In this case, we do not want to use /dev/console.
RL="$(runlevel | awk -F" " '{print $2}')"
if [ "$RL" = "1" -o "$RL" = "S" ]; then
  exec </dev/console >/dev/console 2>&1
fi

# Load DRBL and Clonezilla functions
. /opt/drbl/sbin/drbl-conf-functions
. /opt/drbl/sbin/ocs-functions
. /opt/drbl/conf/drbl-ocs.conf

# functions
wait_for_press_enter_then_abort() {
  local messg="$1"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "$messg"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "Abort!"
  echo -n "Press Enter to exit..."
  read
  exit 1
}      
do_start_ocs_run() {
  # Example:
  #  /opt/drbl/sbin/ocs-sr --language 0  -g auto -hn0 PC -p true --max-time-to-wait 300 --mcast-port 2232 multicast_restoredisk 2disks hda hdb
  
  # 3 modes, proc-cmdline, ocs-run.param, and both. If both, the ocs_opt are the same in proc-cmdline and ocs-run.param. We choose to use proc-cmdline if it's both so that we can always put "ocs_client_trig_type=both" in drbl-ocs.conf. If we want to interactively debug, use ocs-run -p.
  case "$ocs_client_trig_type" in
    proc-cmdline|both)
      echo "Reading ocs_opt from /proc/cmdline..."
      ocs_opt="$(grep -Ew "ocs_opt" /proc/cmdline | sed -e "s/.*ocs_opt=\"\(.*\)\".*$/\1/")"
      if [ -n "$ocs_opt" ]; then
        echo Now run: $DRBL_SCRIPT_PATH/sbin/ocs-sr $ocs_opt
        $DRBL_SCRIPT_PATH/sbin/ocs-sr $ocs_opt
      else
        wait_for_press_enter_then_abort "No clonezilla param in /proc/cmdline!"
      fi
      ;;
    ocs-run.param)
      echo "Reading ocs_opt from /etc/ocs/ocs-run.param..."
      ocs_opt=$(cat /etc/ocs/ocs-run.param 2>/dev/null) 
      if [ -n "$ocs_opt" ]; then
        echo Now run: $DRBL_SCRIPT_PATH/sbin/ocs-sr $ocs_opt
        $DRBL_SCRIPT_PATH/sbin/ocs-sr $ocs_opt
      else
        wait_for_press_enter_then_abort "No clonezilla param in /etc/ocs/ocs-run.param!"
      fi
      ;;
    *)
      wait_for_press_enter_then_abort "Unknown mode, only proc-cmdline or ocs-run.param is accepted!"
      ;;
  esac
} # end of do_start_ocs_run
#
do_stop_ocs_run() {
    kill_ps_by_kill_9 ocs-sr
    kill_ps_by_killall_9 udp-receiver
    kill_ps_by_killall_9 partimage
    kill_ps_by_killall_9 ntfsclone
} # end of do_stop_ocs_run

# ocs_client_trig_type is read from /opt/drbl/conf/drbl-ocs.conf, but we can overwrite it here.
# proc-cmdline: read ocs_opt from /proc/cmdline
# ocs-run.param: read ocs_opt from /etc/ocs/ocs-run.param
# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    -c|--proc-cmdline)
            ocs_client_trig_type="proc-cmdline"
            shift ;;
    -p|--ocs-run-param)
            ocs_client_trig_type="ocs-run.param"
            shift ;;
    -*)     echo "${0}: ${1}: invalid option" >&2
            USAGE >& 2
            exit 2 ;;
    *)      break ;;
  esac
done

action="$1"

# Set the locale to avoid the dialog distortion.
if [ -e /etc/debian_version ]; then
  # Debian
  # For B2D, it uses /etc/sysconfig/i18n...
  [ -e /etc/sysconfig/i18n ] && . /etc/sysconfig/i18n
  [ -e /etc/environment ] && . /etc/environment
elif [ -e /etc/SuSE-release ]; then
  # SuSE
  [ -e /etc/SuSEconfig/profile ] && . /etc/SuSEconfig/profile
else
  # RH-like
  [ -e /etc/sysconfig/i18n ] && . /etc/sysconfig/i18n
fi
# Force to set LANG=C so that we can see yes/no in dialog...
export LANG=C

#
case "$action" in
  start)
    do_start_ocs_run;;
  stop)
    do_stop_ocs_run;;
  *)
    echo "Usage: $0 [-c|-p] {start|stop}"
    exit 2
    ;;
esac

exit 0
