#!/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.
# This script works with live helper 1.0~a42-2 (Patched by DRBL team)

#
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/opt/drbl/}"
. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
. $DRBL_SCRIPT_PATH/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_type="standard"
DEBIAN_DIST_DEF="etch"
pkgs="$PKG_FROM_DBN_WHICH_OCS_LIVE_NEED drbl $PKG_FROM_DRBL_FOR_CLONEZILLA_LIVE"
categories_default="main"

# DRBL repository
DRBL_REPOSITORY_URL="http://free.nchc.org.tw/drbl-core"
# url to the repository key
DRBL_GPG_KEY_URL="http://drbl.nchc.org.tw/GPG-KEY-DRBL"

# The files in dir $ocs_live_script_dir/ will be copied to the dir /live-hook-dir in dir chroot. The file "ocs-live-hook" is in $ocs_live_script_dir
# We put some files in dir ocs_minimal_hook/ to do some jobs, like clean unnecessary files, set locales...
ocs_live_script_dir="$DRBL_SCRIPT_PATH/setup/files/ocs/live-hook"
# The script inside $ocs_live_script_dir/ will be run when chroot. There are many files in $ocs_live_script_dir/, we will just run one here.
run_hook_script="ocs-live-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 "-a, --packages PKG     Specify to add PKG, e.g. firmware-bnx2"
    echo "-b, --branch [s|stable|t|testing|u|unstable]  specifies the DRBL branch to be used in Live CD. Default is stable."
    echo "-c, --categories CAT   Sepcify the category, e.g. 'main', 'main non-free', default is \'$categories_default\' if not specified."
    echo "-d, --debian-dist [stable|testing|unstable|etch|lenny|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 "-k, --live-kernel-pkg KERNEL_VER Assign kernel version as KERNEL_VER (KERNEL VER package must exist in repository. Ex. if KERNEL_VER is 2.6.20-1-486, then linux-image-2.6.20-1-486, squashfs-modules-2.6.20-1-486, and unionfs-modules-2.6.20-1-486 will be used."
    echo "-l, --drbl-live-branch [s|stable|t|testing|u|unstable|e|experimental]  specifies the DRBL live branch to be used in Live CD. Default is stable."
    echo "-p, --use-backports Include backports in the repository."
    echo "-v, --verbose   Run make-live/lh_build in verbose mode"
    echo "Ex: $0 -l experimental -b unstable -k 2.6.24-etchnhalf.1"
}
#
turn_on_etch_backports() {
  # Backports
  # We use etchbpo instead of bpo to avoid being overwritten by the one "bpo" (it's for sarge-backports) in /etc/make-live.conf.
  export LIVE_REPOSITORIES="$LIVE_REPOSITORIES etchbpo"
  export LIVE_REPOSITORY_etchbpo="http://www.backports.org/debian/"
  export LIVE_REPOSITORY_KEY_etchbpo="http://backports.org/debian/archive.key"
  export LIVE_REPOSITORY_KEYRING_etchbpo=""
  export LIVE_REPOSITORY_DISTRIBUTION_etchbpo="etch-backports"
  export LIVE_REPOSITORY_SECTIONS_etchbpo="main contrib non-free"
}

# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    -a|--packages)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              extra_pkgs="$1"
              shift
            fi
	    [ -z "$extra_pkgs" ] && USAGE && exit 1
            ;;
    -b|--branch)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              drbl_branch="$1"
              shift
            fi
	    [ -z "$drbl_branch" ] && USAGE && exit 1
            ;;
    -c|--categories)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              categories="$1"
              shift
            fi
	    [ -z "$categories" ] && USAGE && exit 1
            ;;
    -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
            ;;
    -k|--live-kernel)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              live_kernel_ver="$1"
              shift
            fi
	    [ -z "$live_kernel_ver" ] && USAGE && exit 1
            ;;
    -l|--drbl-live-branch)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
              # skip the -xx option, in case 
              drbl_live_branch="$1"
              shift
            fi
	    [ -z "$drbl_live_branch" ] && USAGE && exit 1
            ;;
    -p|--use-backports)
	    use_backports="yes"
            shift ;;
    -v|--verbose)
	    verbose="on"
            shift ;;
    -*)     echo "${0}: ${1}: invalid option" >&2
            USAGE >& 2
            exit 2 ;;
    *)      break ;;
  esac
done

if ! type lh_build &>/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-helper' to install the necessary packages, then run $0 again."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  exit 1
fi

rm -rf debian-live/.stage/

[ -z "$debian_dist" ] && debian_dist="$DEBIAN_DIST_DEF"
[ -z "$categories" ] && categories="$categories_default"
[ -n "$extra_pkgs" ] && pkgs="$pkgs $extra_pkgs"

