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

# 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

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

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

# run drbl setup, we use DRBL SSI mode and clonezilla box mode
yes "" | $DRBL_SCRIPT_PATH/sbin/drblsrv -i -l 0

# NOTE! All the setting about clients must be done before drblpush.
# run drblpush, each ethernet card provide $client_no_per_NIC clients.
yes "" | $DRBL_SCRIPT_PATH/sbin/drblpush -i -l 0 -r 1 -z 1 -p $client_no_per_NIC

# post process
# enable autologin
case "$autologin_passwd_opt" in
  "random")
    passwd_autologin="$($DRBL_SCRIPT_PATH/random_pw_gen.pl $random_passwd_length)"
  *)
    passwd_autologin="$passwd_autologin_def"
    ;;
esac
$DRBL_SCRIPT_PATH/sbin/drbl-login-switch -l 0 -a -p ${passwd_autologin}

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

# remove all the service in rc, we do not want all the services to automatically start at boot
$DRBL_SCRIPT_PATH/sbin/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
echo "Since some config files are modified in template client, creating template tarball for DRBL SSI..."
$DRBL_SCRIPT_PATH/sbin/gen_ssi_files

# 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.
# Before --purge nfs-kernel-server, we have to backup /etc/exports
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

# generate a template for tcp wrapper.
client_IP_list="$($DRBL_SCRIPT_PATH/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="$($DRBL_SCRIPT_PATH/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="$($DRBL_SCRIPT_PATH/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=$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)
(
  # 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 $DRBL_SCRIPT_PATH/lib/..."
  mkdir -p $DRBL_SCRIPT_PATH/lib/
  cd $DRBL_SCRIPT_PATH/lib/
  wget $wget_opt $uni_font_url
)

# 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

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

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

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