#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw> and Blake, Kuo-Lien Huang <klhaung _at_ gmail.com>. 
# License: GPL 
# Description: DRBL utitilty to switch the mode for clients.

# 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

# Load some necessary setting from drbl-ocs.conf
. /opt/drbl/conf/drbl-ocs.conf

# Load basic functions of ocs
. /opt/drbl/sbin/ocs-functions

# Load the preset mode
# we need to know $drbl_ssi_mode...
[ -f "/etc/drbl/drbl_deploy.conf" ] && . /etc/drbl/drbl_deploy.conf

#
# DIA (="dialog") is loaded from drbl.conf
MAX_DIALOG_HEIGHT="12"

#
check_if_root

# get the OS_Version and OS_type
check_distribution_name

# load user's default setting
[ -e "$HOME/.dcsrc" ] && . $HOME/.dcsrc

# function
USAGE() {
   echo "Usage: $0 [OPTION] MODE [MODE2]"
   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-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 "terminal              Remote display Linux, terminal mode."
   echo "remote-memtest        Remote boot to run memtest86"
   echo "remote-fdos           Remote boot to run FreeDOS"
   echo "clonezilla-start      Start Clonezilla 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 "drbl-ssi              Switch the DRBL Single System Image mode" 
   echo "login-switch          Switch DRBL client login mode"
   echo 
   echo "MODE2:"
   echo "Based on MODE, assign the 2nd level mode:"
   echo "For MODE=clonezilla-start, available MODE2:"
   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 "For MODE=others, available MODE2:"
   echo "switch-drbl-serv      Start, stop or restart DRBL related services in DRBL server"
   echo "client-reautologin    Force to restart client autologin" 
   echo "clean-autologin-account Delete all autologin accounts" 
   echo "re-deploy             Re-deploy files to clients, i.e. update the files of server to clients."
   echo "clean-dhcpd-lease     Force to reset DHCP IP address leases" 
   echo 
   echo "If no argument is specified, a dialog will appear to let you choose the mode!"
}

# 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 "$ocs_lock_dir/clonezilla.lock" ]; then
    echo "Your system is already in clonezilla mode... we will stop it first, then change to other mode..."
   # Now clonezilla can not be multi-image, so it will be a mess if just
   # stop some clients. Therefore if IP_LIST exists, we still stop 
   # all the clonezilla process.
   # if [ -n "$IP_LIST" ]; then 
   #   $DRBL_SCRIPT_PATH/sbin/drbl-ocs -n $OCS_OPTS -h "$IP_LIST" -l $lang stop
   # else
   #   $DRBL_SCRIPT_PATH/sbin/drbl-ocs -n $OCS_OPTS -l $lang stop
   # fi
   $DRBL_SCRIPT_PATH/sbin/drbl-ocs -n $OCS_OPTS -l $lang stop
  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
  $DIA --separate-output --backtitle "$msg_nchc_free_software_labs" --title  \
  "$msg_clonezilla_advanced_extra_param" --checklist "$msg_choose_param_to_set:" \
  0 0 0 \
  "-g auto"  "$msg_ocs_param_g_auto" on \
  "-x"       "$msg_ocs_param_x" on \
  "-p true"  "$msg_ocs_param_p_true" off \
  "-p poweroff"  "$msg_ocs_param_p_poweroff" off \
  "-hn0 $WIN_HOSTNAME_PREFIX"  "$msg_ocs_param_hn0" off \
  "-hn1 $WIN_HOSTNAME_PREFIX"  "$msg_ocs_param_hn1" off \
  "-v"       "$msg_ocs_param_v" off \
  "-nogui"   "$msg_ocs_param_nogui" off \
  "-c"       "$msg_ocs_param_c" off \
  "-u"       "$msg_ocs_param_u" off \
  "-y0"      "$msg_ocs_param_y0" off \
  "-y1"      "$msg_ocs_param_y1" off \
  "-t"       "$msg_ocs_param_t" off \
  "-j0"      "$msg_ocs_param_j0" off \
  "-ns"      "$msg_ocs_param_ns" off \
  "-k"       "$msg_ocs_param_k" off \
  "-r"       "$msg_ocs_param_r" off \
  "-e"       "$msg_ocs_param_e" off \
  "-f"       "$msg_ocs_param_f" off \
  "-s"       "$msg_ocs_param_s" off \
  "-a"       "$msg_ocs_param_a" off \
  "-o"       "$msg_ocs_param_o" off \
  2> $OCS_PARAM_TMP
