@echo off

rem <> Manually set path to JLink install directory here if you do not
rem    want to use the auto detect method. Make sure a backslash
rem    is at the end of the path
set BASE=C:\Program Files (x86)\SEGGER\JLink\
if exist "%BASE%\JLink.exe" goto PATH_SET
rem check for 64bit version
set BASE=C:\Program Files\SEGGER\JLink\
if exist "%BASE%\JLink.exe" goto PATH_SET

:NO_PATH
echo ===================================================================
echo ERROR: You need to set the path for JLink.exe 
echo ===================================================================
pause
exit
:PATH_SET

rem <> extract the version number from the path
set /A MINJVER =72000

set KEYNAME=HKEY_LOCAL_MACHINE\SOFTWARE\SEGGER\J-Link
set VALNAME=CurrentVersion
for /f "tokens=2,*" %%a in ('reg query %KEYNAME% /v %VALNAME% ^| findstr %VALNAME%') do (
    set /A JVER=%%b
)

if "%JVER%" EQU "" goto JTAGCONF_CHECK

rem check for version >= 7.20.00
echo Your JLINK Version is '%JVER%'
echo Minimum JLINK Version is %MINJVER%
echo.
if /I %JVER% GEQ %MINJVER% (goto loop)
echo ===================================================================
echo ERROR: You need at least JLINK version %MINJVER%
echo ===================================================================
pause
exit

:JTAGCONF_CHECK

echo ===================================================================
echo WARNING: You need at least JLINK version %MINJVER%
echo ===================================================================
pause
:loop

echo. 
echo                               8888 8888 8888                              8888 
echo                                    8888                                        
echo      8888  8888 8888     8888 8888 8888 8888   888888888   88888  88888   8888 
echo    888888888888 8888     8888 8888 8888 8888 8888888888888 8888888888888  8888 
echo   88888   88888 8888     8888 8888 8888 8888          8888 88888    88888 8888 
echo   8888     8888 8888     8888 8888 8888 8888  888888888888 88888    88888 8888 
echo   888888  88888 8888     8888 8888 8888 8888 8888    88888 88888    88888 8888 
echo    888888888888 8888888888888 8888 8888 8888 8888888888888 88888    88888 8888 
echo            8888   888888 8888 8888 8888 8888  8888888 8888 88888    88888 8888 
echo   88888   8888                                                                 
echo     888888888               B Y  T E S  E L E C T R O N I C  S O L U T I O N S                                                                 
echo  ***************************************************************************** 
echo.
echo Please select one of the following options:
echo. 
echo  1 - Flash Bootloader
echo  2 - Flash Guiliani Application
echo  3 - Flash Guiliani Resources
echo. 
echo  x - Exit
echo.
set /p SELECTED=Your choice: 

if "%SELECTED%" == "x" goto :eof
if "%SELECTED%" == "1" goto :flashBootloader
if "%SELECTED%" == "2" goto :flashApplication
if "%SELECTED%" == "3" goto :flashResources
goto :errorInput 

:flashBootloader
cls
echo.
echo You are about to flash bootloader for QSPI, do you want to proceed? (y/n)
set /p INPUT=
if %INPUT% NEQ y goto loop
cd JLink
"%BASE%\JLink.exe" -if JTAG -speed 12000 -device R7S921053VCBG -jtagconf -1,-1 -CommanderScript FlashBootloader_RZA2M.Command
pause
cd ..
goto :loop

:flashApplication
echo.
echo Which binary to flash?
echo Release version for RZA2M built with eGML Library            = 2
echo Go to main menu                                              = x
set /p INPUT=
if %INPUT% == 2 goto Release_RZA2M_eGML
if %INPUT% == x goto loop
echo Incorrect input & goto flashApplication
:Release_RZA2M_eGML
echo.
pause
cd JLink
"%BASE%\JLink.exe" -if JTAG -speed 12000 -device R7S921053VCBG -jtagconf -1,-1 -CommanderScript FlashApplication_Release_RZA2M.Command
pause
cd ..
goto :loop

:flashResources
echo.
echo For which board are resources to be flashed?
echo RZA2M            = 2
echo Go to main menu  = x
set /p INPUT=
if %INPUT% == 2 goto RZA2M
if %INPUT% == x goto loop
echo Incorrect input & goto flashResources
:RZA2M
if not exist "..\Export\RZA2M\Resources.dat" goto :errorNoResourceFile_RZA2M
copy ..\Export\RZA2M\Resources.dat ..\Export\RZA2M\Resources.bin>nul 
echo.
pause
cd JLink
cls
"%BASE%\JLink.exe" -if JTAG -speed 12000 -device R7S921053VCBG -jtagconf -1,-1 -CommanderScript FlashResources_RZA2M.Command
pause
cd ..
goto :loop

:errorNoResourceFile_RZA2M
echo.
echo Resource file not exists! Please export resources from GSE into "Export/RZA2M" folder in case of RZA2M!
echo.
pause
goto :loop

:errorInput
echo.
echo Illegal input! Please try again!
echo.
pause
goto :loop

