#! /bin/bash

set -e

## Setup script run at boot time.

############################
TIMEOUT=120
URL="http.debian.net"
INSTALLER="/usr/lib/debian-installer/images/*/*/text/debian-installer/"

DLROOT="/opt/live"
. /etc/fai/fai.conf
. /etc/fai/nfsroot.conf

##########

check_network () {
    ## Check if package repository is accessible:
    if ! wget --quiet --output-document=/tmp/fai-setup $URL ; then
	echo "Error accessing '$URL', check network and internet access."
	exit 1
    fi
}

setup_nfsroot () {
    echo "Creating the nfsroot for FAI."
    trap "rc=$?; rm -rf $NFSROOT; exit $rc" ERR SIGHUP SIGINT SIGTERM
    fai-setup -e -v -l
    trap - ERR SIGHUP SIGINT SIGTERM
    ## Create pxelinux boot configuration for workstationXX.
    ## The seq range is sed from the corresponding variable
    ## when fcopy'd:
    NUM=0
    for i in `seq WS_RANGE` ; do
	NUMSTR=`printf "%02d" $NUM`
	fai-chboot -IFvu $FAI_CONFIG_SRC workstation$NUMSTR  2>&1 | tee -a /var/log/fai/fai-chboot.log
	NUM=$(($NUM+1))
    done

    if [ -d $DLROOT ] ; then
	fai-chboot -vc diskless.tmpl default
    else
        ## create default configuration (sysinfo):
	fai-chboot -Svu $FAI_CONFIG_SRC default
    fi
}


setup_diskless () {
    export LC_ALL=C
    trap "rc=$?; rm -rf $DLROOT; exit $rc" ERR SIGHUP SIGINT SIGTERM
    fai -vNu diskless dirinstall $DLROOT/filesystem.dir/
    trap - ERR SIGHUP SIGINT SIGTERM

    TEMPLATE=$TFTPROOT/pxelinux.cfg/diskless.tmpl
    if [ ! -e $TEMPLATE ]; then
	KERNEL=`basename $(ls $TFTPROOT/vmlinuz*)`
	INITRD=`basename $(ls $TFTPROOT/initrd.img*)`

	echo "Creating template with $KERNEL and $INITRD."
	cat > $TEMPLATE <<EOF
# template for diskless
default fai-generated

label fai-generated
kernel $KERNEL
append initrd=$INITRD ip=dhcp root=/dev/nfs nfsroot=/opt boot=live
EOF
    else
	echo "The template $TEMPLATE exists already!"
    fi

    ## Create pxelinux boot configuration for disklessXX.
    ## The seq range is sed from the corresponding variable
    ## when fcopy'd:
    NUM=0
    for i in `seq DL_RANGE` ; do
	NUMSTR=`printf "%02d" $NUM`
	fai-chboot -vc diskless.tmpl diskless$NUMSTR  2>&1 | tee -a /var/log/fai/fai-chboot.log
	NUM=$(($NUM+1))
    done

    ## Boot unknown machines as diskless:
    fai-chboot -vc diskless.tmpl default
}


setup_PXEinstaller () {
    ## Add Debian PXE Installer.
    ## Copy stuff, symlinks do not work (chroot environment):
    cp -ru $INSTALLER $TFTPROOT

    if [ -d $TFTPROOT/debian-installer/i386 ] ; then
	## add installer menu
	cat >> $TFTPROOT/pxelinux.cfg/default <<EOF
menu label Debian-LAN/FAI live system

menu title Boot menu
menu background debian-installer/i386/boot-screens/splash.png
menu color title        * #FFFFFFFF *
menu color border       * #00000000 #00000000 none
menu color sel          * #ffffffff #76a1d0ff *
menu color hotsel       1;7;37;40 #ffffffff #76a1d0ff *
menu color tabmsg       * #ffffffff #00000000 *
menu color help         37;40 #ffdddd00 #00000000 none
menu vshift 16
menu rows 7
menu helpmsgrow 12
menu cmdlinerow 12
menu tabmsgrow 13
default debian-installer/i386/boot-screens/vesamenu.c32
timeout 150
ontimeout fai-generated
prompt 0
noescape 1

label Debian Installer i386
        config debian-installer/i386/pxelinux.cfg/default

label Debian Installer amd64
        config debian-installer/amd64/pxelinux.cfg/default
EOF
    fi
}


#########################
## Add missing plugins to munin, the line will be commented afterwards:
munin-node-configure --shell 2>/dev/null | sh && sed -i "s%\(^munin-node-configure\)%\#\1%" $0

## Setup nfsroot for FAI:
if [ ! -d $NFSROOT ] ; then
    cat <<EOF
================================================================================
The nfsroot for FAI may be created by executing $0.
This can be done now, later or manually.
Internet access is needed to download packages.
If unanswered, this script will exit after $TIMEOUT seconds.

EOF
    read -e -t $TIMEOUT -n 1 -p "Install the nfsroot for FAI now? [y|N]: " inp
    inp=${inp:-N}
    case $inp in
	y)
	    check_network
	    setup_nfsroot
	    setup_PXEinstaller
	    ;;
	*)
	    exit 0
	    ;;
    esac
fi

## The following code is activated if diskless machines
## are to be served.  Do not change the following line:
exit 0  ##DISKLESS_SERVER##

## Setup chroot for diskless machines:
if [ ! -d $DLROOT ] ; then
    cat <<EOF
================================================================================
To install the chroot for diskless clients execute:
    export LC_ALL=C
    fai -vNu diskless dirinstall $DLROOT/filesystem.dir/
This can be done right now or manually.
If unanswered, this script will exit after $TIMEOUT seconds.

EOF
    read -e -t $TIMEOUT -n 1 -p "Install the chroot for diskless clients now? [y|N]: " inp
    inp=${inp:-N}
    case $inp in
	y)
	    check_network
	    setup_diskless
	    setup_PXEinstaller
	    ;;
	*)
	    exit 0
	    ;;
    esac
fi
