#!/bin/sh
# Written by K. L. Huang, Blake <c00hkl00@nchc.org.tw>. 
# Modified by Steven Shiau <steven@nchc.org.tw> to use in DRBL for RedHat
#
# To use reboot, shutdown, wake-on-lan for Cygwin clients, you must put the
# ssh key to those Cygwin clients.

# Load DRBL setting and functions
if [ ! -f "/opt/drbl/sbin/drbl-conf-functions" ]; then
  echo "Unable to find /opt/drbl/sbin/drbl-conf-functions! Program terminated!" 
  exit 1
fi
. /opt/drbl/sbin/drbl-conf-functions

#
MAX_DIALOG_HEIGHT="12"
DIA="/usr/bin/dialog"

PATH=$PATH:/usr/bin:/usr/sbin
#
check_if_root

#grep "^[[:space:]]*[#]*[[:space:]]*filename .*" dhcpd.conf

# function
USAGE() {
   echo "Usage: $0 [OPTION] MODE"
   echo "OPTION"
   language_help_prompt_by_idx_no
   echo "-h, --host [IP_LIST|MAC_LIST] Instead of all DRBL clients, assign some DRBL clients (IP_LIST or MAC_LIST) to be processed, if IP_LIST or MAC_LIST is not set, a dialog menu will be shown so that the target DRBL client(s) can be selected. The IP_LIST is like: \"192.168.0.1 192.168.0.2\", and the MAC_LIST is like \"00:50:56:01:02:01 00:50:56:01:02:02\". NOTE!!! You must put \" \" before and after the IP_LIST or MAC_LIST!"
   echo "-nl, --no-list-host   Set all clients' mode (This is default)"
   echo "MODE:"
   echo "remote-linux:         Client machine will boot from DRBL server,"
   echo "                      use the default text/graphic mode, for powerful client."
   echo "remote-linux-gra: Client machine will boot from DRBL server,"
   echo "                      and enter graphic mode, for powerful client."
   echo "remote-linux-txt:    Client machine will boot from DRBL server,"
   echo "                      and enter text mode, for powerful client."
   echo "thin-client:          Remote display Linux, thin client"
   echo "remote-memtest:       Remote boot to run memtest86"
   echo "remote-fdos:          Remote boot to run FreeDOS"
   echo "clonezilla-save-disk: Clonezilla save disk mode"
   echo "clonezilla-restore-disk: Clonezilla restore disk Mode"
   echo "clonezilla-save-parts: Clonezilla save partitions mode"
   echo "clonezilla-restore-parts: Clonezilla restore partitions mode"
   echo "clonezilla-save-hda1: Clonezilla save 1st partition (hda1) mode"
   echo "clonezilla-restore-hda1: Clonezilla restore 1st partition (hda1) mode"
   echo "clonezilla-stop:      Stop the clonezilla mode"
   echo "local:                Client machine will boot from local (now PXE only)"
   echo "reboot                Reboot DRBL clients now"
   echo "shutdown              Shutdown DRBL clients now"
   echo "Wake-on-LAN           Turn on DRBL clients by Wake-on-LAN now"
   echo "netinstall            Let clients to install Linux distribution via network (PXE/etherboot)"
   echo "switch-pxe-menu       Switch DRBL client menus"
   echo "login-switch          Switch DRBL client login mode"
   echo "switch-drbl-serv Start, stop or restart DRBL related services in DRBL server"
   echo "If no argument is specified, a dialog will appear to let you choose the mode!"
}

set_drbl_default_nbi() {
  # no matter it's "include" or "#include", just overwrite it, set it as we want
  # i.e. "include" is NOT comment, "filename" is comment
  # i.e. only the combination of " " and "#"  ("#" is zero or one...)
  FILENAME=""
  perl -p -i -e "s/^[[:space:]]*[#]*[[:space:]]*include[[:space:]]+\"\/etc\/dhcpd.conf.etherboot.include\";/include \"\/etc\/dhcpd.conf.etherboot.include\";/" $DHCPDCONF_DIR/dhcpd.conf
  perl -p -i -e "s/^[[:space:]]*[#]*[[:space:]]*filename[[:space:]]+\".*\"[[:space:]]*;/  #filename \"$FILENAME\";/" $DHCPDCONF_DIR/dhcpd.conf
  /etc/init.d/dhcpd restart
}