else
  # save
  $DIA --separate-output --backtitle "$msg_nchc_free_software_labs" --title  \
  "$msg_clonezilla_advanced_extra_param" --checklist "$msg_choose_param_to_set:" \
  0 0 0 \
  "-q"       "$msg_ocs_param_q" on \
  "-p true"  "$msg_ocs_param_p_true" off \
  "-nogui"   "$msg_ocs_param_nogui" off \
  "-a"       "$msg_ocs_param_a" off \
  "-f"       "$msg_ocs_param_f" off \
  "-s"       "$msg_ocs_param_s" off \
  2> $OCS_PARAM_TMP
fi

# if -hn0|-hn1 is chosen, 
# (1). prompt the warning about ntfs-3g/nfsmount for ntfs is necessary
# (2). ask if the hostname prefix want to change
if grep -qE "\-hn[01]" $OCS_PARAM_TMP; then
  # part 1.
  $DIA --title "$msg_change_hostname_of_MS_WIN_on_the_fly" --clear \
       --msgbox "$msg_write_MS_WIN_is_necessary" 15 70

  # part 2.
  HNTMP="$(mktemp /tmp/winhn.XXXXXX)"
  $DIA --backtitle "$msg_nchc_free_software_labs" --title "$msg_nchc_clonezilla" \
  --inputbox "$msg_What_the_win_hostname_prefix ?" 0 0 "$WIN_HOSTNAME_PREFIX" 2> $HNTMP
  HN_PREFIX="$(cat $HNTMP)"
  if [ -z "$HN_PREFIX" ]; then
     echo "You did not specify the hostname prefix for M$ windows! We use the default value in drbl-ocs.conf"
  else
     perl -pi -e "s/(-hn.*) $WIN_HOSTNAME_PREFIX/\$1 $HN_PREFIX/g" $OCS_PARAM_TMP
  fi
  [ -f "$HNTMP" ] && rm -f $HNTMP
fi

if [ "$mode" = "save" ]; then
  # save
  $DIA --backtitle "$msg_nchc_free_software_labs" --title  \
  "$msg_clonezilla_advanced_extra_param" --radiolist "$msg_choose_one_compression_param_to_save:" \
  0 0 0\
  "-z0"      "$msg_ocs_param_z0" off \
  "-z1"      "$msg_ocs_param_z1" off \
  "-z2"      "$msg_ocs_param_z2" off \
  "-z3"      "$msg_ocs_param_z3" on \
  2>> $OCS_PARAM_TMP
fi
} # end of ask_if_skip_set_ocs_extra_param

#
ask_if_input_save_image_name_in_server() {
  local save_type=$1
$DIA --title "$msg_title_input_save_image_name_in_server" \
     --yesno "$msg_ask_if_input_save_image_name_in_server" 0 0 

case $? in
  0)
    input_save_image_name_in_server="yes";;
  1)
    input_save_image_name_in_server="no";;
  255)
    echo "Box closed.";;
esac

  if [ "$input_save_image_name_in_server" = "yes" ]; then
    local ANS_TMP=`mktemp /tmp/ocs_ans.XXXXXX`
    trap "[ -f "$ANS_TMP" ] && rm -f $ANS_TMP" HUP INT QUIT TERM EXIT
    get_input_image_name $ANS_TMP
    input_save_img_name="$(cat $ANS_TMP)"
    [ -f "$ANS_TMP" ] && rm -f $ANS_TMP

    local TMP=`mktemp /tmp/ocs_name.XXXXXX`
    local dev_example
    local ASK_DEV_NAME=1
    trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM EXIT
    case "$save_type" in
     "savedisk")
           dev_example="hda $msg_or sda"
	   dev_preset="hda"
           ;;
     "saveparts")
           dev_example="'hda1 hda2' $msg_or 'sda1 sda2'"
           dev_preset="hda1 hda2"
           ;;
    esac
    while [ "$ASK_DEV_NAME" -ne 0 ]; do
      $DIA --backtitle "$msg_nchc_free_software_labs" --title "$msg_nchc_clonezilla" \
      --inputbox "$msg_input_device_name ($msg_ex: $dev_example)" 0 0 "$dev_preset" 2> $TMP
      input_device_name="$(cat $TMP)"
      if [ -z "$input_device_name" ]; then
        $DIA --backtitle "$msg_nchc_free_software_labs" --title "$msg_nchc_clonezilla" \
        --msgbox "$msg_you_must_input_device_name_to_save_image!\n$msg_please_do_it_again!!!" 0 0 
      else
        ASK_DEV_NAME=0
      fi
    done
    [ -f "$TMP" ] && rm -f $TMP
  fi
}

