#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Program to create Clonezilla live.

# load config file
. /live-hook-dir/drbl.conf
. /live-hook-dir/drbl-ocs.conf
. /live-hook-dir/ocs-live-hook.conf
# load functions
. /live-hook-dir/ocs-live-hook-functions 

# locale_to_keep_for_no_X is loaded from ocs-live-hook.conf.
locale_to_keep="$locale_to_keep_for_no_X"

#
apt-get update; apt-get -y dist-upgrade

#
install_debian_extra_modules

# preset lo network setting
cat <<-NET_END > /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

NET_END

#
clean_udev_persistent_net_rules

# Touch an empty /etc/fstab
touch /etc/fstab

# set root passwd (maybe none)
set_root_passwd

# Append the PATH in system.
append_drbl_clonezilla_PATH_in_system

# put the clonezilla live script in rcS.d
cp_ocs_live_startup_to_rcS.d

# for better security
turn_off_ssh_service

# Remove unnecessary service
remove_service_in_system mdadm-raid

# some required modules at startup
append_mod_in_etc_modules

# we need real /sbin/start-stop-daemon
remove_cdebootstrap-helper-diverts

# Exclude live mount point in /etc/init.d/umountfs to avoid error messages when rebooting or halting.
exclude_umount_live_mnt_point_in_umountfs

# run localepurge
set_localepurge
localepurge

# Generate the locales. We will use "nolocales" to avoid these to be wipped by live-initramfs.
locales_gen

# Fix the bug about login prompt too early (Ref: https://bugs.launchpad.net/ubuntu/+source/upstart/+bug/65230)
fix_ubuntu_upstart_tty1_6_distorted_if_necessary

# If kexec tools is installed, disable the service. We do not need that for Clonezilla
disable_kexec

# Touch /etc/inittab to suppress the error of Ubuntu booting, since it uses upstart, and no /etc/inittab, but live-initramfs will try to do something about /etc/inittab and if not found, an error message will be shown
touch /etc/inittab

#
append_framebuffer_modules_for_ubuntu_if_necessary

# If busybox-initramfs was found, replace it with the complete one. Since busybox-initramfs lacks some function, like tftp... We want the complete "busybox". 
# This is specially for Ubuntu since Debian does not have package busybox-initramfs.
updte_initramfs_flag="no"
if [ -e "/usr/lib/initramfs-tools/bin/busybox" ]; then
  cp -af /bin/busybox /usr/lib/initramfs-tools/bin/busybox
  updte_initramfs_flag="yes"
fi
# This cdrom_id is also for Ubuntu only, since update-initramfs udev hook in Debian will copy /live/udev/*_id, but Ubuntu does not.
# Ref: https://bugs.launchpad.net/ubuntu/+source/live-initramfs/+bug/254360
if ! grep -Eq "^for program in /lib/udev/\*_id; do" /usr/share/initramfs-tools/hooks/udev; then
  cat <<-UDEV_END >> /usr/share/initramfs-tools/hooks/udev

# Appended by Clonezilla live
# Ref: https://bugs.launchpad.net/ubuntu/+source/live-initramfs/+bug/254360
for program in /lib/udev/*_id; do
  copy_exec \$program /lib/udev
done
UDEV_END
  updte_initramfs_flag="yes"
fi
# We have to run update-initramfs so that the new busybox/cdromid will be in initramfs.
[ "$updte_initramfs_flag" = "yes" ] && update-initramfs -u

# Update the variable BUILD_SYSTEM in /etc/live.conf (This is special for live-initramfs, not for casper)
update_build_system_in_etc_live_conf

# Put font
mkdir -p $DRBL_SCRIPT_PATH/lib/
wget -P $DRBL_SCRIPT_PATH/lib/ http://free.nchc.org.tw/clonezilla-live/template-files/fonts/unifont.bgf

# Put start script
mkdir -p /etc/ocs/
cp -r $DRBL_SCRIPT_PATH/setup/files/ocs/ocs-live.d /etc/ocs/

# Prepare the prerun/postrun dirs of clonezilla
mkdir -p $OCS_PRERUN_DIR $OCS_POSTRUN_DIR

# If upstart dir is found, copy ttyS0, we need that for serial console
if [ -d /etc/event.d ]; then
 if [ ! -e /etc/event.d/ttyS0 ]; then
  cp -af $DRBL_SCRIPT_PATH/setup/files/ocs/live-hook/upstart/ttyS0 /etc/event.d/
 fi
fi

# Generate a config file
# Set ocs_live_keymap="" and ocs_lang="" so that when a general clonezilla live boot, it will ask for language and keymap.
cat <<-OCS_CONF > /etc/ocs/ocs-live.conf
# ocs_live_run is the main program to run in Clonezilla live to save or restore. or other command. Available program: ocs-live-general, ocs-live-restore or any command you write. Use the Absolute path in Clonezilla live.
ocs_live_run="ocs-live-general"

# ocs_live_extra_param will be used only when ocs_live_run=ocs-live-restore (not for ocs-live-general or any other), then it will be passed to ocs-sr. Therefore these parameters are actually those of ocs-sr.
# Ex: ocs_live_extra_param="-b -c restoredisk sarge-r5 hda"
ocs_live_extra_param=""

# ocs_live_keymap is for keymap used in Clonezilla live. Man install-keymap for more details. Ex: 
# ocs_live_keymap="NONE" (won't change the default layout)
# ocs_live_keymap="/usr/share/keymaps/i386/azerty/fr-latin9.kmap.gz" (French keyboard)
ocs_live_keymap=""

# batch mode or not (yes/no), if no, will run interactively.
ocs_live_batch="no"

# ocs_lang is the language used in Clonezilla live. Available value: en_US.UTF-8, zh_TW.UTF-8... (see \$DRBL_SCRIPT_PATH/lang/bash/)
ocs_lang=""

OCS_CONF

# Dirty hacking: rm files
if [ "$dirty_hacking_remove_files_for_ocs_live" = "yes" ]; then
  dirty_hacking_rm_files_for_ocs_live
fi

# Create a list of all packages:
dpkg --get-selections > /root/Clonezilla-live-packages.txt

# ///Note/// This should be the last one after any apt-get.
# clean unnecessary backup file to save space
clean_unnecessary_backup_file_in_boot

### THE END ###
# DO NOT PUT ANY SCRIPT AFTHER THIS!!!
# kill this program before creating squashfs filesystem.
rm -rf /live-hook-dir
