#!/bin/sh

set -e

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

TFTPBOOT=/var/lib/tftpboot/ltsp
PXECFG=$TFTPBOOT/pxelinux.cfg
test -z "$LTSPROOT" && LTSPROOT=/opt/ltsp
ARCH=$(dpkg --print-architecture)
VENDOR=$(lsb_release -i|awk '{ print $3 }')

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* $LTSPROOT/*/boot/System.map*; 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
    if [ $VENDOR = "Ubuntu" ]; then
        cat > $PXECFG/default <<EOF
DEFAULT vmlinuz ro initrd=initrd.img quiet splash
EOF
    else 
	    cat > $PXECFG/default <<EOF
DEFAULT vmlinuz ro initrd=initrd.img root=/dev/nfs ip=dhcp
EOF
    fi
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 --ip=dhcp -o $nbi $kernel $initrd
        else
            mkelf-linux --ip=dhcp -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

if which elftoaout >/dev/null ; then
    for kernel in $TFTPBOOT/vmlinuz*; do
        basename=$(basename "$kernel")
        SUB_ARCH=$(uname -m)
        piggyback_cmd=""
        case $SUB_ARCH in
            sparc64) piggyback_cmd=piggyback64 ;;
            sparc32) piggyback_cmd=piggyback32 ;;
        esac
        if [ "$basename" = "vmlinuz" ]; then
            initrd=$TFTPBOOT/initrd.img
            sysmap=$TFTPBOOT/System.map
            nbi=$TFTPBOOT/$SUB_ARCH.img
        else
            version=${basename##vmlinuz-}
            initrd=$TFTPBOOT/initrd.img-$version
            sysmap=$TFTPBOOT/System.map-$version
            nbi=$TFTPBOOT/$SUB_ARCH.img-$version
        fi
        # TODO: proper tempfile handline
        gzip -cd $kernel > $nbi.tmp
        elftoaout -o $nbi $nbi.tmp
        if [ -f "$initrd" ]; then
            $piggyback_cmd $nbi $sysmap $initrd
        else
            $piggyback_cmd $nbi $sysmap
        fi
    done
else
    echo "Skipping sparc piggyback images. Install the sparc-utils 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