switch_filename() {
  FILENAME=$1
  # comment the include, i.e. disable the filename automatically generated by
  # etherboot client triggered.
  # i.e. "include" is comment, "filename" is NOT comment
  perl -p -i -e "s/^[[:space:]]*[#]*[[:space:]]*include[[:space:]]+\"\/etc\/dhcpd.conf.etherboot.include\";/#include \"\/etc\/dhcpd.conf.etherboot.include\";/" $DHCPDCONF_DIR/dhcpd.conf
  # set the filename in /etc/dhcpd.conf
  perl -p -i -e "s/^[[:space:]]*[#]*[[:space:]]*filename[[:space:]]+\".*\"[[:space:]]*;/  filename \"$FILENAME\";/" $DHCPDCONF_DIR/dhcpd.conf
  /etc/init.d/dhcpd restart
}

# function : stop_ocs
stop_ocs_if_necessary() {
  # $IP_LIST and $OCS_OPTS are global variables

  # if "LIST_HOST" is NOT nothing, we will process those clients... use the
  # -h parameter of drbl-ocs.
  if [ -f "$ocsroot/clonezilla.lock" ]; then
    echo "Your system is already in clonezilla mode... we will stop it first, then change to other mode..."
    if [ -n "$IP_LIST" ]; then 
      $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -h "$IP_LIST" -l $lang stop
    else
      $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -l $lang stop
    fi
  fi
}

#
multicast_clonezilla_restore_warning() {
  $DIA --title "$msg_multicast_clonezilla_warning" --clear \
       --msgbox "$msg_multicast_clonezilla_warning_des" 0 0
}

#
ask_if_want_list_host() {
$DIA --title "$msg_title_set_all_clients" \
     --yesno "$msg_set_all_clients \n\
$msg_set_all_clients_des" 0 0 

case $? in
  0)
    LIST_HOST="off";;
  1)
    LIST_HOST="on";;
  255)
    echo "Box closed.";;
esac

}
#
ask_if_skip_set_ocs_extra_param() {
  #TODO: not all drbl-ocs parameters are set here... add more in the future.
  local mode=$1
  OCS_PARAM_TMP=$2
  local OCS_OPTS

  if [ "$mode" = "restore" ]; then
    restore_prompt1="-c,      $msg_osc_param_c\n-f,      $msg_osc_param_f \n-g auto, $msg_osc_param_g_auto"
  fi

$DIA --title "$msg_clonezilla_advanced_extra_param" \
     --yesno "$msg_skip_set_advanced_extra_param\
$restore_prompt1" 0 0 
case $? in
  0)
    # skip setting, so set_ocs_param=no
    set_ocs_param="no";;
  1)
    # NO skiping setting, so set_ocs_param=yes
    set_ocs_param="yes";;
  255)
    echo "Box closed.";;
esac

if [ "$set_ocs_param" = "yes" ]; then
  if [ "$mode" = "restore" ]; then
    $DIA --separate-output --backtitle "$msg_nchc_free_software_labs" --title  \
    "$msg_clonezilla_advanced_extra_param" --checklist "$msg_choose_param_to_set:" \
    0 0 0 \
    "-n"       "$msg_osc_param_n" off \
    "-c"       "$msg_osc_param_c" off \
    "-s"       "$msg_osc_param_s" off \
    "-f"       "$msg_osc_param_f" off \
    "-g auto"  "$msg_osc_param_g_auto" off \
    2> $OCS_PARAM_TMP
  else
    $DIA --separate-output --backtitle "$msg_nchc_free_software_labs" --title  \
    "$msg_clonezilla_advanced_extra_param" --checklist "$msg_choose_param_to_set:" \
    0 0 0 \
    "-n"       "$msg_osc_param_n" off \
    "-s"       "$msg_osc_param_s" off \
    2> $OCS_PARAM_TMP
  fi
fi
}

#
run_login_switch() {
  TMP=$(mktemp /tmp/menu.XXXXXX)
  trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM RETURN EXIT
  echo "Switch DRBL client login mode"
  $DIA --backtitle "$msg_nchc_free_software_labs" --title  \
  "$msg_login_switch" --radiolist "$msg_choose_action:" \
  0 0 0 \
  "normal"  "$msg_normal_login" off \
  "auto"    "$msg_auto_login" off \
  "timed"   "$msg_timed_login" off \
  2> $TMP
  action="--""$(cat $TMP)"
  [ -f "$TMP" ] && rm -f $TMP
  [ -z "$action" ] && echo "You must specify the action! Program terminated!!!" && exit 1
  $DRBL_SCRIPT_PATH/sbin/drbl-login-switch $action
} # end of run_login_switch

