#!/bin/sh
set -e

version="$1"

# avoid running multiple times
if [ -n "$DEB_MAINT_PARAMS" ]; then
        eval set -- "$DEB_MAINT_PARAMS"
        if [ -z "$1" ] || [ "$1" != "configure" ]; then
                exit 0
        fi
fi

# do not clobber pre-signed stubble
if [ -e /boot/stubble.efi-$version ]; then
    exit 0
fi

dtbs=$(/usr/libexec/stubble/finddtbs.py /usr/lib/firmware/$version/device-tree /usr/share/stubble/hwids)
echo "$dtbs"

# XXX: we should have the list of hwids split by supported kernel.
echo "II: making /boot/stubble.efi-$version from /boot/vmlinuz-$version"
/usr/bin/ukify build --linux=/boot/vmlinuz-$version \
    --stub=/usr/lib/stubble/stubble.efi \
    $(echo "$dtbs" | xargs -i echo "--devicetree-auto={}") \
    --hwids=/usr/share/stubble/hwids \
    --sbat="@/usr/share/stubble/sbat" \
    --output=/boot/stubble.efi-$version
