#!/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/ocs-live-hook.conf
# load functions
. /live-hook-dir/ocs-live-hook-functions 

#
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, I do not like root without passwd.
set_root_passwd

# We have to create account, say "user", otherwise there is a problem when we put command in user's ~/.bash_profile. See ocs-live-hook-functions for more details.
preseed_autologin_account

# Append the PATH in system.
append_drbl_clonezilla_PATH_in_system

# Append start of clonezilla in ~user/.bash_profile
append_start_clonezilla_in_user_bash_profile

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

# for better security
turn_off_ssh_service

# some required modules at startup
append_mod_in_etc_modules

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

# Turn on number lock when booting
turn_on_numlock_in_booting

# 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

#
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.
[ -e "/usr/lib/initramfs-tools/bin/busybox" ] && cp -af /bin/busybox /usr/lib/initramfs-tools/bin/busybox

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

# 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