#
run_switch_drbl_service() {
  TMP=$(mktemp /tmp/menu.XXXXXX)
  trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM RETURN EXIT
  echo "Switch DRBL related services in DRBL server"
  $DIA --backtitle "$msg_nchc_free_software_labs" --title  \
  "$msg_switch_drbl_service" --radiolist "$msg_choose_action:" \
  0 0 0 \
  "start"    "$msg_start_drbl_all_services" off \
  "stop"     "$msg_stop_drbl_all_services" off \
  "restart"  "$msg_restart_drbl_all_services" off \
  2> $TMP
  action="$(cat $TMP)"
  [ -f "$TMP" ] && rm -f $TMP
  [ -z "$action" ] && echo "You must specify the action! Program terminated!!!" && exit 1
  $DRBL_SCRIPT_PATH/sbin/drbl-all-service $action
} # end of run_switch_drbl_service

# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    -l|--language)
	shift; specified_lang="$1"
	shift
	;;
    -h|--host)  
	    LIST_HOST="on"
            shift
            # skip the -xx option, in case 
            if [ -z "$(echo $1 |grep ^-.)" ]; then
	      # parse if the arg is MAC or IP, skip those not MAC or IP
              if [ -n "$(echo $1 | grep -E "^[[:space:]]*([[:alnum:]]+:){5}[[:alnum:]]+([[:space:]]|$)+")" -o -n "$(echo $1 | grep -E "^[[:space:]]*([[:digit:]]+\.){3}[[:digit:]]+([[:space:]]|$)+")" -o -z "$1" ] ; then
                IP_LIST=$1
                shift
	      else
		echo "\"$1\" is not a suitable IP_LIST or MAC_LIST !!! Program terminated!!!"
		exit 1
              fi
            fi
	    ;;
    -nl|--no-list-host)  
	    LIST_HOST="off" 
	    shift;;
    -*)     echo "${0}: ${1}: invalid option" >&2
            USAGE >& 2
            exit 2 ;;
    *)      break ;;
  esac
done

######
## main script
######
MODE="$1"

ask_and_load_lang_set $specified_lang

[ -z "$LIST_HOST" ] && ask_if_want_list_host

# 
if [ "$LIST_HOST" = "on" ]; then
  # We need 1 file and 1 variable to sepcify hosts, 
  # (1) file "HOSTS_TMP", which contains IP list or MAC list.
  # (2) variable "IP_LIST", which contains IP list only (if user specify MAC list, we will convert them to IP list based on /etc/dhcpd.conf)

  # NOTES: after the modification of PXE_CONF, we must run
  #       [ "$LIST_HOST" = "on" ] && set_specific_host_pxe_conf `cat $HOSTS_TMP`
  # to update the pxeconf (both for 01-MAC and IP)

  HOSTS_TMP="$(mktemp /tmp/hosts_tmp.XXXXXXX)"
  trap "[ -f "$HOSTS_TMP" ] && rm -f $HOSTS_TMP" HUP INT QUIT TERM RETURN EXIT
  MAC_LIST=""
  # The content of $HOSTS_TMP can be IP list or MAC list (generated by select_hosts)
  if [ -z "$IP_LIST" ]; then
    $DRBL_SCRIPT_PATH/bin/select_hosts -l $lang $HOSTS_TMP 
    retval=$?
  else
    # user already specified the hosts, so we just put them in $HOSTS_TMP
    echo $IP_LIST > $HOSTS_TMP
    retval=$?
  fi
  if [ $retval -eq 0 ]; then
    # copy the skeleton file
    PXE_CONF="$PXELINUX_DIR/default_skeleton"
    cp -f $PXELINUX_DIR/default $PXE_CONF
  
    if grep -Eq "^[[:space:]]*([[:alnum:]]+:){5}[[:alnum:]]+([[:space:]]|$)+" $HOSTS_TMP; then
      # HOSTS_TMP is MAC list.
      # prepare the IP list if the HOSTS_TMP is MAC list.
      MAC_LIST="$(cat $HOSTS_TMP)"
      for imac in $MAC_LIST; do
        #IP="$(awk -F" " '/$imac/ {print $2}' $HOST_MAC_TABLE)"
        IP="$(grep $imac $HOST_MAC_TABLE | awk -F" " '{print $2}')"
        IP_LIST="$IP_LIST $IP"
      done
    else
      # HOSTS_TMP is IP list.
      IP_LIST="$(cat $HOSTS_TMP)"
    fi
  else
    echo "No client is selected! Abort!"
    exit 0
  fi
