#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Description: This script will create a Debian live cd iso which is used as a template for clonezilla image with restoration function.

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

mirror_url="http://free.nchc.org.tw/debian"
mirror_security_url="http://free.nchc.org.tw/debian-security/"
# debian_type can be minimal (about 67 MB for Etch)/minimal-net (about 85 MB for Etch).
debian_type="minimal"
DEBIAN_DIST_DEF="testing"
pkgs="$PKG_FROM_DBN_WHICH_OCS_LIVE_NEED"
target_iso="debian-live-for-ocs-`date +%Y%m%d`.iso"
# The files in dir $ocs_live_script/ will be copied to the root (/) dir in dir chroot when make-live is run with --include-chroot. The file "ocs-minimal-hook" is in $ocs_live_script
# We put some files in dir ocs_minimal_hook/ to do some jobs, like clean unnecessary files, set locales...
ocs_live_script="$DRBL_SCRIPT_PATH/setup/files/ocs/live-chroot"
# The script inside $ocs_live_script/ will be run when chroot. There are many files in $ocs_live_script/, we will just run one here.
run_hook_script="ocs-minimal-hook"

#
check_if_root
#
prog="$(basename $0)"

# functions
USAGE() {
    echo "Usage:"
    echo "To create a Debian live CD which is used a template for Clonezilla live:"
    echo "$prog [OPTION]"
    echo "OPTION:"
    echo "-d, --debian-dist [stable|testing|unstable|sarge|etch|sid...]  Assign Debian dist, the default is 'testing' if not assigned."
    echo "-v, --verbose   Run make-live in verbose mode"
}

# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    -d|--debian-dist)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              debian_dist="$1"
              shift
            fi
	    [ -z "$debian_dist" ] && USAGE && exit 1
            ;;
    -v|--verbose)
	    verbose="on"
            shift ;;
    -*)     echo "${0}: ${1}: invalid option" >&2
            USAGE >& 2
            exit 2 ;;
    *)      break ;;
  esac
done

if ! type make-live &>/dev/null; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "This script only works in Debian Etch or later!"
  echo "If you are running Debian Etch or later, use 'apt-get install live-package' to install the necessary packages, then run $0 again."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  exit 1
fi

[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "Creating a Debian live cd iso which is used for clonezilla image with restoration function. The created iso will be in $target_iso" 
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL

rm -rf debian-live/.stage/

[ -z "$debian_dist" ] && debian_dist="$DEBIAN_DIST_DEF"

if [ "$verbose" = "on" ]; then
  pref="bash -x"
fi

# add the script in $live_pkg_hook_dir/minimal
if ! grep -q "/$run_hook_script" $live_pkg_hook_dir/minimal; then
 echo "/$run_hook_script" >> $live_pkg_hook_dir/minimal
fi
$pref make-live --mirror $mirror_url --mirror-security $mirror_security_url --distribution $debian_dist --flavour $debian_type --packages "$pkgs" --include-chroot "$ocs_live_script"
# remove the line /$run_hook_script in $live_pkg_hook_dir/minimal
perl -i -ne "print unless /^\/$run_hook_script/ .. /.*$/" $live_pkg_hook_dir/minimal
mv -f debian-live/binary.iso $target_iso
