#!/bin/sh -e

# this script is run either chrooted on the server, or by a client with write
# access to the NFS mount point. (much of this code was originally in
# server/ltsp-update-kernels). --vagrant 20060801

if [ -f /etc/ltsp/update-kernels.conf ]; then
    . /etc/ltsp/update-kernels.conf
fi

BOOT=${BOOT:-"/boot"}
SUB_ARCH=${SUB_ARCH:-"$(uname -m)"}
VENDOR=${VENDOR:-"$(lsb_release -i -s)"}

case $0 in
  /etc/kernel/post*.d*) QUIET=true
esac

msg() {
    if [ "$QUIET" != "true" ]; then
       echo $@ 
    fi
}

if [ -z "$CHROOT_NAME" ]; then
    # FIXME: replace with a common function
    CHROOT_NAME="$(dpkg --print-installation-architecture)"
fi

if [ -z "$BOOTPROMPT_OPTS" ]; then
    if [ "$VENDOR" = "Ubuntu" ]; then
        BOOTPROMPT_OPTS="quiet splash"
    else 
        BOOTPROMPT_OPTS="root=/dev/nfs ip=dhcp"
    fi
fi

if [ -f /usr/lib/yaboot/yaboot ]; then
    cp -a -v /usr/lib/yaboot/yaboot $BOOT
    cat > $BOOT/yaboot.conf <<EOF
timeout=0
default=ltsp
root=/dev/ram

image=/ltsp/$CHROOT_NAME/vmlinux
        label=ltsp
        initrd=/ltsp/$CHROOT_NAME/initrd.img
        append="$BOOTPROMPT_OPTS"
EOF

else
    msg "Skipping yaboot configuration. install yaboot package if you need it."
fi    

if [ -f /usr/lib/syslinux/pxelinux.0 ]; then
    PXECFG=$BOOT/pxelinux.cfg
    cp /usr/lib/syslinux/pxelinux.0 $BOOT
    if [ ! -d $PXECFG ]; then
        mkdir $PXECFG
    fi
    if [ -z "$PXELINUX_CMDLINE" ]; then
        PXELINUX_CMDLINE="DEFAULT vmlinuz ro initrd=initrd.img $BOOTPROMPT_OPTS"
    fi
    cat > $PXECFG/default <<EOF
$PXELINUX_CMDLINE
EOF

else
    msg "Skipping PXE configuration.  Install the syslinux package if you need it."
fi

# allow specifying a specific kernel image to update, from kernel postinst
if [ -f "$2" ]; then
    ALL_KERNELS="$2"
else
    ALL_KERNELS="$(find $BOOT -type f -name 'vmlinu*')"
fi

# look for symlinks, too, and put them after the "real" kernels
ALL_KERNELS="$ALL_KERNELS $(find $BOOT -type l -name 'vmlinu*')"

for kernel in $ALL_KERNELS ; do
    basename=$(basename "$kernel")
    initrd=initrd.img
    nbi=nbi.img

    case $basename in
        vmlinuz|vmlinux)
            # USE DEFAULT
        ;;
        vmlinu*.old) 
            initrd=$initrd.old
            nbi=$nbi.old
        ;;
        vmlinuz*) 
            version=${basename##vmlinuz-}
            initrd=$initrd-$version
            nbi=$nbi-$version
        ;;
        vmlinux*) 
            version=${basename##vmlinux-}
            initrd=$initrd-$version
            nbi=$nbi-$version
        ;;
    esac

    if [ -L "$kernel" ]; then
        basename="$(readlink $kernel)"
        if [ -f "$BOOT/$basename" ]; then
            case $basename in
                vmlinuz*)
                    version=${basename##vmlinuz-}
                ;;
                vmlinux*)
                    version=${basename##vmlinux-}
                ;;
            esac
            case $SUB_ARCH in
                sparc*) 
                    realnbi="nbi.img-$version-$SUB_ARCH" 
                ;;
                *)
                    realnbi="nbi.img-$version"
                ;;
            esac
            if [ -f "$BOOT/$realnbi" ]; then
                ln -sf $realnbi $BOOT/$nbi
            fi
        fi
    else
        if which mkelf-linux >/dev/null; then
            if [ -z "$MKELF_LINUX_OPTS" ]; then
                MKELF_LINUX_OPTS="--ip=dhcp"
            fi
            if [ -f "$BOOT/$initrd" ]; then
                mkelf-linux $MKELF_LINUX_OPTS -o $BOOT/$nbi $kernel $BOOT/$initrd
            else
                mkelf-linux $MKELF_LINUX_OPTS -o $BOOT/$nbi $kernel
            fi
        else
            if [ -z "$mkelf_seen" ]; then
                msg "Skipping etherboot images.  Install the mknbi package if you need them."
                mkfelf_seen=true
            fi
        fi
        if which netabootwrap >/dev/null; then
            if [ -f "$BOOT/$initrd" ]; then
                netabootwrap -t $BOOT/$nbi -k $kernel -i $BOOT/$initrd
            else
                netabootwrap -t $BOOT/$nbi -k $kernel
            fi
        else
            if [ -z "$netabootwrap_seen" ]; then
                msg "Skipping netabootwrap images.  Install the aboot package if you need them."
                netabootwrap_seen=true
            fi
        fi
        if which elftoaout >/dev/null ; then
            piggyback_cmd=""
            case $SUB_ARCH in
                sparc64) piggyback_cmd=piggyback64 ;;
                sparc32) piggyback_cmd=piggyback32 ;;
            esac
            sysmap=$BOOT/System.map-$version
            nbi=$nbi-$SUB_ARCH

            # TODO: proper tempfile handline
            gzip -cd $kernel > $nbi.tmp
            elftoaout -o $nbi $nbi.tmp
            if [ -f "$BOOT/$initrd" ]; then
                $piggyback_cmd $BOOT/$nbi $sysmap $BOOT/$initrd
            else
                $piggyback_cmd $BOOT/$nbi $sysmap
            fi
        else
            if [ -z "$sparc_piggyback_seen" ]; then
                msg "Skipping sparc piggyback images. Install the sparc-utils package if you need them."
                sparc_piggyback_seen=true
            fi
        fi
    fi
done