else
  PXE_CONF="$PXELINUX_DIR/default"
  # Clean all the previous saved config file.
  # These files maybe look like: 01-MAC address (with ":" -> "-"), something like C0A8XXXX (only for 192.168.x), default_skeleton
  echo -n "Clean all the previous saved config file if they exist..."
  for ifile in $PXELINUX_DIR/*; do
    [ "$ifile" != "$PXE_CONF" -a -f "$ifile" ] && rm -f $ifile
  done
  echo "done!"
fi

# Get the mode from use
if [ -z "$MODE" ]; then 
     TMP="$(mktemp /tmp/menu.XXXXXX)"
     trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM RETURN EXIT
     $DIA \
     --backtitle "$msg_nchc_title" \
     --title "$msg_nchc_drbl" \
     --no-shadow \
     --radiolist "$msg_switch_client_mode:" 0 0 0 \
     "remote-linux" "$msg_remote_linux" off \
     "remote-linux-gra" "$msg_remote_linux_graphic" off \
     "remote-linux-txt" "$msg_remote_linux_text" off \
     "thin-client" "$msg_thin_client" off \
     "remote-memtest" "$msg_remote_memtest" off \
     "remote-fdos" "$msg_remote_fdos" off \
     "clonezilla-start" "$msg_clonezilla_start" off \
     "clonezilla-stop" "$msg_clonezilla_stop" off \
     "netinstall" "$msg_netinstall" off \
     "local" "$msg_local" off \
     "reboot" "$msg_reboot" off \
     "shutdown" "$msg_shutdown" off \
     "Wake-on-LAN" "$msg_Wake_on_LAN" off \
     "switch-pxe-menu" "$msg_switch_pxe_menu" off \
     "login-switch" "$msg_login_switch" off \
     "switch-drbl-serv" "$msg_switch_drbl_service" off \
     2> $TMP
     MODE=$(cat $TMP)
     [ -f "$TMP" ] && rm -f $TMP
fi

# set the mode for PXE client
case "$MODE" in
  "remote-linux") 
         echo -n "Settinig client as remote-linux mode..."
         stop_ocs_if_necessary $IP_LIST
         $DRBL_SCRIPT_PATH/sbin/set-default-pxe-img -i drbl -c $PXE_CONF -l "$powerful_client_menu_label"
         if [ "$LIST_HOST" = "on" ]; then
	   $DRBL_SCRIPT_PATH/sbin/drbl-powerful-thin-client -p -h "$IP_LIST" -ln $lang
         else
	   $DRBL_SCRIPT_PATH/sbin/drbl-powerful-thin-client -p -ln $lang
	 fi
	 # set the etherboot environment
	 # set_drbl_default_nbi

	 # specify the nodes if assigned by user
         [ "$LIST_HOST" = "on" ] && set_specific_host_pxe_conf `cat $HOSTS_TMP`

	 echo "done!"
     ;;
  "remote-linux-gra") 
         CLIENT_INIT="5"
         echo "Settinig client as remote-linux graphic mode..."
         stop_ocs_if_necessary $IP_LIST
         $DRBL_SCRIPT_PATH/sbin/set-default-pxe-img -i drbl -c $PXE_CONF -l "$powerful_client_menu_label"
         if [ "$LIST_HOST" = "on" ]; then
	   $DRBL_SCRIPT_PATH/sbin/drbl-powerful-thin-client -p -h "$IP_LIST" -ln $lang
         else
	   $DRBL_SCRIPT_PATH/sbin/drbl-powerful-thin-client -p -ln $lang
         fi
	 # set the etherboot environment
	 # set_drbl_default_nbi

	 # specify the nodes if assigned by user
         [ "$LIST_HOST" = "on" ] && set_specific_host_pxe_conf `cat $HOSTS_TMP`

         for host in $drblroot/*; do
           # if the LIST_HOST is on, skip those IP not listed in the $IP_LIST
	   if [ "$LIST_HOST" = "on" ]; then
	     [ -z "$(echo $IP_LIST | grep ${host##/*/})" ] && continue
           fi

           # ocs config
           echo "Setting the remote-linux graphic mode for node IP = ${host##/*/}"
           # set the init to 5, i.e. default is X
           perl -p -i -e "s/^id:[1-5]:initdefault:/id:$CLIENT_INIT:initdefault:/g" $host/etc/inittab
         done
	 echo "done!"
         ;;
  "remote-linux-txt") 
         CLIENT_INIT="3"
         echo "Settinig client as remote-linux text mode..."
         stop_ocs_if_necessary $IP_LIST
         $DRBL_SCRIPT_PATH/sbin/set-default-pxe-img -i drbl -c $PXE_CONF -l "$powerful_client_menu_label"
         if [ "$LIST_HOST" = "on" ]; then
	   $DRBL_SCRIPT_PATH/sbin/drbl-powerful-thin-client -p -h "$IP_LIST" -ln $lang
         else
	   $DRBL_SCRIPT_PATH/sbin/drbl-powerful-thin-client -p -ln $lang
	 fi
	 # set the etherboot environment
	 # set_drbl_default_nbi

	 # specify the nodes if assigned by user
         [ "$LIST_HOST" = "on" ] && set_specific_host_pxe_conf `cat $HOSTS_TMP`

         for host in $drblroot/*; do
           # if the LIST_HOST is on, skip those IP not listed in the $IP_LIST
	   if [ "$LIST_HOST" = "on" ]; then
	     [ -z "$(echo $IP_LIST | grep ${host##/*/})" ] && continue
           fi

           # ocs config
           echo "Setting the remote-linux text mode for node IP = ${host##/*/}"
           # set the init to 3, i.e. default is text mode
           perl -p -i -e "s/^id:[1-5]:initdefault:/id:$CLIENT_INIT:initdefault:/g" $host/etc/inittab
         done
	 echo "done!"
         ;;
  "remote-memtest")
         echo -n "Setting client as remote-memtest mode..."
         stop_ocs_if_necessary $IP_LIST
         $DRBL_SCRIPT_PATH/sbin/set-default-pxe-img -i memtest -c $PXE_CONF
	 # set the etherboot environment
         # switch_filename memtest86.nbi

	 # specify the nodes if assigned by user
         [ "$LIST_HOST" = "on" ] && set_specific_host_pxe_conf `cat $HOSTS_TMP`

	 echo "done!"
	 ;;
  "remote-fdos")
         echo -n "Setting client as remote-fdos mode..."
         stop_ocs_if_necessary $IP_LIST
         $DRBL_SCRIPT_PATH/sbin/set-default-pxe-img -i fdos -c $PXE_CONF
	 # set the etherboot environment
         # switch_filename $fdos_nbi_output

	 # specify the nodes if assigned by user
         [ "$LIST_HOST" = "on" ] && set_specific_host_pxe_conf `cat $HOSTS_TMP`

	 echo "done!"
	 ;;
  "thin-client")
         echo -n "Settinig client as DRBL thin-client mode..."
         stop_ocs_if_necessary $IP_LIST
         # set_drbl_default_nbi
         $DRBL_SCRIPT_PATH/sbin/set-default-pxe-img -i drbl-thin-client -c $PXE_CONF

	 # specify the nodes if assigned by user
         [ "$LIST_HOST" = "on" ] && set_specific_host_pxe_conf `cat $HOSTS_TMP`

         if [ "$LIST_HOST" = "on" ]; then
           $DRBL_SCRIPT_PATH/sbin/drbl-client-system-select -e -h "$IP_LIST" on
	   $DRBL_SCRIPT_PATH/sbin/drbl-powerful-thin-client -t -h "$IP_LIST" -ln $lang
         else
           $DRBL_SCRIPT_PATH/sbin/drbl-client-system-select -e on
	   $DRBL_SCRIPT_PATH/sbin/drbl-powerful-thin-client -t -ln $lang
         fi
	 echo "done!"
	 ;;
  "netinstall")
     TMP="$(mktemp /tmp/menu.XXXXXX)"
     trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM RETURN EXIT
     netinstall_list="$(grep -E "^[[:space:]]*label[[:space:]]*[a-zA-Z]+-.*netinstall" $PXELINUX_DIR/default | sed -e "s/^[[:space:]]*label[[:space:]]*//g" | sort)"
     if [ -z "$netinstall_list" ]; then
        $DIA --title "$msg_no_network_installation_img" --clear \
        --msgbox "$msg_no_network_installation_img_des" 0 0
       exit 0
     fi
     MENU=""
     numimages=0
     for i in $netinstall_list; do
        numimages=$((numimages + 1))
        # EX for i: rh-8.0-netinstall
        # create the description for the netinstall image
        des="$(echo $i | sed -e "s/-netinstall$//g" | sed -e "s/rh-FC/Fedora_Core_/g" -e "s/rh-/RedHat-/g" -e "s/mdk-/MandrakeLinux_/g" | sed -e "s/-/_/g")"
        des=${des}_${msg_install_via_network}
        MENU="$MENU $i $des off "
     done
     if [ $numimages -gt 0 ]; then
       if [ $numimages -lt $MAX_DIALOG_HEIGHT ]; then
         height=$numimages
       else
         height="$MAX_DIALOG_HEIGHT"
       fi
       $DIA \
       --backtitle "$msg_nchc_title" \
       --title "$msg_netinstall" \
       --radiolist "$msg_choose_netinstall_imag" 0 80 $height \
       $MENU 2> $TMP
       OS_netinstall="$(cat $TMP)"
     fi
     [ -f "$TMP" ] && rm -f $TMP
     [ -z "$OS_netinstall" ] && echo "No distribution is selected! Program terminated!!!" && exit 1
     echo "This network installation is for: $OS_netinstall"
     echo "Setting client to the mode for installing this distribution via network..."
     stop_ocs_if_necessary $IP_LIST
     echo -n "Set the PXE environment..."
     $DRBL_SCRIPT_PATH/sbin/set-default-pxe-img -i $OS_netinstall -c $PXE_CONF
     # img_full="$(rpm -ql $OS_netinstall | grep -E "^.*_install.nbi")"
     # nbi="$(basename $img_full)"
     # [ -z "$nbi" ] && echo "Can NOT find the etherboot NBI image for this distribution!!! Something went wrong! Program terminated!!!" && exit 1
     # echo -n "Set the etherboot environment..."
     # switch_filename $nbi
     echo "done!"
     # specify the nodes if assigned by user
     [ "$LIST_HOST" = "on" ] && set_specific_host_pxe_conf `cat $HOSTS_TMP`
     ;;
  "clonezilla-start")
     TMP=$(mktemp /tmp/menu.XXXXXX)
     trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM RETURN EXIT
     $DIA \
     --backtitle "$msg_nchc_title" \
     --title "$msg_start_clonezilla_mode" \
     --radiolist "$msg_choose_mode" 0 0 0 \
     "clonezilla-save-disk" "$msg_clonezilla_save_disk" off \
     "clonezilla-restore-disk" "$msg_clonezilla_restore_disk" off \
     "clonezilla-save-parts" "$msg_clonezilla_save_parts" off \
     "clonezilla-restore-parts" "$msg_clonezilla_restore_parts" off \
     "clonezilla-save-hda1" "$msg_clonezilla_save_hda1" off \
     "clonezilla-restore-hda1" "$msg_clonezilla_restore_hda1" off 2> $TMP
     ocs_mode=$(cat $TMP)
     [ -f "$TMP" ] && rm -f $TMP
     [ -z "$ocs_mode" ] && echo "No clonezilla mode is selected! Program terminated!!!" && exit 1
     echo "Now the clonezilla mode is: $ocs_mode"
     case "$ocs_mode" in
       "clonezilla-save-disk")
          echo -n "Setting client as clonezilla-save mode..."

	  # ask if want to set ocs extra param
          OCS_PARAM_TMP=`mktemp /tmp/ocs_param_tmp.XXXXXX`
          trap "[ -f "$OCS_PARAM_TMP" ] && rm -f $OCS_PARAM_TMP" HUP INT QUIT TERM RETURN EXIT
          ask_if_skip_set_ocs_extra_param save $OCS_PARAM_TMP
          OCS_OPTS="$(cat $OCS_PARAM_TMP)"
          [ -f "$OCS_PARAM_TMP" ] && rm -f $OCS_PARAM_TMP

          stop_ocs_if_necessary $IP_LIST
          if [ "$LIST_HOST" = "on" ]; then
            $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -h "$IP_LIST" -l $lang startdisk save
          else
            $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -l $lang startdisk save
          fi
          echo "done!"
          ;;
       "clonezilla-restore-disk")
          echo -n "Setting client as clonezilla-restore-disk mode..."

	  # ask if want to set ocs extra param
          OCS_PARAM_TMP=`mktemp /tmp/ocs_param_tmp.XXXXXX`
          trap "[ -f "$OCS_PARAM_TMP" ] && rm -f $OCS_PARAM_TMP" HUP INT QUIT TERM RETURN EXIT
          ask_if_skip_set_ocs_extra_param restore $OCS_PARAM_TMP
          OCS_OPTS="$(cat $OCS_PARAM_TMP)"
          [ -f "$OCS_PARAM_TMP" ] && rm -f $OCS_PARAM_TMP

          stop_ocs_if_necessary $IP_LIST
          # Blake, 2004/01/29, unicast, multicast or unicast + multicast
          TMP=$(mktemp /tmp/menu.XXXXXX)
          trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM RETURN EXIT
          $DIA \
          --backtitle "$msg_nchc_title" \
          --title "$msg_nchc_clonezilla" \
          --radiolist "$msg_choose_the_mode_to_restore_disk" 0 0 0 \
          "unicast" "$msg_unicast_restore" off \
          "multicast" "$msg_multicast_restore" off 2> $TMP
          RESTORE_MODE=$(cat $TMP) 
          [ -f "$TMP" ] && rm -f $TMP
          [ -z "$RESTORE_MODE" ] && echo "No mode is selected! Program terminated!!!" && exit 1
          if [ "$RESTORE_MODE" = "multicast" ]; then
            if [ "$LIST_HOST" = "on" ]; then
              multicast_clonezilla_restore_warning
              $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -h "$IP_LIST" -l $lang startdisk multicast_restore
            else
              $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -l $lang startdisk multicast_restore
            fi
          else
            if [ "$LIST_HOST" = "on" ]; then
              $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -h "$IP_LIST" -l $lang startdisk restore
            else
              $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -l $lang startdisk restore
	    fi
          fi
          echo "done!"
          ;;
       "clonezilla-save-parts")
          echo -n "Setting client as clonezilla-save mode..."

	  # ask if want to set ocs extra param
          OCS_PARAM_TMP=`mktemp /tmp/ocs_param_tmp.XXXXXX`
          trap "[ -f "$OCS_PARAM_TMP" ] && rm -f $OCS_PARAM_TMP" HUP INT QUIT TERM RETURN EXIT
          ask_if_skip_set_ocs_extra_param save $OCS_PARAM_TMP
          OCS_OPTS="$(cat $OCS_PARAM_TMP)"
          [ -f "$OCS_PARAM_TMP" ] && rm -f $OCS_PARAM_TMP

          stop_ocs_if_necessary $IP_LIST
          if [ "$LIST_HOST" = "on" ]; then
            $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -h "$IP_LIST" -l $lang startparts save
          else
            $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -l $lang startparts save
          fi
          echo "done!"
          ;;
       "clonezilla-restore-parts")
          echo -n "Setting client as clonezilla-restore-parts mode..."

	  # ask if want to set ocs extra param
          OCS_PARAM_TMP=`mktemp /tmp/ocs_param_tmp.XXXXXX`
          trap "[ -f "$OCS_PARAM_TMP" ] && rm -f $OCS_PARAM_TMP" HUP INT QUIT TERM RETURN EXIT
          ask_if_skip_set_ocs_extra_param restore $OCS_PARAM_TMP
          OCS_OPTS="$(cat $OCS_PARAM_TMP)"
          [ -f "$OCS_PARAM_TMP" ] && rm -f $OCS_PARAM_TMP

          stop_ocs_if_necessary $IP_LIST
          # Blake, 2004/01/29, unicast, multicast or unicast + multicast
          TMP=$(mktemp /tmp/menu.XXXXXX)
          trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM RETURN EXIT
          $DIA \
          --backtitle "$msg_nchc_title" \
          --title "$msg_nchc_clonezilla" \
          --radiolist "$msg_choose_the_mode_to_restore_partitions" 0 0 0 \
          "unicast" "$msg_unicast_restore" off \
          "multicast" "$msg_multicast_restore" off 2> $TMP
          RESTORE_MODE=$(cat $TMP) 
          [ -f "$TMP" ] && rm -f $TMP
          [ -z "$RESTORE_MODE" ] && echo "No mode is selected! Program terminated!!!" && exit 1
          if [ "$RESTORE_MODE" = "multicast" ]; then
            if [ "$LIST_HOST" = "on" ]; then
              $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -h "$IP_LIST" -l $lang startparts multicast_restore
            else
              $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -l $lang startparts multicast_restore
	    fi
          else
            if [ "$LIST_HOST" = "on" ]; then
              $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -h "$IP_LIST" -l $lang startparts restore
            else
              $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -l $lang startparts restore
	    fi
          fi
          echo "done!"
          ;;
       "clonezilla-save-hda1")
          echo -n "Setting client as clonezilla-save-hda1 mode..."

	  # ask if want to set ocs extra param
          OCS_PARAM_TMP=`mktemp /tmp/ocs_param_tmp.XXXXXX`
          trap "[ -f "$OCS_PARAM_TMP" ] && rm -f $OCS_PARAM_TMP" HUP INT QUIT TERM RETURN EXIT
          ask_if_skip_set_ocs_extra_param save $OCS_PARAM_TMP
          OCS_OPTS="$(cat $OCS_PARAM_TMP)"
          [ -f "$OCS_PARAM_TMP" ] && rm -f $OCS_PARAM_TMP

          stop_ocs_if_necessary $IP_LIST
          if [ "$LIST_HOST" = "on" ]; then
            $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -h "$IP_LIST" -l $lang start save
          else
            $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -l $lang start save
	  fi
          echo "done!"
          ;;
       "clonezilla-restore-hda1")
          echo -n "Setting client as clonezilla-restore-hda1 mode..."

	  # ask if want to set ocs extra param
          OCS_PARAM_TMP=`mktemp /tmp/ocs_param_tmp.XXXXXX`
          trap "[ -f "$OCS_PARAM_TMP" ] && rm -f $OCS_PARAM_TMP" HUP INT QUIT TERM RETURN EXIT
          ask_if_skip_set_ocs_extra_param restore $OCS_PARAM_TMP
          OCS_OPTS="$(cat $OCS_PARAM_TMP)"
          [ -f "$OCS_PARAM_TMP" ] && rm -f $OCS_PARAM_TMP

          stop_ocs_if_necessary $IP_LIST
          # Blake, 2004/01/29, unicast, multicast or unicast + multicast
          TMP=$(mktemp /tmp/menu.XXXXXX)
          trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM RETURN EXIT
          $DIA \
          --backtitle "$msg_nchc_title" \
          --title "$msg_nchc_clonezilla" \
          --radiolist "$msg_choose_the_mode_to_restore_hda1" 0 0 0 \
          "unicast" "$msg_unicast_restore" off \
          "multicast" "$msg_multicast_restore" off 2> $TMP
          RESTORE_MODE=$(cat $TMP) 
          [ -f "$TMP" ] && rm -f $TMP
          [ -z "$RESTORE_MODE" ] && echo "No mode is selected! Program terminated!!!" && exit 1
          if [ "$RESTORE_MODE" = "multicast" ]; then
            if [ "$LIST_HOST" = "on" ]; then
              $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -h "$IP_LIST" -l $lang start multicast_restore
            else
              $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -l $lang start multicast_restore
	    fi
          else
            if [ "$LIST_HOST" = "on" ]; then
              $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -h "$IP_LIST" -l $lang start restore
            else
              $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -l $lang start restore
	    fi
          fi
          echo "done!"
          ;;
     esac
     # specify the nodes if assigned by user
     # [ "$LIST_HOST" = "on" ] && set_specific_host_pxe_conf `cat $HOSTS_TMP`
     ;;

  "clonezilla-stop")
     echo -n "Trying to stop the clonezilla mode..."
     stop_ocs_if_necessary $IP_LIST
     # specify the nodes if assigned by user
     # [ "$LIST_HOST" = "on" ] && set_specific_host_pxe_conf `cat $HOSTS_TMP`
     echo "done!"
     ;;

  "local")
         echo -n "Setting client as local mode..."
         stop_ocs_if_necessary $IP_LIST
         $DRBL_SCRIPT_PATH/sbin/set-default-pxe-img -i local -c $PXE_CONF
	 # set it as the default one... Should fix it in the future when I get
	 # the idea how to setup the local boot in etherboot
	 # set_drbl_default_nbi

         # specify the nodes if assigned by user
         [ "$LIST_HOST" = "on" ] && set_specific_host_pxe_conf `cat $HOSTS_TMP`

	 echo "done!"
	 ;;
  "reboot")
	 # TODO
         echo "Reboot DRBL clients now..."
	 echo "For DRBL clients..."
         if [ "$LIST_HOST" = "on" ]; then
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -b -u root -h "$IP_LIST" /sbin/reboot
         else
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -b -u root /sbin/reboot
	 fi
	 echo "For Cygwin clients..."
         if [ "$LIST_HOST" = "on" ]; then
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -b -u administrator -h "$IP_LIST" /usr/bin/shutdown -f -r now
         else
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -b -u administrator /usr/bin/shutdown -f -r now
	 fi
	 echo "done!"
	 ;;
  "shutdown")
         echo "Shutdown DRBL clients now..."
	 echo "For DRBL clients..."
         if [ "$LIST_HOST" = "on" ]; then
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -b -u root -h "$IP_LIST" /sbin/poweroff
         else
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -b -u root /sbin/poweroff
         fi
	 echo "For Cygwin clients..."
         if [ "$LIST_HOST" = "on" ]; then
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -b -u administrator -h "$IP_LIST" /usr/bin/shutdown -f -s now
         else
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -b -u administrator /usr/bin/shutdown -f -s now
         fi
	 echo "done!"
	 ;;
  "Wake-on-LAN")
         echo "Turn on DRBL clients by Wake-on-LAN now..."
         if [ "$LIST_HOST" = "on" ]; then
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -b -h "$IP_LIST" --wol
         else
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -b --wol
         fi
	 echo "done!"
	 ;;
  "switch-pxe-menu")
         echo "Switch DRBL client menus..."
	 $DRBL_SCRIPT_PATH/sbin/switch-pxe-menu -c $PXE_CONF -l $lang

	 # specify the nodes if assigned by user
         [ "$LIST_HOST" = "on" ] && set_specific_host_pxe_conf `cat $HOSTS_TMP`
	 ;;
  "login-switch")
         run_login_switch
	 ;;
  "switch-drbl-serv")
         run_switch_drbl_service
	 ;;
  *)
         USAGE
	 ;;
esac
[ -f "$HOSTS_TMP" ] && rm -f $HOSTS_TMP
