#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Program to create DRBL/Clonezilla server live. You'd better to have 2 or more NICs in the machine, and they are configured so the created Clonezilla server live has preset DRBL environment.

# load drbl setting
. /opt/drbl/sbin/drbl-conf-functions
# load config files
. /live-hook-dir/ocs-live.conf
# load functions
. /live-hook-dir/ocs-live-hook-functions 

# create a dummy /etc/fstab in Live CD so that nis/yp won't complain
touch /etc/fstab

# clean some locales
set_localepurge
localepurge

# append the module so that it will be loaded, then gparted can grow filesystem
# Ref: http://gparted.sourceforge.net/features.php
append_mod_in_etc_modules

# set root passwd, I do not like root without passwd.
set_root_passwd

# We have to create account user, otherwise there is some problem. See ocs-live-hook-functions for more details.
preseed_autologin_account

# If mlterm is found with xfce installed, set default x-terminal-emulator as mlterm. Since xterm is not as good as mlterm in Traditional Chinese font.
if dpkg -L xfce4 &>/dev/null && dpkg -L mlterm &>/dev/null; then
  echo "Set mlterm as default x-terminal-emulator."
  update-alternatives --set x-terminal-emulator /usr/bin/mlterm
  # change the fg/bg color
  perl -pi -e "s/^.*fg_color=.*/fg_color=white/g" /etc/mlterm/main
  perl -pi -e "s/^.*bg_color=.*/bg_color=black/g" /etc/mlterm/main
fi

# preset some network setting
guess_uplink_port="$(route -n | awk '/^0.0.0.0/ {print $8}' | sort | head -n 1)"
case "$alias_eth0_for_drbl_clients" in
  yes)
    ethernet_drbl="eth0:1"
    ifconfig $ethernet_drbl $alias_eth0_IP_addr netmask 255.255.255.0
    ;;
esac

# We need loopback device, just in case.
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

# run drbl setup, we use DRBL SSI mode and clonezilla box mode
# Get the installed kernel so that we can use drblsrv-offline directly.
kernel_ver="$(unalias ls 2>/dev/null; ls /boot/vmlinuz-* | sort | tail -n 1)"
kernel_ver="$(basename $kernel_ver | sed -e "s/vmlinuz-//g")"
yes "" | drblsrv-offline -l en_US -s "$kernel_ver"

# NOTE! All the setting about clients must be done before drblpush.
# Run drblpush, now we use 1 for drbl client. The cient number is assigned in ocs-live.conf and is applied to drbl-live.sh (it is run to start drbl service after the live cd is booted).
yes "" | drblpush -l en_US -i -r 1 -z 1 -p 1 --not-add-start-drbl-srvi

# post process
# stop all the services so that make-live can pack it.
drbl-all-service stop

# remove all the service in rc, we do not want all the services to automatically start at boot
drbl-all-service del

# we still have to add some modules in clients. We still have to do this although server's /etc/modules already has them, but drblpush will clean all to avoid some problem.
for ih in $drblroot/*; do
  for imod in $mod_loaded_at_startup; do
    echo "$imod" >> $ih/etc/modules
  done
done

# we clean the template node and tarballs: (1) save the space (2) if user uses different subnet for NIC, the template directory (Ex: /tftpboot/nodes/192.168.100.1) and tarball are useless. Since we will re-run drblpush in drbl-live.sh after drbl live boots. They will be created again.
[ -d "$drblroot" -a -n "$drblroot" ] && rm -rf $drblroot/*
[ -d "$drbl_common_root" -a -n "$drbl_common_root" ] && rm -rf $drbl_common_root/drbl_ssi/*.tgz

# Note! there is an issue about squashfs 3.1 + kernel nfs, we can use user space nfs instead:
# http://lists.alioth.debian.org/pipermail/debian-live-devel/2006-September/000470.html 
# This also has a benefit, the mounted /home/partimage is ready to be seen by client.
# Before --purge nfs-kernel-server, we have to backup /etc/exports
if [ "$use_unfs" = "yes" ]; then
  cp -f /etc/exports /etc/exports.unfs3
  apt-get --yes --purge remove nfs-kernel-server
  apt-get --yes install unfs3
  mv -f /etc/exports.unfs3 /etc/exports
fi

# Append the PATH and default LANG in system.
echo "export PATH=$DRBL_SCRIPT_PATH/sbin:$DRBL_SCRIPT_PATH/bin:\$PATH" >> /etc/profile
echo "export PATH=$DRBL_SCRIPT_PATH/sbin:$DRBL_SCRIPT_PATH/bin:\$PATH" >> /etc/bash.bashrc

# do some dirty clean... no idea why "/etc/init.d/nfs-kernel-server stop" and "/"/etc/init.d/nfs-common stop" won't be able to release this:
# nfsd on /proc/fs/nfsd type nfsd (rw)
umount nfsd

# get unifont.bgf for bterm, this is used in drbl live standard version.
# This must be after drblsrv is run, since after that, we have clonezilla (so drbl-ocs.conf exists)
(
  # now with clonezilla installed, we can load variable $DEBIAN_ISO_ETC_PATH_DEF
  . /opt/drbl/conf/drbl-ocs.conf
  uni_font_url="$DEBIAN_ISO_ETC_PATH_DEF/fonts/$uni_font_file"
  echo "Download unifont.bgf from $uni_font_url and put it in $DRBL_SCRIPT_PATH/lib/..."
  mkdir -p $DRBL_SCRIPT_PATH/lib/
  cd $DRBL_SCRIPT_PATH/lib/
  wget $wget_opt $uni_font_url
)

# since ssh services is on, and autologin account is known for the whole world, we have to block it.
block_all_clients_by_tcpwrapper

# put some desktop icons in the account $autologin_account.
mkdir -p /home/$autologin_account/Desktop
chown -R $autologin_account.$autologin_account /home/$autologin_account/Desktop
cp -a $DRBL_SCRIPT_PATH/setup/files/misc/desktop-icons/drbl-live/*.desktop /home/$autologin_account/Desktop
chown $autologin_account.$autologin_account /home/$autologin_account/Desktop/*.desktop

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

# Turn on number lock when booting
turn_on_numlock_in_booting

# turn off alias IP address
[ "$alias_eth0_for_drbl_clients" = "yes" ] && ifconfig eth0:1 down

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