case "$drbl_branch" in
  t|testing)
     echo "Using DRBL testing branch..."
     LIVE_REPOSITORY_SECTIONS_drbl="stable testing"
     ;;
  u|unstable)
     echo "Using DRBL unstable branch..."
     LIVE_REPOSITORY_SECTIONS_drbl="stable testing unstable"
     ;;
  *)
     echo "Using DRBL stable branch..."
     LIVE_REPOSITORY_SECTIONS_drbl="stable"
     ;;
esac
case "$drbl_live_branch" in
  t|testing)
     echo "Using DRBL Live testing branch..."
     LIVE_REPOSITORY_SECTIONS_drbl="$LIVE_REPOSITORY_SECTIONS_drbl live-stable live-testing"
     ;;
  u|unstable)
     echo "Using DRBL Live unstable branch..."
     LIVE_REPOSITORY_SECTIONS_drbl="$LIVE_REPOSITORY_SECTIONS_drbl live-stable live-testing live-unstable"
     ;;
  e|experimental)
     echo "Using DRBL Live experimental branch..."
     LIVE_REPOSITORY_SECTIONS_drbl="$LIVE_REPOSITORY_SECTIONS_drbl live-stable live-testing live-unstable live-experimental"
     ;;
  *)
     echo "Using DRBL live stable branch..."
     LIVE_REPOSITORY_SECTIONS_drbl="$LIVE_REPOSITORY_SECTIONS_drbl live-stable"
     ;;
esac

#
[ "$use_backports" = "yes" ] && turn_on_etch_backports

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

[ "$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

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 /dev/pts &>/dev/null
  chroot debian-live/chroot umount /proc &>/dev/null
  chroot debian-live/chroot umount /sys &>/dev/null
  (
    cd debian-live/
    lh_clean
  )
  rm -rf debian-live
fi

mkdir debian-live
(
cd debian-live

$pref lh_config --categories "$categories"
$pref lh_config --mirror-binary $mirror_url --mirror-binary-security $mirror_security_url 
$pref lh_config --mirror-bootstrap $mirror_url
$pref lh_config --mirror-chroot $mirror_url --mirror-chroot-security $mirror_security_url
$pref lh_config --bootstrap-flavour $debian_type --packages "$pkgs" --hook /live-hook-dir/$run_hook_script --apt apt --apt-recommends disabled --binary-indices disabled --bootstrap cdebootstrap --tasksel none
$pref lh_config --initramfs live-initramfs
$pref lh_config --username user --bootappend username=user

if [ "$debian_dist" = "etch" ] || [ "$debian_dist" = "stable" ]; then
  if [ -n "$live_kernel_ver" ]; then
    # kernel_related_pkgs="linux-image-${live_kernel_ver} unionfs-modules-${live_kernel_ver} squashfs-modules-${live_kernel_ver}"
    kernel_related_pkgs="linux-image-${live_kernel_ver} aufs-modules-${live_kernel_ver} squashfs-modules-${live_kernel_ver}"
    else
    #kernel_related_pkgs="linux-image-2.6 unionfs-modules-2.6 squashfs-modules-2.6"
    kernel_related_pkgs="linux-image-2.6 aufs-modules-2.6 squashfs-modules-2.6"
  fi
  #$pref lh_config --distribution $debian_dist --union-filesystem unionfs --linux-packages "$kernel_related_pkgs"
  $pref lh_config --distribution $debian_dist --union-filesystem aufs --linux-packages "$kernel_related_pkgs"
else
  if [ -n "$live_kernel_ver" ]; then
    kernel_related_pkgs="linux-image-${live_kernel_ver} aufs-modules-${live_kernel_ver} squashfs-modules-${live_kernel_ver}"
    else
    kernel_related_pkgs="linux-image-2.6 aufs-modules-2.6 squashfs-modules-2.6"
  fi
  $pref lh_config --distribution $debian_dist --union-filesystem aufs --linux-packages "$kernel_related_pkgs"
fi

# exclude some stuffs from the squashfs root, since in Live system, we will only use vmlinuz and initrd in /{casper,live}, not in the /boot/ in the squashfs.
export MKSQUASHFS_OPTIONS="-e boot"

# We force to use 486 kernel only.
$pref lh_config --linux-flavours 486

# No memtest from debian, we will use the one from drbl since it's newer.
$pref lh_config --memtest disabled

# Put hook files
mkdir -p config/chroot_local-includes/live-hook-dir
for i in $ocs_live_script_dir; do
  cp -p $i/* config/chroot_local-includes/live-hook-dir/
done
cp $DRBL_SCRIPT_PATH/conf/drbl.conf config/chroot_local-includes/live-hook-dir/

# prepare drbl source list
cat << AddDRBLRepository > config/chroot_sources/drbl.chroot
deb $DRBL_REPOSITORY_URL drbl $LIVE_REPOSITORY_SECTIONS_drbl
AddDRBLRepository

# prepare drbl key
LANG=C wget -O config/chroot_sources/drbl.chroot.gpg $DRBL_GPG_KEY_URL

$pref lh_build
)
mv -f debian-live/binary.iso $target_iso
