#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Description: Download the Clonezilla live iso for Clonezilla SE environment.

DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/opt/drbl/}"

. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
. $DRBL_SCRIPT_PATH/conf/drbl-ocs.conf
. $DRBL_SCRIPT_PATH/sbin/ocs-functions

# Settings
# URL to get the default Clonezilla live for using in Clonezilla SE.
default_iso_url_for_pxe_ocs_live="http://clonezilla.org/clonezilla-server-edition/clonezilla_live_ver_for_SE.txt"
# By default the mode is to install.
mode="install"

# Functions
USAGE() {
  echo "Prepare the Clonezilla SE (Server Edtion) to use Clonezilla live as the OS of client."
  echo "Usage: $0 [OPTION]"
  echo "OPTION:"
  echo "-i, --iso-url ISO_URL:  Use the ISO_URL (e.g. http://prdownloads.sourceforge.net/clonezilla/clonezilla-live-1.2.5-6-i686.iso) instead of the one assigned in drbl-ocs.conf. $0 will download clonezilla-live from ISO_URL then use the as the OS of client when running Clonezilla job."
  echo "-u, --uninstall:  Uninstall the Clonezilla-live based mode."
  echo "Ex: To use http://prdownloads.sourceforge.net/clonezilla/clonezilla-live-1.2.5-6-i686.iso as the OS of client when running Clonezilla job, run:"
  echo "    $0 -i http://prdownloads.sourceforge.net/clonezilla/clonezilla-live-1.2.5-6-i686.iso"
}
#
put_clonezilla_live() {
  local ocslive_tmp
  ocslive_tmp="$(mktemp -d /tmp/ocslive_tmp.XXXXXX)"
  # iso_url_for_pxe_ocs_live is from drbl-ocs.conf. e.g. 
  # (1) http://prdownloads.sourceforge.net/clonezilla/clonezilla-live-1.2.5-5-i686.iso
  # (2) http://sourceforge.net/projects/clonezilla/files/clonezilla_live_testing/clonezilla-live-1.2.5-5-i686.iso
  if [ -z "$iso_url_for_pxe_ocs_live" ]; then
    # First we try to see if iso_url_for_pxe_ocs_live_default is assigned in drbl-ocs.conf.
    if [ -n "$iso_url_for_pxe_ocs_live_default" ]; then
      iso_url_for_pxe_ocs_live="$iso_url_for_pxe_ocs_live_default"
    fi
    if [ -z "$iso_url_for_pxe_ocs_live" ]; then
      # If not assigned in drbl-ocs.conf, try to find one.
      echo "No Clonezilla live iso was assigned in drbl-ocs.conf or command line option."
      echo "Trying to find the available Clonezilla live iso from $default_iso_url_for_pxe_ocs_live..."
      wget $wget_opt -P "$ocslive_tmp" $default_iso_url_for_pxe_ocs_live
      iso_url_for_pxe_ocs_live="$(LC_ALL=C cat $ocslive_tmp/clonezilla_live_ver_for_SE.txt)"
    fi
  fi
  if [ -z "$iso_url_for_pxe_ocs_live" ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "iso_url_for_pxe_ocs_live _NOT_ found in drbl-ocs.conf, and can not be found from $default_iso_url_for_pxe_ocs_live!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    echo "$msg_program_stop"
    [ -d "$ocslive_tmp" -a -n "$(echo "$ocslive_tmp" | grep "ocslive")" ] && rm -rf $ocslive_tmp
    exit 1
  fi
  ocs_live_iso="$(LC_ALL=C basename $iso_url_for_pxe_ocs_live)"
  
  echo "Downloading the Clonezilla live iso from $iso_url_for_pxe_ocs_live..."
  wget -P "$ocslive_tmp" $iso_url_for_pxe_ocs_live
  rc="$?"
  if [ "$rc" -ne 0 ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "Failed to download Clonezilla live iso from $iso_url_for_pxe_ocs_live!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    echo "$msg_program_stop"
    [ -d "$ocslive_tmp" -a -n "$(echo "$ocslive_tmp" | grep "ocslive")" ] && rm -rf $ocslive_tmp
    exit 1
  fi
  
  # (1) Run drbl-SL.sh to put it on the system.
  echo "Put Clonezilla live in this DRBL server..."
  drbl-SL.sh -b -n -i $ocslive_tmp/$ocs_live_iso
  #
  [ -d "$ocslive_tmp" -a -n "$(echo "$ocslive_tmp" | grep "ocslive")" ] && rm -rf $ocslive_tmp
  # (2) Change drbl-ocs.conf as clonezilla-live based Clonezilla SE
  echo "Modifying option diskless_client_os in drbl-ocs.conf..."
  LC_ALL=C perl -pi -e 's/^[[:space:]]*diskless_client_os=.*/diskless_client_os="clonezilla-live"/gi' $DRBL_SCRIPT_PATH/conf/drbl-ocs.conf
} # end of put_clonezilla_live

#
remove_clonezilla_live() {
  local lines begin_line end_line
  # (1) Turn off the menu in pxelinux config only.
  lines="$(get_pxecfg_image_block Clonezilla-live $pxecfg_pd/pxelinux.cfg/default)"
  begin_line="$(echo $lines | awk -F" " '{print $1}')"
  end_line="$(echo $lines | awk -F" " '{print $2}')"
  if [ -n "$begin_line" -a -n "$end_line" ]; then
    echo "Turn off the PXE Clonezilla live menu in this DRBL server..."
    hide_reveal_pxe_img Clonezilla-live hide $pxecfg_pd/pxelinux.cfg/default
  fi
  #
  # (2) Change drbl-ocs.conf as clonezilla-live based Clonezilla SE
  echo "Modifying option diskless_client_os in drbl-ocs.conf..."
  LC_ALL=C perl -pi -e 's/^[[:space:]]*diskless_client_os=.*/diskless_client_os="nfsroot"/gi' $DRBL_SCRIPT_PATH/conf/drbl-ocs.conf
} # end of remove_clonezilla_live

#############
###  MAIN ###
#############
#
check_if_root

# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    -i|--iso-url)
        shift; mode="install"
        if [ -z "$(echo $1 |grep ^-.)" ]; then
          # skip the -xx option, in case 
          iso_url_for_pxe_ocs_live="$1"
          [ -z "$iso_url_for_pxe_ocs_live" ] && USAGE && exit 1
	  shift
        fi
	;;
    -u|--uninstall) shift; mode="uninstall" ;;
    -*)     echo "${0}: ${1}: invalid option" >&2
            USAGE >& 2
            exit 2 ;;
    *)      break ;;
  esac
done

#
case "$mode" in 
  install) put_clonezilla_live ;;
  uninstall) remove_clonezilla_live;;
  *) USAGE; exit 1;;
esac