#
run_login_switch() {
  TMP=$(mktemp /tmp/menu.XXXXXX)
  trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM 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
  # If the mode is autologin or timed login graphic mode, we should switch to graphic mode first.
  if [ "$action" = "--auto" -o "$action" = "--timed" ]; then
    echo "Set the client as remote-linux graphic mode..."
    if [ "$LIST_HOST" = "on" ]; then
      $DRBL_SCRIPT_PATH/sbin/drbl-client-switch -l $lang -h "$IP_LIST" remote-linux-gra
    else
      $DRBL_SCRIPT_PATH/sbin/drbl-client-switch -l $lang -nl remote-linux-gra
    fi
  fi
  $DRBL_SCRIPT_PATH/sbin/drbl-login-switch $action -l $lang
} # end of run_login_switch
#
run_drbl_ssi_switch() {
  TMP=$(mktemp /tmp/menu.XXXXXX)
  trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM EXIT
  echo "Switch DRBL client image mode"
  $DIA --backtitle "$msg_nchc_free_software_labs" --title  \
  "$msg_drbl_ssi_switch" --radiolist "$msg_drbl_ssi_switch_long" \
  0 0 0 \
  "on"  "$msg_drbl_ssi_on" off \
  "off" "$msg_drbl_ssi_off" off \
  2> $TMP
  action="$(cat $TMP)"
  [ -f "$TMP" ] && rm -f $TMP
  [ -z "$action" ] && echo "You must specify the action! Program terminated!!!" && exit 1
  # Ask if clonezilla box mode is used or not ?
  TMP2=$(mktemp /tmp/menu2.XXXXXX)
  trap "[ -f "$TMP2" ] && rm -f $TMP2" HUP INT QUIT TERM EXIT
  echo "Turn on clonezilla box mode, too ?"
  $DIA --backtitle "$msg_nchc_free_software_labs" --title  \
  "$msg_clonezilla_box_mode_switch" --radiolist "$msg_clonezilla_box_mode_switch_long" \
  0 0 0 \
  "yes"  "$msg_clonezilla_box_mode_yes" off \
  "no" "$msg_clonezilla_box_mode_no" off \
  2> $TMP2
  clonezilla_box_mode_opt="$(cat $TMP2)"
  [ -f "$TMP2" ] && rm -f $TMP2
  [ -z "$clonezilla_box_mode_opt" ] && echo "You must specify the clonezilla_box_mode_opt! Program terminated!!!" && exit 1
  [ -n "$clonezilla_box_mode_opt" ] && clonezilla_box_mode_opt="-z $clonezilla_box_mode_opt"
  $DRBL_SCRIPT_PATH/sbin/turn-drbl-ssi-mode $clonezilla_box_mode_opt $action
} # end of run_drbl_ssi_switch

