#! /bin/bash

## Setup script run at boot time.

set -e
umask 0022
export http_proxy="http://aptcache:3128"

############################
URL="httpredir.debian.org"
INSTALLER="/usr/lib/debian-installer/images/*/*/text/debian-installer/"

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

WS_TEMPLATE=$TFTPROOT/pxelinux.cfg/workstation.tmpl
DL_TEMPLATE=$TFTPROOT/pxelinux.cfg/diskless.tmpl
##########

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."
    if [ -x $NFSROOT/usr/bin/apt-get ] ; then
        ## Update nfsroot:
        fai-make-nfsroot -v -k
    else
        fai-setup -f -e -v
    fi

    if [ -e $WS_TEMPLATE ]; then
        cp -v $WS_TEMPLATE ${WS_TEMPLATE}_$(date +%F)
    fi
    KERNEL=`basename $(ls $TFTPROOT/vmlinuz*)`
    INITRD=`basename $(ls $TFTPROOT/initrd.img*)`

    ## Create links needed for di-netboot-installer:
    [ -L $TFTPROOT/vmlinuz ]    || ln -vs $KERNEL $TFTPROOT/vmlinuz
    [ -L $TFTPROOT/initrd.img ] || ln -vs $INITRD $TFTPROOT/initrd.img

    echo "Creating template with $KERNEL and $INITRD."
    cat > $WS_TEMPLATE <<EOF
# template for workstation
default Debian-LAN workstation

label Debian-LAN workstation
kernel $KERNEL
append initrd=$INITRD ip=dhcp root=nfs4:/$(basename $NFSROOT) aufs FAI_FLAGS=verbose,sshd,createvt FAI_CONFIG_SRC=nfs://faiserver/config FAI_ACTION=install
EOF

    ## Create pxelinux boot configuration for workstationXX.
    ## The seq range is sed from the corresponding variable
    ## when fcopy'd:
    echo -n "Creating pxelinux boot configurations: "
    NUM=0
    for IPADDR in `seq WS_RANGE` ; do
        fai-chboot -vc workstation.tmpl PREFIX.$IPADDR &>> /var/log/fai/fai-chboot.log
        echo -n "."
        NUM=$(($NUM+1))
    done
    echo -e " Done.\nCreated $NUM workstation configurations."

    if [ -e $DL_TEMPLATE ] ; then
        fai-chboot -vc diskless.tmpl default &>> /var/log/fai/fai-chboot.log
    else
        ## create default configuration (sysinfo):
        fai-chboot -Svu $FAI_CONFIG_SRC default &>> /var/log/fai/fai-chboot.log
        sed -i "s/fai-generated/FAI System Information/g" $TFTPROOT/pxelinux.cfg/default
    fi
}


setup_diskless () {
    export LC_ALL=C
    if [ -x $DLROOT/usr/sbin/fai ] ; then
        mount -t sysfs sysfs $DLROOT/sys
        mount -t proc  proc  $DLROOT/proc
        chroot $DLROOT fai -vNu diskless softupdate
        umount $DLROOT/sys $DLROOT/proc
    else
        fai -vNu diskless dirinstall $DLROOT
    fi

    if [ -e $DL_TEMPLATE ]; then
        cp -v $DL_TEMPLATE ${DL_TEMPLATE}_$(date +%F)
    fi
    KERNEL=`basename $(ls $TFTPROOT/vmlinuz*)`
    INITRD=`basename $(ls $TFTPROOT/initrd.img*)`

    echo "Creating template with $KERNEL and $INITRD."
    cat > $DL_TEMPLATE <<EOF
# template for diskless
default Debian-LAN/FAI Live System

label Debian-LAN/FAI Live System
kernel $KERNEL
## FIXME #774033 ## append initrd=$INITRD ip=dhcp root=nfs4:/$(basename $DLROOT) aufs
append initrd=initrd.img ip=dhcp root=/dev/nfs nfsroot=/srv/nfs4/live aufs
EOF

    ## Create pxelinux boot configuration for disklessXX.
    ## The seq range is sed from the corresponding variable
    ## when fcopy'd:
    echo -n "Creating pxelinux boot configurations: "
    NUM=0
    for IPADDR in `seq DL_RANGE` ; do
        fai-chboot -vc diskless.tmpl PREFIX.$IPADDR &>> /var/log/fai/fai-chboot.log
        echo -n "."
        NUM=$(($NUM+1))
    done
    echo -e " Done.\nCreated $NUM diskless machine configurations."

    ## Boot unknown machines as diskless:
    fai-chboot -vc diskless.tmpl default &>> /var/log/fai/fai-chboot.log
}

#########################
## 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/update nfsroot for FAI:
read -e -n 1 -p "Install/update the nfsroot for FAI now? [y|N]: " inp
if [ "$inp" = "y" ] ; then
    check_network
    setup_nfsroot
fi

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

if [ -e $NFSROOT/.THIS_IS_THE_FAI_NFSROOT ] ; then
    ## Setup/update chroot for diskless machines:
    read -e -n 1 -p "Install/update the chroot for diskless clients now? [y|N]: " inp
    if [ "$inp" = "y" ] ; then
        check_network
        setup_diskless
    fi
fi

exit 0
