@echo off
setlocal EnableExtensions EnableDelayedExpansion
goto :Main

:Require
if not exist "%~1" (
    echo.
    echo ERROR: missing file:
    echo   %~1
    echo Run: idf.py build
    echo.
    pause
    exit /b 1
)
exit /b 0

:ReadVersion
for /f "usebackq tokens=2 delims==" %%A in (`findstr /b "CONFIG_MY_APP_VERSION" "%~1" 2^>nul`) do (
    set "VER=%%~A"
)
exit /b 0

:HashFile
set "HASH="
for /f "skip=1 usebackq delims=" %%H in (`certutil -hashfile "%~1" SHA256 2^>nul`) do (
    set "HASH=%%H"
    goto :HashDone
)
:HashDone
set "HASH=!HASH: =!"
set "%~2=!HASH!"
exit /b 0

:AppendJsonFile
call :HashFile "%~1" H
for %%A in ("%~1") do (
    set "FN=%%~nxA"
    set "SZ=%%~zA"
)
if "%~2"=="last" (
    >>"%REL%\manifest.json" echo     {"name":"!FN!","bytes":!SZ!,"sha256":"!H!"}
) else (
    >>"%REL%\manifest.json" echo     {"name":"!FN!","bytes":!SZ!,"sha256":"!H!"},
)
exit /b 0

:Main
REM Copy idf.py build -> firmware/release/ (double-click OK)
cd /d "%~dp0"
cd /d ".."
set "ROOT=%CD%"

set "BUILD=%ROOT%\build"
set "REL=%ROOT%\firmware\release"
set "OTA=%REL%\ota"
set "FAC=%REL%\factory"

call :Require "%BUILD%\ESPRCCar.bin"
call :Require "%BUILD%\bootloader\bootloader.bin"
call :Require "%BUILD%\partition_table\partition-table.bin"
call :Require "%BUILD%\ota_data_initial.bin"
call :Require "%BUILD%\storage.bin"

if not exist "%OTA%" mkdir "%OTA%"
if not exist "%FAC%" mkdir "%FAC%"

echo [1/4] Copy binaries ...
copy /Y "%BUILD%\ESPRCCar.bin" "%OTA%\ESPRCCar.bin" >nul
copy /Y "%BUILD%\ESPRCCar.bin" "%FAC%\ESPRCCar.bin" >nul
copy /Y "%BUILD%\bootloader\bootloader.bin" "%FAC%\bootloader.bin" >nul
copy /Y "%BUILD%\partition_table\partition-table.bin" "%FAC%\partition-table.bin" >nul
copy /Y "%BUILD%\ota_data_initial.bin" "%FAC%\ota_data_initial.bin" >nul
copy /Y "%BUILD%\storage.bin" "%FAC%\storage.bin" >nul
if exist "%BUILD%\flash_args" copy /Y "%BUILD%\flash_args" "%FAC%\flash_args.txt" >nul

echo [2/4] Read version ...
set "VER=unknown"
if exist "%ROOT%\sdkconfig" call :ReadVersion "%ROOT%\sdkconfig"
if "!VER!"=="unknown" if exist "%ROOT%\sdkconfig.defaults" call :ReadVersion "%ROOT%\sdkconfig.defaults"

set "UTC=unknown"
for /f "delims=" %%T in ('powershell -NoProfile -Command "(Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ')" 2^>nul') do set "UTC=%%T"
set "LOCAL=%date% %time%"

echo [3/4] Write manifest.json ...
> "%REL%\manifest.json" echo {
>>"%REL%\manifest.json" echo   "project": "ESPRCCar",
>>"%REL%\manifest.json" echo   "chip": "esp32s3",
>>"%REL%\manifest.json" echo   "flash_mode": "dio",
>>"%REL%\manifest.json" echo   "flash_freq": "80m",
>>"%REL%\manifest.json" echo   "flash_size": "16MB",
>>"%REL%\manifest.json" echo   "version": "!VER!",
>>"%REL%\manifest.json" echo   "generated_utc": "!UTC!",
>>"%REL%\manifest.json" echo   "ota_image": "release/ota/ESPRCCar.bin",
>>"%REL%\manifest.json" echo   "factory_dir": "release/factory",
>>"%REL%\manifest.json" echo   "files": [
call :AppendJsonFile "%OTA%\ESPRCCar.bin"
call :AppendJsonFile "%FAC%\bootloader.bin"
call :AppendJsonFile "%FAC%\partition-table.bin"
call :AppendJsonFile "%FAC%\ESPRCCar.bin"
call :AppendJsonFile "%FAC%\ota_data_initial.bin"
call :AppendJsonFile "%FAC%\storage.bin" last
>>"%REL%\manifest.json" echo   ]
>>"%REL%\manifest.json" echo }

echo [4/5] Copy Android device doc ...
set "DOC_SRC=%ROOT%\docs\Android端设备对接文档_v!VER!.md"
if exist "!DOC_SRC!" (
    copy /Y "!DOC_SRC!" "%REL%\Android端设备对接文档_v!VER!.md" >nul
    echo   docs -^> release\Android端设备对接文档_v!VER!.md
) else (
    echo   WARN: missing !DOC_SRC!  (create docs\Android端设备对接文档_v!VER!.md before handoff)
)

echo [5/5] Write VERSION.txt ...
call :HashFile "%OTA%\ESPRCCar.bin" OTAHASH
for %%A in ("%OTA%\ESPRCCar.bin") do set "OTASZ=%%~zA"

> "%REL%\VERSION.txt" (
echo ESPRCCar firmware release
echo =========================
echo version:        !VER!
echo project:        ESPRCCar
echo chip:           esp32s3
echo built_utc:      !UTC!
echo built_local:    !LOCAL!
echo.
echo OTA:
echo   path:   firmware/release/ota/ESPRCCar.bin
echo   bytes:  !OTASZ!
echo   sha256: !OTAHASH!
echo.
echo Factory: firmware/release/factory/
echo   tool:   Espressif Flash Download Tools
echo   addrs:  see factory/flash_args.txt
echo.
echo machine_readable: manifest.json
echo.
echo android_doc:  firmware/release/Android端设备对接文档_v!VER!.md
)

echo.
echo Done: firmware/release/  version=!VER!
echo   VERSION.txt + manifest.json updated
echo.
echo Note: run after idf.py build; build does not auto-copy here.
echo.
pause
exit /b 0