do_clonezilla_save_dev() {
  # dev is: disk or parts
  local dev="$1"
  [ -z "$dev" ] && echo "You must specify the device type (parts or disk)!!! Program terminated!!!" && exit 1

  echo -n "Setting client as clonezilla-save-${dev} mode..."

  # ask if want to input the saved name now
  ask_if_input_save_image_name_in_server save${dev}

  # 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 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
  if [ "$LIST_HOST" = "on" ]; then
    $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -h "$IP_LIST" -l $lang start${dev} save $input_save_img_name "$input_device_name"
    deploy_pxecfg_files
    [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
    echo PS. $msg_run_drbl_ocs_again_cmd 
    echo $DRBL_SCRIPT_PATH/sbin/drbl-ocs -b $OCS_OPTS -h \"$IP_LIST\" -l $lang start${dev} save $input_save_img_name \"$input_device_name\"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  else
    $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -l $lang start${dev} save $input_save_img_name "$input_device_name"
    deploy_pxecfg_files
    [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
    echo PS. $msg_run_drbl_ocs_again_cmd 
    echo $DRBL_SCRIPT_PATH/sbin/drbl-ocs -b $OCS_OPTS -l $lang start${dev} save $input_save_img_name \"$input_device_name\"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  fi
  echo "done!"
} # end of do_clonezilla_save_dev

#
do_clonezilla_restore_dev() {
  # dev is: disk or parts
  local dev="$1"
  local target_image
  [ -z "$dev" ] && echo "You must specify the device type (parts or disk)!!! Program terminated!!!" && exit 1
  # We won't provide the method to change the mount point to replace /home/partimag in dcs, so we fix the imagedir="$ocsroot" (/home/partimag). However, user still can use drbl-ocs to do that.
  #
  # change to other mount point for extra harddisk
  #if [ -n "$mount_point" ]; then
  #   imagedir="$mount_point" 
  #   echo "Using the mount point $imagedir."
  #else
  #   imagedir="$ocsroot"
  #fi
  imagedir="$ocsroot"

  echo -n "Setting client as clonezilla-restore-$dev 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 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

  if [ -n "$(echo $OCS_OPTS | grep -E "\-[u]")" ]; then
    # with -u, we will choose the image in the client, so 
    # 1. do not ask image name here.
    # 2. only unicast available.
    [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
    echo "$msg_ocs_param_exclude_multicast"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    RESTORE_MODE="unicast"
  else
    # if no -u, we have to ask image name here.
    # get the image
    ANS_TMP=`mktemp /tmp/ocs_ans.XXXXXX`
    trap "[ -f "$ANS_TMP" ] && rm -f $ANS_TMP" HUP INT QUIT TERM EXIT
    get_existing_${dev}_image $ANS_TMP
    # the return name will be only one image name.
    target_image="$(cat $ANS_TMP)"
    [ -f "$ANS_TMP" ] && rm -f $ANS_TMP
    # Note! We won't add leading $ocsroot in $target_image, because when task_saveparts do the real job later, it will add that.
    
    # If it's partitions, we have to choose the partitions.
    case "$dev" in
     "disk")
        target_dev=$(cat $imagedir/$target_image/disk 2>/dev/null)
        ;;
     "parts")
        ANS_TMP=`mktemp /tmp/ocs_ans.XXXXXX`
        trap "[ -f "$ANS_TMP" ] && rm -f $ANS_TMP" HUP INT QUIT TERM EXIT
        get_existing_partitions $ANS_TMP $target_image
        # we have to remove " (comes with checklist in dialog) so that for loop
        # will work (Specially for FC3/4...)
        target_dev="$(cat $ANS_TMP | tr -d \")"
        [ -f "$ANS_TMP" ] && rm -f $ANS_TMP
        ;;
    esac

    # get the mode of unicast of multicast
    eval msg_choose_the_mode_to_restore_dev=\$msg_choose_the_mode_to_restore_$dev
    TMP=$(mktemp /tmp/menu.XXXXXX)
    trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM EXIT
    $DIA \
    --backtitle "$msg_nchc_title" \
    --title "$msg_nchc_clonezilla" \
    --radiolist "$msg_choose_the_mode_to_restore_dev" 0 0 0 \
    "multicast" "$msg_multicast_restore" on \
    "unicast" "$msg_unicast_restore" off \
    2> $TMP
    RESTORE_MODE=$(cat $TMP) 
    [ -f "$TMP" ] && rm -f $TMP
  fi

  [ -z "$RESTORE_MODE" ] && echo "No mode is selected! Program terminated!!!" && exit 1
  if [ "$RESTORE_MODE" = "unicast" ]; then
    if [ "$LIST_HOST" = "on" ]; then
      # NOTE: To prompt user, we must use \" to show ", but actually run it without \
      $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -h "$IP_LIST" -l $lang start${dev} restore $target_image "$target_dev"
      deploy_pxecfg_files
      [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
      echo PS. $msg_run_drbl_ocs_again_cmd 
      # NOTE: To prompt user, we must use \" to show ", but actually run it without \
      echo $DRBL_SCRIPT_PATH/sbin/drbl-ocs -b $OCS_OPTS -h \"$IP_LIST\" -l $lang start${dev} restore $target_image \"$target_dev\"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    else
      # NOTE: To prompt user, we must use \" to show ", but actually run it without \
      $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS -l $lang start${dev} restore $target_image "$target_dev"
      deploy_pxecfg_files
      [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
      echo PS. $msg_run_drbl_ocs_again_cmd 
      # NOTE: To prompt user, we must use \" to show ", but actually run it without \
      echo $DRBL_SCRIPT_PATH/sbin/drbl-ocs -b $OCS_OPTS -l $lang start${dev} restore $target_image \"$target_dev\"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    fi
  else
    # multicast
    # Get the multicast options: time_to_wait, clients_to_wait...
    mcast_mode_opt=""
    ANS_TMP=`mktemp /tmp/ocs_ans.XXXXXX`
    trap "[ -f "$ANS_TMP" ] && rm -f $ANS_TMP" HUP INT QUIT TERM EXIT
    ask_time_or_clients_to_wait $ANS_TMP
    . $ANS_TMP
    # we will get time_to_wait or client_to_want.
    [ -n "$time_to_wait" ] && mcast_mode_opt="$mcast_mode_opt --time-to-wait $time_to_wait"
    [ -n "$clients_to_wait" ] && mcast_mode_opt="$mcast_mode_opt --clients-to-wait $clients_to_wait"
    [ -n "$max_time_to_wait" ] && mcast_mode_opt="$mcast_mode_opt --max-time-to-wait $max_time_to_wait"
    [ -f "$ANS_TMP" ] && rm -f $ANS_TMP

    stop_ocs_if_necessary

    if [ "$LIST_HOST" = "on" ]; then
      multicast_clonezilla_restore_warning
      # NOTE: To prompt user, we must use \" to show ", but actually run it without \
      $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS $mcast_mode_opt -h "$IP_LIST" -l $lang start${dev} multicast_restore $target_image "$target_dev"
      deploy_pxecfg_files
      [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
      echo PS. $msg_run_drbl_ocs_again_cmd 
      # NOTE: To prompt user, we must use \" to show ", but actually run it without \
      echo $DRBL_SCRIPT_PATH/sbin/drbl-ocs -b $OCS_OPTS $mcast_mode_opt -h \"$IP_LIST\" -l $lang start${dev} multicast_restore $target_image \"$target_dev\"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    else
      # NOTE: To prompt user, we must use \" to show ", but actually run it without \
      $DRBL_SCRIPT_PATH/sbin/drbl-ocs $OCS_OPTS $mcast_mode_opt -l $lang start${dev} multicast_restore $target_image "$target_dev"
      deploy_pxecfg_files
      [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
      echo PS. $msg_run_drbl_ocs_again_cmd 
      # NOTE: To prompt user, we must use \" to show ", but actually run it without \
      echo $DRBL_SCRIPT_PATH/sbin/drbl-ocs -b $OCS_OPTS $mcast_mode_opt -l $lang start${dev} multicast_restore $target_image \"$target_dev\"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    fi
  fi
  echo "done!"
} # end of do_clonezilla_restore_dev

#
run_switch_drbl_service() {
  TMP=$(mktemp /tmp/menu.XXXXXX)
  trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM 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 \
  "add"      "$msg_add_drbl_all_services" off \
  "del"      "$msg_del_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
#
run-switch-pxe-bg-mode() {
  TMP=$(mktemp /tmp/pxemenu.XXXXXX)
  trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM EXIT
  echo "Switch DRBL client PXE boot menu mode"
  $DIA --backtitle "$msg_nchc_free_software_labs" --title  \
  "$msg_switch_pxe_bg_mode" --radiolist "$msg_switch_pxe_bg_mode" \
  0 0 0 \
  "text"  "$msg_pxemenu_text" text \
  "graphic" "$msg_pxemenu_graphic" graphic \
  2> $TMP
  action="$(cat $TMP)"
  [ -f "$TMP" ] && rm -f $TMP
  [ -z "$action" ] && echo "You must specify the mode! Program terminated!!!" && exit 1
  $DRBL_SCRIPT_PATH/sbin/switch-pxe-bg-mode -m $action
} # end of run-switch-pxe-bg-mode 
#
clean_stale_node_pxe_cfg(){
  # $PXE_CONF is global variable.
  # 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 PXELINUX config file if they exist..."
  for ifile in $PXELINUX_DIR/*; do
    [ "$ifile" != "$PXE_CONF" -a -f "$ifile" ] && rm -f $ifile
  done
  echo "done!"
} # end of clean_stale_node_pxe_cfg
#
deploy_pxecfg_files(){
  # This function will set the specific config or just keep the "default"
  if [ "$LIST_HOST" = "on" ]; then
    # specify the nodes if assigned by user
    echo "Create specific config for PXE client."
    set_specific_host_pxe_conf `cat $HOSTS_TMP`
  else
    # for all clients, so we just keep the file pxelinux.cfg/default, no others.
    echo "This is for all clients, so we remove other host-based PXE config files in $PXELINUX_DIR/ and keep $PXE_CONF only."
    clean_stale_node_pxe_cfg
  fi
} # end of deploy_pxecfg_files

# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    -l|--language)
	shift
        if [ -z "$(echo $1 |grep ^-.)" ]; then
          # skip the -xx option, in case 
	  specified_lang="$1"
	  shift
        fi
	;;
    -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"
# We havw two levels, like clonezilla-start clonezilla-save-disk
MODE2="$2"

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 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 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/sbin/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"
fi
# If clonezilla box mode, we have to suppress some functions
[ -f /etc/drbl/drbl_deploy.conf ] && . /etc/drbl/drbl_deploy.conf
if [ "$clonezilla_box_mode" != "yes" ]; then
   # "remote-linux-gra" "$msg_remote_linux_graphic" off \
   remote_linux_gra_1="remote-linux-gra"
   remote_linux_gra_2="$msg_remote_linux_graphic"
   remote_linux_gra_3="off"
   # "remote-linux-txt" "$msg_remote_linux_text" off \
   remote_linux_txt_1="remote-linux-txt"
   remote_linux_txt_2="$msg_remote_linux_text"
   remote_linux_txt_3="off"
   # "terminal" "$msg_terminal" off \
   terminal_1="terminal"
   terminal_2="$msg_thin_client"
   terminal_3="off"
   # "login-switch" "$msg_login_switch" off \
   login_switch_1="login-switch"
   login_switch_2="$msg_login_switch"
   login_switch_3="off"
   # "client-reautologin" "$msg_client_reautologin" off \
   client_reautologin_1="client-reautologin"
   client_reautologin_2="$msg_client_reautologin"
   client_reautologin_3="off"
   # "clean-autologin-account" "$msg_clean_autologin_account" off \
   clean_autologin_account_1="clean-autologin-account"
   clean_autologin_account_2="$msg_clean_autologin_account"
   clean_autologin_account_3="off"
   # "reset-autologin-account" "$msg_reset_autologin_account" off \
   reset_autologin_account_1="reset-autologin-account"
   reset_autologin_account_2="$msg_reset_autologin_account"
   reset_autologin_account_3="off"
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 EXIT
     $DIA \
     --backtitle "$msg_nchc_title" \
     --title "$msg_nchc_drbl" \
     --no-shadow \
     --radiolist "$msg_switch_client_mode:" 0 0 0 \
     $remote_linux_gra_1 $remote_linux_gra_2 $remote_linux_gra_3 \
     $remote_linux_txt_1 $remote_linux_txt_2 $remote_linux_txt_3 \
     $terminal_1 $terminal_2 $terminal_3 \
     "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 \
     "drbl-ssi" "$msg_drbl_ssi_switch" off \
     $login_switch_1 $login_switch_2 $login_switch_3 \
     "others" "$msg_dcs_others" off \
     2> $TMP
     MODE=$(cat $TMP)
     [ -f "$TMP" ] && rm -f $TMP
fi

# set the mode for PXE client
case "$MODE" in
  "remote-linux-gra") 
         echo "Settinig client as remote-linux graphic mode..."
         stop_ocs_if_necessary
         $DRBL_SCRIPT_PATH/sbin/set-default-pxe-img -i drbl -c $PXE_CONF -l "$FULL_OS_Version $powerful_client_menu_label"
         if [ "$LIST_HOST" = "on" ]; then
           # now use --no-gen-ssi, but we will run gen_ssi_files when finished
	   $DRBL_SCRIPT_PATH/sbin/drbl-powerful-thin-client --no-gen-ssi -p -h "$IP_LIST" -ln $lang
         else
           # now use --no-gen-ssi, but we will run gen_ssi_files when finished
	   $DRBL_SCRIPT_PATH/sbin/drbl-powerful-thin-client --no-gen-ssi -p -ln $lang
         fi

	 deploy_pxecfg_files

         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

	   # 
           host_ip=$(basename $host)
           echo "Setting the remote-linux graphic mode for node IP = $host_ip"
	   if [ -e /etc/debian_version ]; then
             # Debian
             if [ -e /etc/b2d-release ]; then
	       # For B2D, it uses runlevel 5 for X 
               CLIENT_INIT="5"
             else
	       # For Debian, Ubuntu, it uses runlevel 2 for X 
               CLIENT_INIT="2"
             fi
             default_dm="$($DRBL_SCRIPT_PATH/bin/check_dm -h $host_ip)"
             prepare_update_rc_d_env $host
             LC_ALL=C chroot $host/ /usr/sbin/update-rc.d $default_dm start 99 2 3 4 5 . stop 05 0 1 6 . &> /dev/null
           else
             # RH-like or SUSE
             # set the init to 5, i.e. default is X
             CLIENT_INIT="5"
           fi
	   # Modify client's /etc/inittab
           perl -p -i -e "s/^id:[0-9]:initdefault:/id:$CLIENT_INIT:initdefault:/g" $host/etc/inittab
         done
	 echo "done!"
         #
         echo "-------------------------------------------------------"
         if [ "$drbl_ssi_mode" = "yes" ]; then
           echo "Since it's in DRBL SSI mode, and some config files are modified in template client, creating template tarball for DRBL SSI..."
           $DRBL_SCRIPT_PATH/sbin/gen_ssi_files
         fi
         ;;
  "remote-linux-txt") 
         echo "Settinig client as remote-linux text mode..."
         stop_ocs_if_necessary
         $DRBL_SCRIPT_PATH/sbin/set-default-pxe-img -i drbl -c $PXE_CONF -l "$FULL_OS_Version $powerful_client_menu_label"
         if [ "$LIST_HOST" = "on" ]; then
           # now use --no-gen-ssi, but we will run gen_ssi_files when finished
	   $DRBL_SCRIPT_PATH/sbin/drbl-powerful-thin-client --no-gen-ssi -p -h "$IP_LIST" -ln $lang
         else
           # now use --no-gen-ssi, but we will run gen_ssi_files when finished
	   $DRBL_SCRIPT_PATH/sbin/drbl-powerful-thin-client --no-gen-ssi -p -ln $lang
	 fi

	 deploy_pxecfg_files

         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

           # 
           host_ip=$(basename $host)
           echo "Setting the remote-linux text mode for node IP = $host_ip"
	   if [ -e /etc/debian_version ]; then
             CLIENT_INIT="2"
             default_dm="$($DRBL_SCRIPT_PATH/bin/check_dm -h $host_ip)"
             prepare_update_rc_d_env $host
             LC_ALL=C chroot $host/ /usr/sbin/update-rc.d -f $default_dm remove &> /dev/null
           else
             # RH-like or SUSE
             # set the init to 3, i.e. default is text mode
             CLIENT_INIT="3"
           fi
           perl -p -i -e "s/^id:[0-9]:initdefault:/id:$CLIENT_INIT:initdefault:/g" $host/etc/inittab
         done
	 echo "done!"
         #
         echo "-------------------------------------------------------"
         if [ "$drbl_ssi_mode" = "yes" ]; then
           echo "Since it's in DRBL SSI mode, and some config files are modified in template client, creating template tarball for DRBL SSI..."
           $DRBL_SCRIPT_PATH/sbin/gen_ssi_files
         fi
         ;;
  "remote-memtest")
         echo -n "Setting client as remote-memtest mode..."
         stop_ocs_if_necessary
         $DRBL_SCRIPT_PATH/sbin/set-default-pxe-img -i memtest -c $PXE_CONF
	 deploy_pxecfg_files
	 echo "done!"
	 ;;
  "remote-fdos")
         echo -n "Setting client as remote-fdos mode..."
         stop_ocs_if_necessary
         $DRBL_SCRIPT_PATH/sbin/set-default-pxe-img -i fdos -c $PXE_CONF
	 deploy_pxecfg_files
	 echo "done!"
	 ;;
  "terminal")
         echo -n "Settinig client as DRBL terminal mode..."
         stop_ocs_if_necessary
         $DRBL_SCRIPT_PATH/sbin/set-default-pxe-img -i drbl-terminal -c $PXE_CONF -l "$FULL_OS_Version $thin_client_menu_label"
	 deploy_pxecfg_files

         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 EXIT
     netinstall_list="$(grep -Ei "^[[:space:]]*label[[:space:]]+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: netinstall-ubuntu-breezy-i386
        # create the description for the netinstall image
        des="$(echo $i | sed -e "s/^netinstall-//gi" | 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
     echo -n "Set the PXE environment..."
     $DRBL_SCRIPT_PATH/sbin/set-default-pxe-img -i $OS_netinstall -c $PXE_CONF
     echo "done!"
     deploy_pxecfg_files
     ;;
  "clonezilla-start")
     if [ -z "$MODE2" ]; then
       TMP=$(mktemp /tmp/menu.XXXXXX)
       trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM EXIT
       $DIA \
       --backtitle "$msg_nchc_title" \
       --title "$msg_start_clonezilla_mode" \
       --radiolist "$msg_choose_mode ($msg_abandon_hda1)" 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 \
       2> $TMP
       ocs_mode=$(cat $TMP)
       [ -f "$TMP" ] && rm -f $TMP
     else
       ocs_mode="$MODE2"
     fi
     [ -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")
          do_clonezilla_save_dev disk
          ;;
       "clonezilla-restore-disk")
          do_clonezilla_restore_dev disk
          ;;
       "clonezilla-save-parts")
          do_clonezilla_save_dev parts
          ;;
       "clonezilla-restore-parts")
          do_clonezilla_restore_dev parts
          ;;
     esac
     ;;

  "clonezilla-stop")
     echo -n "Trying to stop the clonezilla mode..."
     stop_ocs_if_necessary
     # When clonezilla stops, we should make clients boot locally
     $DRBL_SCRIPT_PATH/sbin/set-default-pxe-img -i local -c $PXE_CONF
     deploy_pxecfg_files
     echo "done!"
     ;;

  "local")
         echo -n "Setting client as local mode..."
         stop_ocs_if_necessary
         $DRBL_SCRIPT_PATH/sbin/set-default-pxe-img -i local -c $PXE_CONF
	 deploy_pxecfg_files
	 echo "done!"
	 ;;
  "reboot")
	 # TODO
         echo "Reboot DRBL clients now..."
	 echo "For DRBL clients..."
         if [ "$LIST_HOST" = "on" ]; then
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -l $lang -b -u root -h "$IP_LIST" /sbin/reboot
         else
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -l $lang -b -u root /sbin/reboot
	 fi
	 echo "For Cygwin clients..."
         if [ "$LIST_HOST" = "on" ]; then
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -l $lang -b -u administrator -h "$IP_LIST" /usr/bin/shutdown -f -r now
         else
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -l $lang -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 -l $lang -b -u root -h "$IP_LIST" /sbin/poweroff
         else
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -l $lang -b -u root /sbin/poweroff
         fi
	 echo "For Cygwin clients..."
         if [ "$LIST_HOST" = "on" ]; then
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -l $lang -b -u administrator -h "$IP_LIST" /usr/bin/shutdown -f -s now
         else
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -l $lang -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 -l $lang -b -h "$IP_LIST" --wol
         else
	   $DRBL_SCRIPT_PATH/bin/drbl-doit -l $lang -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
	 rc=$?
	 [ "$rc" -eq 0 ] && deploy_pxecfg_files
	 ;;
  "drbl-ssi")
	 run_drbl_ssi_switch
	 ;;
  "login-switch")
         run_login_switch
	 ;;
  "others")
         if [ -z "$MODE2" ]; then
           TMP="$(mktemp /tmp/menu.XXXXXX)"
           trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM EXIT
           $DIA \
           --backtitle "$msg_nchc_title" \
           --title "$msg_nchc_drbl" \
           --no-shadow \
           --radiolist "$msg_switch_client_mode:" 0 0 0 \
           "switch-drbl-serv" "$msg_switch_drbl_service" off \
	   $client_reautologin_1 $client_reautologin_2 $client_reautologin_3 \
	   $clean_autologin_account_1 $clean_autologin_account_2 $clean_autologin_account_3 \
	   $reset_autologin_account_1 $reset_autologin_account_2 $reset_autologin_account_3 \
           "re-deploy" "$msg_re_deploy" off \
           "clean-dhcpd-lease" "$msg_clean_dhcpd_lease" off \
           "gen_3N_conf" "$msg_regenerate_nfs_nis_nat_config" off \
           "switch-pxe-bg-mode" "$msg_switch_pxe_bg_mode" off \
           2> $TMP
           OTHER_MODE=$(cat $TMP)
           [ -f "$TMP" ] && rm -f $TMP
         else
           OTHER_MODE=$MODE2
	 fi
	 case "$OTHER_MODE" in
          "switch-drbl-serv")
              run_switch_drbl_service
              ;;
          "client-reautologin")
              if [ "$LIST_HOST" = "on" ]; then
                $DRBL_SCRIPT_PATH/sbin/drbl-client-reautologin -h "$IP_LIST"
              else
                $DRBL_SCRIPT_PATH/sbin/drbl-client-reautologin
              fi
              ;;
          "clean-autologin-account")
              $DRBL_SCRIPT_PATH/sbin/drbl-clean-autologin-account -l $lang
              ;;
          "reset-autologin-account")
              $DRBL_SCRIPT_PATH/sbin/drbl-user-env-reset -l $lang --auto-login
              ;;
          "re-deploy")
              if [ ! -f "$drbl_syscfg/drblpush.conf" ]; then
                [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
                echo "Previously saved config file $drbl_syscfg/drblpush.conf is NOT found. Did you run \"$DRBL_SCRIPT_PATH/sbin/drblpush -i\" before ?"
                echo "Program Terminated!!!"
                [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
                exit 1
              fi
              $DRBL_SCRIPT_PATH/sbin/drblpush --ln $lang -c $drbl_syscfg/drblpush.conf
              ;;
          "clean-dhcpd-lease")
	      $DRBL_SCRIPT_PATH/sbin/clean-dhcpd-lease
	      ;;
          "gen_3N_conf")
	      $DRBL_SCRIPT_PATH/sbin/drbl-3n-conf generate
	      ;;
          "switch-pxe-bg-mode")
	      run-switch-pxe-bg-mode
	      ;;
	 esac
	 ;;
  *)
         USAGE
	 ;;
esac
[ -f "$HOSTS_TMP" ] && rm -f $HOSTS_TMP
