#!/bin/bash
# License: GPL 
# Author: Steven Shiau <steven _at_ clonezilla org>
# Description: Program to run commands assigned in boot parameter, like: ocs_prerun*, ocs_postrun*, ocs_savedisk_prerun*...

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

#
cmdl_file_def="/proc/cmdline"
force_to_run="no"

# Functions
USAGE() {
    echo "$ocs - Run commands assigned in boot parameter"
    echo "Usage:"
    echo "To run $ocs PARAM:"
    echo "$ocs [OPTION]"
    echo "Options:"
    echo "-b, --batch-mode     Run this program in batch mode"
    echo "-c, --cmdline-file   Assign the kernel boot parameter file. If not assigned, \"$cmdl_file_def\" will be used."
    echo "-f, --force          Force to run $ocs, no matter it has been run successfully or not."
    echo "PARAM is one of the following:"
    echo "ocs_prerun*, ocs_postrun*, ocs_savedisk_prerun*..."
    echo "E.g. To run the commands assigned by ocs_prerun* (e.g. ocs_prerun, ocs_prerun1, ocs_prerun2...) in the kernel boot parameter file 'my-cmdline', run:"
    echo "     $ocs -c my-cmdline ocs_prerun"
    echo "Multiple PARAMs in boot param are available, i.e. ocs_prerun*, just append a number after that. E.g."
    echo "ocs_prerun=... ocs_prerun1=... ocs_prerun2=..."
    echo "//NOTE// use \"ocs_prerun\" for this command only, while putting multiple ocs_prerun* in the boot parameter." 
} # end of USAGE

#################
##### MAIN ######
#################

ocs_file="$0"
ocs=`basename $ocs_file`
#
while [ $# -gt 0 ]; do
 case "$1" in
   -b|--batch-mode)
      ocs_batch_mode="on"
      shift;;
   -c|--cmdline-file)
      shift
      if [ -z "$(echo $1 |grep ^-.)" ]; then
        # skip the -xx option, in case 
        cmdl_file="$1"
        shift
      fi
      [ -z "$cmdl_file" ] && echo "-c is used, but no cmdl_file assigned." && exit 1
      ;;
   -f|--force) force_to_run="yes"; shift;;
   -*)     echo "${0}: ${1}: invalid option" >&2
           USAGE >& 2
           exit 2 ;;
   *)      break ;;
 esac
done

ocs_param="$1"

check_if_root
ask_and_load_lang_set

[ -z "$cmdl_file" ] && cmdl_file="$cmdl_file_def"
if [ ! -e "$cmdl_file" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "Kernel cmdline file ($cmdl_file) does _NOT_ exist!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_program_stop"
  exit 1
fi

ocs_param_list="$(grep -Ewo "${ocs_param}[[:digit:]]*" $cmdl_file | uniq | sort -V)"
ocs_param_list="$(echo $ocs_param_list)"  # in one line

if [ -z "$ocs_param_list" ]; then
  exit 1
else
  echo "Found ${ocs_param}* parameter in boot parameters..."
  echo "The order to run: $ocs_param_list"
fi

parse_cmdline_option -c $cmdl_file "echo_${ocs_param}"

eval echo_ocs_param="\$echo_$ocs_param"

# Now start parsing the paramaters listed in $ocs_param_list
RETVAL=0
for i in $ocs_param_list; do
  parse_cmdline_option -c $cmdl_file "$i"
  eval irun=\$$i
  if [ -n "$irun" ]; then
    # Only when not force to run, we will check the tag file
    if [ "$force_to_run" = "no" ]; then
      # Checking if this program is already run
      if [ -e /var/lib/clonezilla/$i ]; then
        [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
	echo "This boot parameter has been successfully run before, so skip it: $i ($irun)"
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
        continue
      fi
    fi
    echo "**************************"
    # run it
    if [ "$echo_ocs_param" != "no" ]; then
      echo "Now run \"$i\": $irun..."
    fi
    # Since "$irun" might not be exe mode, so we test it if it's script, use . $irun, else, run it directly.
    if [ "$(LANG=C file -Ls "$irun" 2>/dev/null | grep -iE "shell script")" ]; then
      $irun
      rc=$?
    else
      eval $irun
      rc=$?
    fi
    RETVAL="$((RETVAL + $rc))"
    # Creating state file
    if [ "$rc" -eq 0 ]; then
      touch /var/lib/clonezilla/$i
    else
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "Failed to run: $irun"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      if [ "$ocs_batch_mode" != "on" ]; then
        echo -n "$msg_press_enter_to_continue..."
        read
      fi
    fi
  fi
done

exit $RETVAL
