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

# We have to create account casper, otherwise there is some problem. See below for more details.
casper_uid="999"
casper_passwd="live"
passwd_autologin=live

# create a dummy /etc/fstab in Live CD so that nis/yp won's 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

# add the casper account with UID=999 first, otherwise if we add accounts later, when Debian live boots, scripts/casper-bottom/10adduser will kill casper account first (if it's not UID=999, 10adduser can remove that successfully), then when it try to add it, it will fail. Then there is no casper account at all. This will result client's /sbin/casper-getty fails in /etc/inittab. Therefore we can not login in console 1-6.
/usr/sbin/useradd -m -u $casper_uid casper
echo "casper:$casper_passwd" | /usr/sbin/chpasswd

# run drbl setup, we use DRBL SSI mode and clonezilla box mode
yes "" | /opt/drbl/sbin/drblsrv -i -l 0
# each ethernet card provide $client_no_per_NIC clients.
yes "" | /opt/drbl/sbin/drblpush -i -l 0 -r 1 -z 1 -p $client_no_per_NIC

# enable autologin
/opt/drbl/sbin/drbl-login-switch -l 0 -a -p ${passwd_autologin}

# stop all the services so that make-live can pack it.
/opt/drbl/sbin/drbl-all-service stop

# Since there is an issue about squashfs + kernel nfs, we use user space nfs
# 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.
apt-get --yes remove nfs-kernel-server
apt-get --yes install unfs3

# generate a template for tcp wrapper.
client_IP_list="$(/opt/drbl/bin/get-client-ip-list)"
client_IP_list="$(echo $client_IP_list)"   # convert to 1 line
echo "ALL: $client_IP_list" > /etc/hosts.allow.drbl-template

# preset some network setting
guess_uplink_port="$(route -n | awk '/^0.0.0.0/ {print $8}' | sort | head -n 1)"
ethernet_list="$(/opt/drbl/bin/get-all-nic-ip -d)"
ethernet_drbl="$(echo $ethernet_list | sed -e "s/$guess_uplink_port//g")"
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

# The primary network interface
auto eth0
iface eth0 inet dhcp

NET_END

for nic in $ethernet_drbl; do
  IP_ADR=""
  IP_ADR="$(/opt/drbl/bin/get_ip $nic)"
  if [ -n "$IP_ADR" ]; then
    cat <<-NET_END >> /etc/network/interfaces
auto $nic
iface $nic inet static
	address $IP_ADR
	netmask 255.255.255.0

NET_END
  fi
done

# Append the PATH and default LANG in system.
echo "export PATH=/opt/drbl/sbin:/opt/drbl/bin:\$PATH" >> /etc/profile
echo "export PATH=/opt/drbl/sbin:/opt/drbl/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

# clean unnecessary backup file to save space
clean_unnecessary_backup_file_in_boot

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

# some required modules at startup
append_mod_in_etc_modules

# dirty work, maybe there shoul have better method to do this...
# If xterm is found with xfce installed, remove xterm since it's not as good as mlterm in Traditional Chinese font. We already installed mlterm, and if xfce4 is removed, mlterm will be the default terminal program.
if dpkg -l xfce4 &>/dev/null; then
  echo "mlterm is installed, it will replace xterm."
  echo "Removing xterm to make mlterm as default terminal in XFCE..."
  apt-get -y remove xterm
fi

# get unifont.bgf for bterm, this is used in drbl live standard version.
(
  # load $wget_opt and $uni_font_url
  . /opt/drbl/conf/drbl-ocs.conf
  echo "Download unifont.bgf from $uni_font_url and put it in /opt/drbl/lib/..."
  mkdir -p /opt/drbl/lib/
  cd /opt/drbl/lib/
  wget $wget_opt $uni_font_url
)

### THE END ###
# DO NOT PUT SCRIPT AFTHER THIS!!!
# kill this program before creating squashfs filesystem.
clean_ocs_hook_files_in_chroot
