# /boot/vmlinuz symlinks are generated when "link_in_boot=yes" is specified
# in /etc/kernel-img.conf, which is generated by ltsp-build-client, which
# normally isn't used when ltsp-update-image --cleanup / is called.

if [ -f /boot/vmlinuz ] && [ -f /boot/initrd.img ]; then
    return 0
fi

if [ -f /etc/ltsp/update-kernels.conf ]; then
    . /etc/ltsp/update-kernels.conf
fi
KERNEL_NAMES=${KERNEL_NAMES:-'s/\(vmlinu[xz]-\)\([^-]*-[^-]*-\)\(.*\)/& \1 \2 \3/p'}
INITRD_NAME=${INITRD_NAME:-'s/vmlinu[xz]/initrd.img/p'}

read file name version flavor <<EOF
$(find "/boot/" -mindepth 1 -maxdepth 1 -type f -printf "%f\n" |
    sed -n "$KERNEL_NAMES" | sort -V -k 4,4  | sort -r -k 3,3)
EOF
initrd=$(echo "$file" | sed -n "$INITRD_NAME")

if [ -f "/boot/$file" ] && [ -f "/boot/$initrd" ]; then
    ln -sf "$file" /boot/vmlinuz
    ln -sf "$initrd" /boot/initrd.img
    nbi=$(echo "$initrd" | sed 's/initrd/nbi/')
    if [ -f "/boot/$nbi" ]; then
        ln -sf "$nbi" /boot/nbi.img
    else
        rm -f /boot/nbi.img
    fi
fi
