#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2015-2016, 2026 InterGenJLU
#
# shim-signed — extract Microsoft-signed shim from Fedora RPM
# Per D1-7 decision: piggyback on Fedora's shim until our own MS-signed
# shim ships post-Monday.

configure() {
    set -e
    # No configure step — we're extracting prebuilt binaries
    return 0
}

build() {
    set -e
    # Orchestrator's extract_source() (scripts/pkg-functions.sh) handles
    # rpm2cpio | cpio extraction for .rpm sources since 2026-05-23 18:14
    # CDT; PWD already contains boot/efi/EFI/fedora/ at this point. Just
    # verify the binaries we expect are present.
    test -f boot/efi/EFI/fedora/shimx64.efi || {
        echo "ERROR: shimx64.efi not found in extracted RPM" >&2
        exit 1
    }
    test -f boot/efi/EFI/fedora/mmx64.efi || {
        echo "ERROR: mmx64.efi not found in extracted RPM" >&2
        exit 1
    }
}

do_install() {
    set -e
    # Stage shim binaries under /usr/share/shim-signed/ where Forge expects
    install -d "$DESTDIR/usr/share/shim-signed"
    install -m 0644 boot/efi/EFI/fedora/shimx64.efi "$DESTDIR/usr/share/shim-signed/"
    install -m 0644 boot/efi/EFI/fedora/mmx64.efi  "$DESTDIR/usr/share/shim-signed/"
    if [ -f boot/efi/EFI/fedora/shimx64-fedora.cer ]; then
        install -m 0644 boot/efi/EFI/fedora/shimx64-fedora.cer "$DESTDIR/usr/share/shim-signed/"
    fi
}
