#!/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).
debian_type="minimal"
debian_dist="etch"
pkgs="$PKG_FROM_DBN_MINIMAL_NEED $PKG_FROM_DBN_WHICH_OCS_LIVE_NEED"
target_iso="debian-live-for-ocs-`date +%Y%m%d`.iso"
ocs_live_script="$DRBL_SCRIPT_PATH/setup/files/ocs/live-chroot"
#hook_script_0="cp -f /K[0-9][0-9]* /etc/rc0.d/; cp -f /K[0-9][0-9]* /etc/rc6.d/;chown root.root /etc/rc[06].d/K[0-9][0-9]*; rm -f /K[0-9][0-9]*"
hook_script_1="mv -f /S[0-9][0-9]* /etc/rc2.d/; chown root.root /etc/rc2.d/S[0-9][0-9]*"
hook_script_2="rm -f /boot/initrd*.bak"
#hook_script_3="localedef -f UTF-8 -i zh_TW zh_TW.UTF-8"
# It seems useless to run this...
#hook_script_3="echo 'zh_TW.UTF-8 UTF-8' > /etc/locale.gen; locale-gen"

#
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 "-v, --verbose   Run make-live in verbose mode"
}

# Parse command-line options
while [ $# -gt 0 ]; do
  case "$1" in
    -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 it."
  [ "$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/

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

# add the script in /usr/share/make-live/hooks/minimal
for i in 1 2; do
  eval hook_scr=\$hook_script_$i
  if ! grep -q "$hook_scr" /usr/share/make-live/hooks/minimal; then
   echo "$hook_scr" >> /usr/share/make-live/hooks/minimal
  fi
done
$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 script in /usr/share/make-live/hooks/minimal
# Ugly... since there are a lot of " and /, perl will be crazy. Just do it line by line here.
#perl -i -ne "print unless /^cp .*rc0.d/ .. /.*$/" /usr/share/make-live/hooks/minimal
perl -i -ne "print unless /^mv .*rc2.d/ .. /.*$/" /usr/share/make-live/hooks/minimal
perl -i -ne "print unless /^rm -f .*initrd.*bak/ .. /.*$/" /usr/share/make-live/hooks/minimal
#perl -i -ne "print unless /^$hook_script_3/ .. /.*$/" /usr/share/make-live/hooks/minimal
mv -f debian-live/binary.iso $target_iso
