@echo off
setlocal enabledelayedexpansion

:: ESP32 flash helper - Technimal EDGE I/O modules (E02SA / E04SD)
:: Flashes a merged.bin downloaded from the web flasher.
::   Usage: flash_esp32.bat <PORT> <FILE>     e.g.  flash_esp32.bat COM5 merged.bin
::
:: Works with esptool v4 and v5: v5 renamed the commands and options (write_flash ->
:: write-flash, --flash_mode -> --flash-mode, default_reset -> default-reset), and each
:: major version warns or errors on the other's spelling. The version is detected below
:: and the matching spelling used, so there are no deprecation warnings either way.

if "%~1"=="" goto usage
if "%~2"=="" goto usage

set "PORT=%~1"
set "FILE=%~2"

if not exist "%FILE%" (
    echo [ERROR] File not found: %FILE%
    echo Please verify the file path and try again.
    pause
    exit /b 1
)

:: Prefer the esptool launcher; fall back to the Python module.
set "ESPTOOL=esptool"
where esptool >nul 2>&1 || set "ESPTOOL=python -m esptool"

:: Pick the option spelling for the esptool actually installed (see note at the top).
:: esptool v4 prints "esptool.py v4.x.y"; v5 prints "esptool v5.x.y".
set "ESPVER=5"
for /f "delims=" %%v in ('%ESPTOOL% version 2^>nul') do (
    echo %%v | findstr /c:"v4." >nul && set "ESPVER=4"
)
if "%ESPVER%"=="4" (
    set "WRITE=write_flash"
    set "BEFORE=default_reset"
    set "AFTER=hard_reset"
    set "F_MODE=--flash_mode"
    set "F_FREQ=--flash_freq"
    set "F_SIZE=--flash_size"
) else (
    set "WRITE=write-flash"
    set "BEFORE=default-reset"
    set "AFTER=hard-reset"
    set "F_MODE=--flash-mode"
    set "F_FREQ=--flash-freq"
    set "F_SIZE=--flash-size"
)

echo --------------------------------------------------
echo [INFO] Starting ESP32 Flashing Process...
echo [INFO] TARGET PORT: %PORT%
echo [INFO] TARGET FILE: %FILE%
echo [INFO] ESPTOOL:     %ESPTOOL% ^(v%ESPVER%^)
echo --------------------------------------------------

%ESPTOOL% --chip esp32 --port %PORT% --baud 460800 --before %BEFORE% --after %AFTER% %WRITE% %F_MODE% dio %F_FREQ% 40m %F_SIZE% detect 0x0 "%FILE%"
set "RC=%errorlevel%"

if "%RC%"=="0" (
    echo --------------------------------------------------
    echo [SUCCESS] ESP32 has been flashed successfully!
    echo --------------------------------------------------
) else (
    echo --------------------------------------------------
    echo [ERROR] An error occurred during the flashing process.
    echo   - Check the port in Device Manager ^(Ports COM ^& LPT^)
    echo   - Close any serial monitor still holding %PORT%
    echo   - If it cannot connect, hold BOOT ^(GPIO0^) while starting
    echo   - Missing esptool? run:  pip install esptool
    echo --------------------------------------------------
)

pause
exit /b %RC%

:usage
echo ==================================================
echo   ESP32 Flash Script Usage Guide
echo ==================================================
echo.
echo Command Syntax:
echo   flash_esp32.bat [PORT] [FILE_PATH]
echo.
echo Example:
echo   flash_esp32.bat COM5 merged.bin
echo.
echo Find your port: Device Manager - Ports ^(COM ^& LPT^)
echo.
echo *Note: Wrap the file path in double quotes if it contains spaces.
echo.
pause
exit /b 1
