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

# Load the setting
. /opt/drbl/sbin/drbl-conf-functions
#
check_if_root

# functions
USAGE() {
  echo "Usage: $0 [Option] [on|off]"
  echo "Options:"
  echo "-n, --no-check-clients    Do NOT check if clients' directories exist in $drblroot."
  echo "-c, --no-create-ssi-template  Do NOT create DRBL single system image template again. Default is to create SSI template when on."
  echo "-t, --template HOST  Use HOST as the template to create etc and var tarballs. If not assigned, $0 will find the first client one in dir $drblroot."
  echo "-x, --exclude-X-config    When creating template, exclude X config."
  echo "-z, --clonezilla-box-mode y/n  Set as clonezilla box mode or not."
  echo "-s, --no-stop-ocs    do NOT stop ocs. (It's better to stop OCS so that the unnecessary services in rc1.d will be removed!)."
  echo "-h, --help           Display this help and exit"
  echo "Example:"
  echo "To turn on the DRBL SSI mode, run:"
  echo "$0 on"
}
# 
set_clonezilla_box_mode() {
  local mode=$1
  echo -n "Setting clonezilla_box_mode=\"$mode\" in /etc/drbl/drbl_deploy.conf and /etc/drbl/drblpush.conf... "
  perl -pi -e "s/^[[:space:]]*clonezilla_box_mode=.*/clonezilla_box_mode=\"$mode\"/g" /etc/drbl/drbl_deploy.conf
  perl -pi -e "s/^[[:space:]]*clonezilla_box_mode=.*/clonezilla_box_mode=$mode/g" /etc/drbl/drblpush.conf
  echo "done!"
}
#
set_drbl_ssi_mode() {
  local mode=$1
  echo -n "Setting drbl_ssi_mode=\"$mode\" in /etc/drbl/drbl_deploy.conf and /etc/drbl/drblpush.conf... "
  perl -pi -e "s/^[[:space:]]*drbl_ssi_mode=.*/drbl_ssi_mode=\"$mode\"/g" /etc/drbl/drbl_deploy.conf
  perl -pi -e "s/^[[:space:]]*drbl_ssi_mode=.*/drbl_ssi_mode=$mode/g" /etc/drbl/drblpush.conf
  echo "done!"
}
turn_on_drbl_ssi_mode() {
  PXE_CONF="$PXELINUX_DIR/default"
  lines=$(get_pxecfg_image_block drbl $PXE_CONF)
  begin_line=$(echo $lines | awk -F" " '{print $1}')
  end_line=$(echo $lines | awk -F" " '{print $2}')
  tag_found="$(head -n $end_line $PXE_CONF | tail -n $(($end_line-$begin_line)) | grep -Ei "^[[:space:]]*append[[:space:]]*.*clientdir=node_root")"
  if [ -z "$tag_found" ]; then
    sub_menu_label_cmd="if ($begin_line..$end_line) {s/(^[[:space:]]*append[[:space:]]+.*)/\$1 clientdir=node_root/gi}"
    perl -pi -e "$sub_menu_label_cmd" $PXE_CONF
  fi
}

turn_off_drbl_ssi_mode() {
  PXE_CONF="$PXELINUX_DIR/default"
  lines=$(get_pxecfg_image_block drbl $PXE_CONF)
  begin_line=$(echo $lines | awk -F" " '{print $1}')
  end_line=$(echo $lines | awk -F" " '{print $2}')
  sub_menu_label_cmd="if ($begin_line..$end_line) {s/(^[[:space:]]*append[[:space:]]+.*)[[:space:]]+clientdir=node_root(.*)/\$1\$2/gi}"
  perl -pi -e "$sub_menu_label_cmd" $PXE_CONF
}

#
create_drbl_ssi_template() {
  # if template is not assigned, find one.
  if [ -z "$template" ]; then
    for ih in $drblroot/*; do
      # use the 1st one drbl client we found as template
      if [ -d "$ih" ]; then
        template="$(basename $ih)"
        break
      fi
    done
  fi
  echo "Creating template for DRBL SSI..."
  [ "$exclude_template_X_conf" = "yes" ] && gen_ssi_extra_opt="-x"
  [ "$stop_ocs" = "no" ] && gen_ssi_extra_opt="$gen_ssi_extra_opt --no-stop-ocs"
  $DRBL_SCRIPT_PATH/sbin/gen_ssi_files -t $template $gen_ssi_extra_opt
}

#
check_drbl_normal_clients() {
  client_ip_lists="$($DRBL_SCRIPT_PATH/bin/get-client-ip-list)"
  for ih in $client_ip_lists; do
    if [ ! -d $drblroot/$ih/etc ]; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "$drblroot/$ih does NOT exist!!! You have to run \"$DRBL_SCRIPT_PATH/sbin/drblpush -i\" again. We can NOT turn off the DRBL SSI mode!"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo "Program terminated!!!"
      exit 1
    fi
  done
}

# default setting
template=""
check_clients_files="yes"
create_ssi_template="yes"
stop_ocs="yes"

# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    -n|--no-stop-ocs)
	    stop_ocs="no"
	    shift;;
    -n|--no-check-clients)
	    check_clients_files="no"
	    shift;;
    -c|--no-create-ssi-template)
	    create_ssi_template="no"
	    shift;;
    -x|--exclude-X-config)
	    exclude_template_X_conf="yes"
	    shift;;
    -t|--template)
 	    shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
	      template="$1"
	      shift
            fi
	    ;;
    -z|--clonezilla-box-mode)
 	    shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
	      clonezilla_box_mode="$1"
	      shift
            fi
	    ;;
    -h|--help)  
            USAGE >& 2
            exit 2 ;;
    -*)     echo "${0}: ${1}: invalid option" >&2
            USAGE >& 2
            exit 2 ;;
    *)      break ;;
  esac
done

switch=$1

# format the clonezilla_box_mode option
case "$clonezilla_box_mode" in
  [yY]|[yY][eE][Ss])
    clonezilla_box_mode="yes"
    ;;
  n|N|[nN][oO])
    clonezilla_box_mode="no"
    ;;
  *)
    clonezilla_box_mode=""
    ;;
esac

[ -z "$switch" ] && USAGE && exit 1

#
case "$switch" in
  [oO][nN])
      echo "Turn on the DRBL SSI mode in pxelinux config... "
      turn_on_drbl_ssi_mode
      set_drbl_ssi_mode yes
      [ "$create_ssi_template" = "yes" ] && create_drbl_ssi_template
      [ -n "$clonezilla_box_mode" ] && set_clonezilla_box_mode $clonezilla_box_mode
      echo "done!"
      ;;
  [oO][fF][fF])
      [ "$check_clients_files" = "yes" ] && check_drbl_normal_clients
      echo "Turn off the DRBL SSI mode in pxelinux config... "
      turn_off_drbl_ssi_mode
      set_drbl_ssi_mode no
      # if drbl_ssi_mode is off, then clonezilla_box_mode must be no.
      [ -n "$clonezilla_box_mode" ] && set_clonezilla_box_mode $clonezilla_box_mode
      echo "done!"
      ;;
esac
