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

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).
# We have to use standard, since if using minimal, it will ignore apt key from DRBL.
#debian_type="minimal"
debian_type="standard"
debian_dist="etch"
pkgs="drbl $PKG_FROM_DBN_WHICH_OCS_LIVE_NEED $PKG_FROM_DBN"
target_iso="clonezilla-server-live-`date +%Y%m%d`.iso"
ocs_live_script="$DRBL_SCRIPT_PATH/setup/files/ocs/live-chroot"
# 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-srv-live-hook" is in $ocs_live_script
# The script will be run when chroot
hook_script="/drbl_hook"

# DRBL repository
LIVE_REPOSITORIES="drbl"
LIVE_REPOSITORY_drbl="http://free.nchc.org.tw/drbl-core"
# url to the repository key
LIVE_REPOSITORY_KEY_drbl="http://drbl.nchc.org.tw/GPG-KEY-DRBL"
# the name of the keyring
LIVE_REPOSITORY_KEYRING_drbl=""
# the name of your distribution
LIVE_REPOSITORY_DISTRIBUTION_drbl="drbl"
# sections
LIVE_REPOSITORY_SECTIONS_drbl="stable testing unstable"
export LIVE_REPOSITORIES LIVE_REPOSITORY_drbl LIVE_REPOSITORY_KEY_drbl LIVE_REPOSITORY_KEYRING_drbl LIVE_REPOSITORY_DISTRIBUTION_drbl LIVE_REPOSITORY_SECTIONS_drbl

#
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 "-k, --package FILE  Specifies FILE to be installed in Live CD."
    echo "-p, --package-list FILE  specifies an external package list file (such as xfce, gnome, kde...), one package for each line"
    echo "-v, --verbose       Run make-live in verbose mode"
}

#
extra_pkgs=""
pkg_list=""
pkg_list_opt=""
# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    -k|--package)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              extra_pkgs="$1"
              shift
            fi
	    [ -z "$extra_pkgs" ] && USAGE && exit 1
            ;;
    -p|--package-list)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              pkg_list="$pkg_list $1"
              shift
            fi
	    [ -z "$pkg_list" ] && 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/

# append the extra packages
[ -n "$extra_pkgs" ] && pkgs="$pkgs $extra_pkgs"
echo "The packages to be included in this live CD:"
echo "$msg_delimiter_star_line"
echo "$pkgs"
echo "$msg_delimiter_star_line"
if [ -n "$pkg_list" ]; then
  pkg_list_opt="$pkg_list_opt --package-list $pkg_list"
  echo "The packages list to be included in this live CD:"
  echo "$msg_delimiter_star_line"
  echo "$pkg_list"
  echo "$msg_delimiter_star_line"
fi

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

# add the script in /usr/share/make-live/hooks/minimal
if ! grep -q "/ocs-srv-live-hook" /usr/share/make-live/hooks/$debian_type; then
 echo "/ocs-srv-live-hook" >> /usr/share/make-live/hooks/$debian_type
fi
$pref make-live --mirror $mirror_url --mirror-security $mirror_security_url --distribution $debian_dist --flavour $debian_type --without-generic-indices --packages "$pkgs" $pkg_list_opt --include-chroot "$ocs_live_script" --bootappend ip=frommedia
# remove the line /ocs-srv-live-hook in /usr/share/make-live/hooks/minimal
perl -i -ne "print unless /^\/ocs-srv-live-hook/ .. /.*$/" /usr/share/make-live/hooks/$debian_type
mv -f debian-live/binary.iso $target_iso
