#!/bin/sh
#
# Prepare for PXE installation of Debian Edu.
#
# A better idea might be to use di-netboot-assistant,
# <URL:http://www.klabs.be/~fpiat/linux/debian/di-netboot-assistant/>.
# See also the original bin/debian-edu-pxelinux.cfg

set -e
set -x

LC_ALL=C
export LC_ALL

# Make sure the created directories and files are readable by tfptd
# run as user nobody.
umask 022

[ "$dist" ]      || dist=$(lsb_release -s -c)
[ "$mirrorurl" ] || mirrorurl=http://ftp.skolelinux.org/debian

# Workaround for bad lsb_release behaviour when only a CD/DVD APT
# source is listed in /etc/sources.list.
if [ "n/a" = "$dist" ] ; then
    dist=lenny
fi

archs="i386 amd64"

# Need to find way to get this from the installed system
#langconfig="debian-installer/locale=nb_NO console-keymaps-at/keymap=no"

tftpdir=/var/lib/tftpboot

# Where to find the preseed file on the web
preseedurl=http://www/debian-edu-install.dat

# Where the preseed file is on the disk.
preseedfile=/var/www/debian-edu-install.dat

[ -d $tftpdir ] || mkdir $tftpdir
[ -d $tftpdir/debian-edu ] || mkdir $tftpdir/debian-edu


for arch in $archs ; do
    (
	[ -d $tftpdir/debian-edu/debian-installer ] || \
	    mkdir $tftpdir/debian-edu/debian-installer
	cd $tftpdir/debian-edu/debian-installer
	if [ ! -f netboot-$arch.tar.gz ] ; then
	    url=$mirrorurl/dists/$dist/main/installer-$arch/current/images/netboot/gtk/netboot.tar.gz
	    echo "Fetching $url"
	    wget -q -O netboot-$arch.tar.gz.new $url && \
		mv netboot-$arch.tar.gz.new netboot-$arch.tar.gz
	    tar --strip-components=2 -zxvf netboot-$arch.tar.gz
	fi
    )
done

echo "Generating $preseedfile"
(
    cat <<EOF
# Enable the Debian Edu installer overrides
d-i     anna/choose_modules     multiselect     debian-edu-install-udeb: Execute Debian-Edu debian-installer profile

# Just accept the proposed hostname and domain, as it is dynamically
# fetched from DNS.
d-i     netcfg/get_hostname     seen    true
d-i     netcfg/get_domain       seen    true

# location of the web proxy, in case it is required
d-i     mirror/http/proxy       string  http://webcache:3128

# list extra packages here
#d-i     pkgsel/include string acroread

# if you want popcon enabled by default
#d-i     debian-edu-install/participate-popcon boolean true
#d-i     debian-edu-install/participate-popcon seen true

# Tell LTSP to not use the CDROM, but a HTTP mirror
d-i ltsp-client-builder ltsp-client-builder/use_cdrom boolean false
d-i ltsp-client-builder/build-client-opts string --mirror $mirrorurl --dist $dist
EOF
    # Enable test repository during testing, but not for production
    if grep -q "\-test" /etc/apt/sources.list ; then
	cat <<EOF
# Test install, activate test repository.  Must use local1, as
# local0 is used internally by the debian-edu install scripts
d-i apt-setup/local1/repository string http://ftp.skolelinux.org/skolelinux $dist-test local
d-i apt-setup/local1/comment string Skolelinux $dist-test repository
d-i apt-setup/local1/source boolean true
EOF
    fi
) > $preseedfile

if [ "$passwdmd5hash" ]; then
    cat <<EOF > $tftpdir/debian-edu/menupasswd.cfg
        MENU PASSWD $passwdmd5hash
EOF
fi

menufile=$tftpdir/debian-edu/install.cfg
echo "Generating $menufile"
(
    for arch in $archs ; do
        cat <<EOF
	# Based on the values used in Debian, and added the preseed URL
LABEL neteduboot-$arch
        MENU LABEL Install Edu/$arch
        kernel debian-installer/$arch/linux
        append video=vesa:ywrap,mtrr vga=788 initrd=debian-installer/$arch/initrd.gz -- quiet url=$preseedurl domain=intern $langconfig
include menupassword.cfg

EOF
    done
    echo "MENU SEPARATOR"
) > $menufile

if [ -f /boot/memtest86.bin ] ; then
    [ -d $tftpdir/debian-edu ] || mkdir $tftpdir/debian-edu
    cp /boot/memtest86.bin $tftpdir/debian-edu/memtest86
    cat <<EOF > $tftpdir/debian-edu/memtest.cfg

LABEL memtest86
        MENU    LABEL  Test ^memory
        kernel  memtest86

MENU SEPARATOR

EOF
fi

cat <<EOF > $tftpdir/debian-edu/localboot.cfg
LABEL localboot
        MENU LABEL Boot from ^local harddrive
        MENU DEFAULT
        localboot 0
MENU SEPARATOR

EOF

ln -sf /usr/lib/syslinux/pxelinux.0 $tftpdir/debian-edu/pxelinux.0
ln -sf /usr/lib/syslinux/vesamenu.c32 $tftpdir/debian-edu/vesamenu.c32
ln -sf /usr/share/pixmaps/splash/debian-edu-splash.png $tftpdir/debian-edu/debian-edu-splash.png

pxecfgdir=$tftpdir/debian-edu/pxelinux.cfg
[ -d  $pxecfgdir ] || mkdir $pxecfgdir
defaultfile=$pxecfgdir/default
echo "Generating $defaultfile"
(
    cat <<EOF
include localboot.cfg
include install.cfg
include ltsp-thin.cfg
include ltsp-diskless.cfg
include memtest.cfg

EOF
    for arch in $archs ; do
        echo "include debian-installer/$arch/boot-screens/menu.cfg"
    done
    cat <<EOF

menu background debian-edu-splash.png
default vesamenu.c32
prompt 0
timeout 0
EOF
) > $defaultfile
