#!/bin/bash

set -e

DISTRI="stretch"

if [ "$FAI_ACTION" == "install" ] || [ "$CONVERT" == "true" ] ; then
    cat >> "${target}/etc/di-netboot-assistant/pxelinux.HEAD" <<EOF
LABEL Local Boot
       MENU LABEL Boot from local hard disk
       KERNEL chain.c32
       APPEND hd0
EOF

    if ifclass DISKLESS_SERVER ; then
        cat >> "${target}/etc/di-netboot-assistant/pxelinux.HEAD" <<EOF

LABEL Debian-LAN Live System
       MENU LABEL Debian-LAN Live System
       KERNEL ::/fai/vmlinuz
       ## FIXME #774033 ## APPEND initrd=::/fai/initrd.img ip=dhcp root=nfs4:/live aufs
       APPEND initrd=::/fai/initrd.img ip=dhcp root=/dev/nfs nfsroot=/srv/nfs4/live aufs
EOF
    fi

    cat >> "${target}/etc/di-netboot-assistant/pxelinux.HEAD" <<EOF

LABEL Debian-LAN workstation (amd64)
       MENU LABEL Debian-LAN: Debian Installer + FAI (amd64)
       kernel ::/debian-installer/$DISTRI/amd64/linux
       append initrd=::/debian-installer/$DISTRI/amd64/initrd.gz auto=true priority=critical url=faiserver/d-i/$DISTRI/preseed.cfg

LABEL Debian-LAN workstation (i368)
       MENU LABEL Debian-LAN: Debian Installer + FAI (i386)
       kernel ::/debian-installer/$DISTRI/i386/linux
       append initrd=::/debian-installer/$DISTRI/i386/initrd.gz auto=true priority=critical url=faiserver/d-i/$DISTRI/preseed.cfg

TIMEOUT 30

EOF
    sed -i "s%^\(TFTP_ROOT=.*\)$%\#\1\nTFTP_ROOT=/srv/tftp%" ${target}/etc/di-netboot-assistant/di-netboot-assistant.conf

    mkdir -p ${target}/var/www/html/d-i/$DISTRI
    cat > "${target}/var/www/html/d-i/$DISTRI/preseed.cfg" <<EOF
#### Preconfiguration file
## For more examples and comments:
##   https://www.debian.org/releases/stable/example-preseed.txt

### From the preseeding machine's debconf:
$(debconf-get-selections | grep "^d-i")

### Network setup
d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string unassigned-hostname
d-i netcfg/get_domain string unassigned-domain
d-i netcfg/wireless_wep string
### Mirror settings
d-i mirror/country string manual
d-i mirror/http/hostname string httpredir.debian.org
d-i mirror/http/directory string /debian
d-i mirror/http/proxy string http://aptcache.intern:3128/
### Account setup
d-i passwd/make-user boolean false
d-i passwd/root-password password r00tme
d-i passwd/root-password-again password r00tme
### Clock and time zone setup
d-i clock-setup/utc boolean true
d-i clock-setup/ntp boolean true
d-i clock-setup/ntp-server string ntp.intern
### Partitioning
d-i partman-auto/method string regular
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-md/device_remove_md boolean true
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-auto/choose_recipe select atomic
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
### Apt setup
d-i apt-setup/non-free boolean true
d-i apt-setup/contrib boolean true
### Package selection
#tasksel tasksel/first multiselect standard, web-server
tasksel tasksel/first multiselect
#tasksel tasksel/desktop multiselect kde, xfce
# Individual additional packages to install
d-i pkgsel/include string fai-client nfs-common aptitude
### GRUB on default disk:
d-i grub-installer/bootdev string default
### This command is run just before the install finishes, it does all the FAI magic:
d-i preseed/late_command string mkdir -p /target/var/lib/fai/config; \\
       in-target modprobe nfsv4; \\
       in-target fai -vN -s nfs://faiserver/config softupdate; \\
       in-target passwd -ld root

#d-i finish-install/reboot_in_progress note
EOF
    $ROOTCMD chmod -R o+rX /var/www/html/d-i
fi

$ROOTCMD di-netboot-assistant install $DISTRI --arch=amd64
$ROOTCMD di-netboot-assistant install $DISTRI --arch=i386
$ROOTCMD di-netboot-assistant rebuild-menu

# chain.c32 is needed to boot from the local hard disk:
$ROOTCMD cp /usr/lib/syslinux/modules/bios/chain.c32 /srv/tftp/debian-installer/

#wheezy:
#d-i preseed/late_command string mkdir -p /target/var/lib/fai/config; \
#        mount -t proc proc /target/proc; \
#        chroot /target service rpcbind start; \
#        chroot /target service nfs-common start; \
#        in-target fai -vN -s nfs://faiserver/config softupdate; \
#        in-target passwd -l root
