@echo off
setlocal

:: 启用延迟扩展
setlocal EnableDelayedExpansion

@REM 可执行文件(exe)文件名
set EXE_NAME=V9_AG_PMU_APP_
@REM 可执行文件(exe)文件路径
set EXE_PATH=.\build
@REM 定制Hex输出路径
set OUTPUT_PATH=.\build
@REM 定制Hex文件名
set output_file_name=%EXE_NAME%
@REM fromelf文件路径
::Set FROMELF_PATH=D:\Keil_v5\ARM\ARMCLANG\bin
@REM 转换并定制Bin文件名 需要将fromelf.exe的路径添加到环境变量中
fromelf --bin %EXE_PATH%\%EXE_NAME%.axf --output %OUTPUT_PATH%\%output_file_name%.bin

cls
echo %date%
echo %time%

del ..\binFile\*.bin /s
del ..\binFile\*.hex /s

set FileName=%Date:~0,4%%Date:~5,2%%Date:~8,2%
echo %FileName%

set "search_dir=..\build"
set "search_ext=V9_AG_PMU_APP_EFTZ*.bin"
set "found_file="

for /R "%search_dir%" %%F in (%search_ext%) do (
    set "found_file=%%~nF"
)

@REM 软件版本文件路径
set "VERSION_FILE_PATH=..\user_inc\config.h"
@REM 软件版本字符串的格式
set "SOFTWARE_VERSION=Z70_DRONE"
@REM 获取软件版本，通过标识符找到所在行，然后根据第一个字符判断并赋值
for /f "tokens=1* delims= " %%a in ('type "%VERSION_FILE_PATH%" ^| findstr /C:"%SOFTWARE_VERSION%"') do (
	echo %%a
    set "firstChar=%%a"
    :: 去除第一个字符前后的空格
    set "firstChar=!firstChar:~0,1!"
    :: 判断第一个字符
    if "!firstChar!"=="/" (
        set "SW_Ver=Z20"
    ) else if "!firstChar!"=="#" (
        set "SW_Ver=Z70"
    ) else (
        set "SW_Ver=UNKNOWN"
    )
	goto :breakLoop
)

:breakLoop



copy ..\build\%found_file%.bin ..\binFile\%found_file%%FileName%.bin
copy ..\build\%found_file%.hex ..\binFile\%found_file%%FileName%.hex


@REM 显示并复制Hex文件AREM
echo Output hex file: %found_file%%FileName%%SW_Ver%%SW_Ver2%.bin

:: 关闭延迟扩展
endlocal

::pause
exit


