#!/bin/bash
#
# prepare-rpi-boot.sh - Prepares a FAT32 boot directory for Raspberry Pi 3/4/5
# from files installed by kernel-mamba-aarch64 and raspberrypi-firmware packages
#
# Usage: rpi-prepare-boot [-v] <destination_dir> [kernel_version]
#   -v:              verbose output
#   destination_dir: path to the FAT32 boot partition mount point
#   kernel_version:  kernel version string (default: from uname -r)


VERBOSE=0
if [ "$1" = "-v" ] || [ "$1" = "-d" ]; then
    VERBOSE=1
    shift
fi

BOOT_SRC="/boot"
FW_SRC="/usr/share/raspberry-firmware/boot"
DEST="${1:?Usage: $0 [-v] <destination_dir> [kernel_version]}"
KVER="${2:-}"

log() {
    [ "$VERBOSE" = 1 ] && echo "$@"
}

# Auto-detect kernel version if not specified
if [ -z "$KVER" ]; then
    KVER=$(uname -r)
fi

VMLINUX="$BOOT_SRC/vmlinux-$KVER"
DTB_SRC="/lib/modules/$KVER/dts/broadcom"

# Verify source files and directories exist
if [ ! -f "$VMLINUX" ]; then
    echo "ERROR: missing $VMLINUX" >&2
    exit 1
fi

if [ ! -d "$FW_SRC" ]; then
    echo "ERROR: firmware directory $FW_SRC not found" >&2
    exit 1
fi

if [ ! -d "$DTB_SRC" ]; then
    echo "ERROR: DTB directory $DTB_SRC not found" >&2
    exit 1
fi

log "Kernel:    $VMLINUX"
log "DTBs:      $DTB_SRC"
log "Firmware:  $FW_SRC"
log "Dest:      $DEST"

mkdir -p "$DEST"

# 1. Kernel (uncompressed Image expected by RPi firmware)
log "Copying kernel..."
cp "$VMLINUX" "$DEST/kernel8.img" || { echo "ERROR: failed to copy kernel" >&2; exit 1; }

# 2. Initramfs
log "Generating initramfs..."
dracut --force --quiet "$DEST/initramfs8.gz" "$KVER" || { echo "ERROR: failed to generate initramfs" >&2; exit 1; }

# 3. Firmware files (bootcode.bin, start*.elf, fixup*.dat)
log "Copying firmware files..."
cp "$FW_SRC"/bootcode.bin "$DEST/" && \
cp "$FW_SRC"/start*.elf "$DEST/" && \
cp "$FW_SRC"/fixup*.dat "$DEST/" || { echo "ERROR: failed to copy firmware files" >&2; exit 1; }

# 4. Device tree overlays
log "Copying overlays..."
mkdir -p "$DEST/overlays"
cp "$FW_SRC"/overlays/*.dtbo "$DEST/overlays/" 2>/dev/null || true

# 5. Device tree blobs for RPi 3, 4 and 5
log "Copying device tree blobs..."
# RPi 3
cp "$DTB_SRC"/bcm2837-rpi-3-*.dtb "$DEST/" 2>/dev/null || true
cp "$DTB_SRC"/bcm2710-rpi-3-*.dtb "$DEST/" 2>/dev/null || true
cp "$DTB_SRC"/bcm2710-rpi-zero-2*.dtb "$DEST/" 2>/dev/null || true
# RPi 4
cp "$DTB_SRC"/bcm2711-rpi-4*.dtb "$DEST/" 2>/dev/null || true
cp "$DTB_SRC"/bcm2711-rpi-400*.dtb "$DEST/" 2>/dev/null || true
cp "$DTB_SRC"/bcm2711-rpi-cm4*.dtb "$DEST/" 2>/dev/null || true
# RPi 5
cp "$DTB_SRC"/bcm2712-rpi-5*.dtb "$DEST/" 2>/dev/null || true
cp "$DTB_SRC"/bcm2712-d-rpi-5*.dtb "$DEST/" 2>/dev/null || true

# 6. config.txt
if [ ! -f "$DEST/config.txt" ]; then
    log "Creating config.txt..."
    cp "$FW_SRC"/config.txt "$DEST/" || { echo "ERROR: failed to copy config.txt" >&2; exit 1; }
else
    log "config.txt already exists, skipping"
fi

# 7. cmdline.txt
if [ ! -f "$DEST/cmdline.txt" ]; then
    log "Creating cmdline.txt..."
    cp "$FW_SRC"/cmdline.txt "$DEST/" || { echo "ERROR: failed to copy cmdline.txt" >&2; exit 1; }
else
    log "cmdline.txt already exists, skipping"
fi

# Summary
SIZE=$(du -sh "$DEST" | cut -f1)
echo "$DEST ready ($SIZE)"
