@echo Off
setlocal enabledelayedexpansion

echo. 
echo =========================
echo    Pico Auto-Programmer
echo =========================

REM Read version and build number for file versioning
set /p version=<build_version.txt
set /p new=< build_number.txt

if %new% lss 10 						set version_build_string=%version%.000%new%
if %new% geq 10 	if %new% lss 100 	set version_build_string=%version%.00%new%
if %new% geq 100	if %new% lss 1000 	set version_build_string=%version%.0%new%
if %new% geq 1000 						set version_build_string=%version%.%new%

REM Create versions directory if it doesn't exist
if not exist versions\ mkdir versions

REM Copy UF2 files with version numbers to versions folder
for %%f in (*.uf2) do (
	set filename_base=%%~nf
	set filename_with_version=!filename_base!_!version_build_string!.uf2
	copy %%f versions\!filename_with_version! >nul 2>&1
)

echo.
echo Running unified Pico programmer...
echo.

REM Call the unified Python script
call python ./Pico_Reset_And_Program.py

if %errorlevel% == 0 (
	echo.
	echo =========================
	echo    Programming Success!
	echo =========================
) else (
	echo.
	echo =========================
	echo      Programming Error
	echo =========================
	exit /b 1
)

echo. 
exit /b