#!/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

# 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"

ocslive_tmp="$(mktemp -d /tmp/ocslive_tmp.XXXXXX)"
# iso_url_for_pxe_ocs_live is from drbl-ocs.conf. e.g. 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
  # If not assigned in drbl-ocs.conf, try to find one.
  echo "Finding the available Clonezilla live iso..."
  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
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..."
wget -P "$ocslive_tmp" $iso_url_for_pxe_ocs_live

# (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

# (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

#
[ -d "$ocslive_tmp" -a -n "$(echo "$ocslive_tmp" | grep "ocslive")" ] && rm -rf $ocslive_tmp
