#!/usr/bin/env bash
# Build pm-kit for the RP2350 and produce pm-kit.uf2.
# Flash: hold BOOTSEL on the Pico 2, plug in USB, drag pm-kit.uf2 onto the RP2350 drive.
#
#   ./build.sh
#
# Override the runtime with RUNTIME=docker ./build.sh
set -euo pipefail

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"   # rust/pm-kit
REPO="$(cd "$DIR/../.." && pwd)"                       # repo root
RUNTIME="${RUNTIME:-podman}"
IMG="pm-rust:2"

"$RUNTIME" run --rm -v "$REPO":/work:Z -w /work/rust/pm-kit "$IMG" bash -c '
  set -e
  rustup component add llvm-tools-preview >/dev/null 2>&1 || true
  cargo build --release
  OBJCOPY="$(rustc --print sysroot)/lib/rustlib/$(rustc -vV | sed -n "s/host: //p")/bin/llvm-objcopy"
  "$OBJCOPY" -O binary target/thumbv8m.main-none-eabihf/release/pm-kit pm-kit.bin
'
python3 "$DIR/uf2.py" "$DIR/pm-kit.bin" "$DIR/pm-kit.uf2"
echo "→ $DIR/pm-kit.uf2  (hold BOOTSEL on the Pico 2, drag this onto the RP2350 drive)"
