#!/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="etch"
pkgs="$PKG_FROM_DBN_WHICH_OCS_LIVE_NEED"

# 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 $DEBIAN_DIST_DEF if not assigned."
    echo "-i, --assign-version-no NO  Assign the version no as NO instead of date."
    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
            ;;
    -i|--assign-version-no)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              version_no="$1"
              shift
            fi
	    [ -z "$version_no" ] && 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 version_no is not assigned, use date (Ex. 20070409)
[ -z "$version_no" ] && version_no="$(date +%Y%m%d)"
target_iso="debian-live-for-ocs-${version_no}.iso"

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

if [ -d "debian-live" ]; then
  echo "Found dir debian-live, clean stale debian-live files..."
  chroot debian-live/chroot umount /proc &>/dev/null
  rm -rf debian-live
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" --hook /$run_hook_script
mv -f debian-live/binary.iso $target_iso
