# PPK2 bench-DUT: local recipes (multi-board).
#
# Standalone justfile: invoked from inside tests/hil/bench-dut/
# only. The top-level repo justfile deliberately doesn't surface
# these recipes because building requires the Nordic NCS toolchain
# and a board on USB, neither of which is in scope for `just ci`.
#
# Toolchain: the nrfutil-managed NCS install at /opt/nordic/ncs
# (NCS_VERSION picks the version; NCS_WORKSPACE overrides the path).
#
# Supported boards (each has a committed overlay under boards/):
#   nrf54lm20dk/nrf54lm20b/cpuapp   (default)
#   nrf54l15dk/nrf54l15/cpuapp
#   nrf52840dk/nrf52840
#
# Per-board build trees live under build/<dk>/; hex
# artefacts (git-ignored) are bench-dut-<dk>.hex where <dk> is the first segment
# of the board target.

NCS_VERSION   := env_var_or_default("NCS_VERSION", "v3.3.1")
NCS_WORKSPACE := env_var_or_default("NCS_WORKSPACE", "/opt/nordic/ncs/" + NCS_VERSION)
DK_SERIAL     := env_var_or_default("DK_SERIAL", "")

BOARD_54LM20 := "nrf54lm20dk/nrf54lm20b/cpuapp"
BOARD_54L15  := "nrf54l15dk/nrf54l15/cpuapp"
BOARD_52840  := "nrf52840dk/nrf52840"

APP_PATH := justfile_directory()

# Show available recipes.
_default:
    @just --list --unsorted

# Toolchain wrapper: runs args inside the NCS toolchain context with
# CWD set to the workspace so west finds its manifest. Private (`_`
# prefix); composed by `build` / `flash` below.
_tc *args:
    @cd "{{NCS_WORKSPACE}}" && nrfutil sdk-manager toolchain launch --ncs-version {{NCS_VERSION}} -- {{args}}

# Per-board build trees go under build/<dk>/ so artefacts don't
# clobber each other.
# Build for one board (default: nRF54LM20DK).
build board=BOARD_54LM20:
    @just _tc west build --board "{{board}}" --pristine=auto --build-dir "{{APP_PATH}}/build/$(echo '{{board}}' | cut -d/ -f1)" "{{APP_PATH}}"

# Build, then refresh the local bench-dut-<dk>.hex artefact
# (git-ignored; the CI firmware matrix builds these per push).
hex board=BOARD_54LM20: (build board)
    @tag="$(echo '{{board}}' | cut -d/ -f1)" && cp "{{APP_PATH}}/build/$tag/merged.hex" "{{APP_PATH}}/bench-dut-$tag.hex" && echo "wrote bench-dut-$tag.hex"

# Rebuild the hex artefacts for all three DKs.
hex-all: (hex BOARD_54LM20) (hex BOARD_54L15) (hex BOARD_52840)

# DK_SERIAL=… targets a specific dev kit (`nrfutil device list`
# shows serial numbers); empty means the only attached DK.
# Flash the most-recently-built hex for a board via host nrfutil-device.
flash board=BOARD_54LM20:
    @tag="$(echo '{{board}}' | cut -d/ -f1)" && hex="{{APP_PATH}}/build/$tag/merged.hex" && if [ ! -f "$hex" ]; then echo "build first: just build {{board}}"; exit 1; fi && nrfutil device program --firmware "$hex" --options chip_erase_mode=ERASE_ALL {{ if DK_SERIAL == "" { "" } else { "--serial-number " + DK_SERIAL } }}

# Wipe all build trees (keeps the local hex artefacts).
clean:
    rm -rf "{{APP_PATH}}/build"

# Print resolved settings (debug helper).
which:
    @echo "NCS_WORKSPACE = {{NCS_WORKSPACE}}"
    @echo "NCS_VERSION   = {{NCS_VERSION}}"
    @echo "DK_SERIAL     = {{DK_SERIAL}}"
    @echo "APP_PATH      = {{APP_PATH}}"
    @echo "boards        = {{BOARD_54LM20}} | {{BOARD_54L15}} | {{BOARD_52840}}"
