#!/bin/sh

set -e

# Make sure the copied files are readable by all
umask 022

TFTPBOOT=/var/lib/tftpboot/ltsp
PXECFG=$TFTPBOOT/pxelinux.cfg
LTSPROOT=/opt/ltsp
ARCH=$(dpkg --print-architecture)

if [ ! -d $TFTPBOOT ] ; then
    echo "error: Directory $TFTPBOOT is missing.  Unable to continue"
    exit 1
fi

case $ARCH in
	powerpc)
	KERNEL=vmlinux
	;;
	*)
	KERNEL=vmlinuz
	;;
esac

for file in $LTSPROOT/*/boot/$KERNEL* $LTSPROOT/*/boot/initrd.img*; do
    cp -a -v "$file" $TFTPBOOT
done

if [ -f /usr/lib/syslinux/pxelinux.0 ]; then
    cp /usr/lib/syslinux/pxelinux.0 $TFTPBOOT
    if [ ! -d $PXECFG ]; then
        mkdir $PXECFG
    fi
    cat > $PXECFG/default <<EOF
DEFAULT vmlinuz ro initrd=initrd.img quiet splash
EOF
else
    echo "Skipping PXE images.  Install the syslinux package if you need them."
fi

if [ -f /usr/lib/yaboot/yaboot ]; then
	cp /usr/lib/yaboot/yaboot $TFTPBOOT
	YABOOTCONFROOT=$(echo $TFTPBOOT|sed s/ltsp//g)
	cat > $YABOOTCONFROOT/yaboot.conf <<EOF
timeout=0
default=ltsp
root=/dev/ram

image=/ltsp/vmlinux
        label=ltsp
        initrd=/ltsp/initrd.img
        append="quiet splash"
EOF
else
	echo "Not on powerpc, skipping yaboot installation."
fi

if which mkelf-linux >/dev/null; then
    for kernel in $TFTPBOOT/vmlinuz*; do
        basename=$(basename "$kernel")

        if [ "$basename" = "vmlinuz" ]; then
            initrd=$TFTPBOOT/initrd.img
            nbi=$TFTPBOOT/nbi.img
        else
            version=${basename##vmlinuz-}
            initrd=$TFTPBOOT/initrd.img-$version
            nbi=$TFTPBOOT/nbi.img-$version
        fi

        if [ -f "$initrd" ]; then
            mkelf-linux -o $nbi $kernel $initrd
        else
            mkelf-linux -o $nbi $kernel
        fi
    done
else
    echo "Skipping etherboot images.  Install the mknbi package if you need them."
fi

if which mkvmlinuz >/dev/null; then
  # mkvmlinuz already happens on powerpc as part of the kernel install-
  # so do nothing.
  true
else
    echo "Skipping openfirmware images.  Install the mkvmlinuz package if you need them."
fi

# TODO merge the mkelf-linux and netabootwrap code
if which netabootwrap >/dev/null; then
    for kernel in $TFTPBOOT/vmlinuz*; do
        basename=$(basename "$kernel")

        if [ "$basename" = "vmlinuz" ]; then
            initrd=$TFTPBOOT/initrd.img
            nbi=$TFTPBOOT/nbi.img
        else
            version=${basename##vmlinuz-}
            initrd=$TFTPBOOT/initrd.img-$version
            nbi=$TFTPBOOT/nbi.img-$version
        fi
        # TODO: work around for debian bug #270801
        if [ -f "$initrd" ]; then
            netabootwrap -t $nbi -k $kernel -i $initrd
        else
            netabootwrap -t $nbi -k $kernel
        fi
    done
else
    echo "Skipping netabootwrap images.  Install the aboot package if you need them."
fi

#for file in $TFTPBOOT/*; do
#    found=""
#    for master in $LTSPROOT/*/boot/$(basename "$file"); do
#        if [ -e "$master" ]; then
#            found=1
#            break
#        fi
#    done
#
#    if [ -z "$found" ]; then
#        rm -f "$file"
#    fi
#done
