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

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

# run drbl setup, we use DRBL SSI mode and clonezilla box mode
yes "" | /opt/drbl/sbin/drblsrv -i -l 0
# each ethernet card provide 20 clients.
yes "" | /opt/drbl/sbin/drblpush -i -l 0 -r 1 -z 1 -p 20
# set root passwd
echo "root:drbllive" | /usr/sbin/chpasswd

# add an account drbl
username=drbl
passwd_drbl=live
/usr/sbin/useradd -m $username
echo "$username:${passwd_drbl}" | /usr/sbin/chpasswd
# add user drbl to sudoers
chmod u+w /etc/sudoers
echo "drbl ALL=(ALL) ALL" >> /etc/sudoers
chmod u-w /etc/sudoers

# enable autologin
passwd_autologin=live
/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

# 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

# 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

# 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

# Run ocs_minimal_hook, this is the same as we do to create debian-live-for-ocs Live CD.
/ocs_minimal_hook

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