@echo off
setlocal enabledelayedexpansion

echo ==========================================
echo     MEDiThings Bladder Patch Programming
echo     (mergehex + nrfutil 8.x)
echo ==========================================

REM -----------------------------------------------------
REM Create hex output folder
REM -----------------------------------------------------
if not exist hex mkdir hex

REM -----------------------------------------------------
REM 1. Copy HEX files
REM -----------------------------------------------------
echo [1/7] Copying HEX files...
copy "..\pca10056\s140\arm5_no_packs\_build\nrf52840_xxaa.hex" hex\app.hex
copy "..\..\..\dfu\secure_bootloader\pca10056_s140_ble\arm5_no_packs\_build\nrf52840_xxaa_s140.hex" hex\boot.hex

if %errorlevel% neq 0 (
    echo ERROR: failed copying files.
    pause
    exit /b 1
)

REM -----------------------------------------------------
REM 2. Generate Bootloader DFU Settings
REM -----------------------------------------------------
echo [2/7] Generating Bootloader DFU settings...
nrfutil settings generate ^
    --family NRF52840 ^
    --application hex\app.hex ^
    --application-version 1 ^
    --bootloader-version 1 ^
    --bl-settings-version 2 ^
    hex\settings.hex

if %errorlevel% neq 0 (
    echo ERROR: nrfutil settings failed.
    pause
    exit /b 1
)

REM -----------------------------------------------------
REM 3. Merge HEX (SoftDevice + Application)
REM -----------------------------------------------------
echo [3/7] Merging SoftDevice + Application...
mergehex.exe --merge s140_nrf52_7.2.0_softdevice.hex hex\app.hex --output hex\part1.hex

if %errorlevel% neq 0 (
    echo ERROR: mergehex part1 failed.
    pause
    exit /b 1
)

REM -----------------------------------------------------
REM 4. Merge HEX (Bootloader + Settings)
REM -----------------------------------------------------
echo [4/7] Merging Bootloader + Settings...
mergehex.exe --merge hex\boot.hex hex\settings.hex --output hex\part2.hex

if %errorlevel% neq 0 (
    echo ERROR: mergehex part2 failed.
    pause
    exit /b 1
)

REM -----------------------------------------------------
REM 5. Final HEX merge (Combine everything)
REM -----------------------------------------------------
echo [5/7] Creating final combined HEX...
mergehex.exe --merge hex\part1.hex hex\part2.hex --output hex\firmware_all.hex

if %errorlevel% neq 0 (
    echo ERROR: mergehex final merge failed.
    pause
    exit /b 1
)

echo Final merged HEX: hex\firmware_all.hex

REM -----------------------------------------------------
REM 6. Detect device SERIAL NUMBER
REM -----------------------------------------------------
echo [6/7] Detecting device serial number...

for /f %%A in ('
    powershell -Command "(nrfutil device list --json | Select-String '\"type\":\"info\"' | ConvertFrom-Json).data.devices[0].serialNumber"
') do set SERIALNUMBER=%%A

if "%SERIALNUMBER%"=="" (
    echo ERROR: No serial number found.
    pause
    exit /b 1
)

echo Using Serial Number: %SERIALNUMBER%
echo Flashing: recover → erase → program → reset

REM recover
nrfutil device recover --serial-number %SERIALNUMBER%
if %errorlevel% neq 0 goto flash_fail

REM erase
nrfutil device erase --serial-number %SERIALNUMBER%
if %errorlevel% neq 0 goto flash_fail

REM program
nrfutil device program --firmware hex\firmware_all.hex --serial-number %SERIALNUMBER%
if %errorlevel% neq 0 goto flash_fail

REM reset
nrfutil device reset --serial-number %SERIALNUMBER%
if %errorlevel% neq 0 goto flash_fail

goto flash_success

:flash_fail
echo ERROR: Flashing failed.
pause
exit /b 1

:flash_success

REM -----------------------------------------------------
REM 7. Set Readback Protection (RBP)
REM -----------------------------------------------------
echo [7/7] Setting Readback Protection (ALL)...
nrfutil device protection-set ALL --serial-number %SERIALNUMBER%

echo ==========================================
echo   Programming Complete!
echo   %date% %time%
echo ==========================================
pause
endlocal
