@echo off
mode con cp select=936
:: 进入脚本当前目录，用于单击右键以管理员权限运行时使用
cd /d %~dp0
echo 1. JLink (＞ v7.60)
echo 2. JLink (≤ v7.60)
echo 3. IAR
set /p tool=请选择（1/2/3）:

:: 根据tool的值判断要patch的工具
if "%tool%"=="1" (
    call :patch_jlink_gt_7.60
) else if "%tool%"=="2" (
    call :patch_jlink_lt_7.60
) else if "%tool%"=="3" (
    call :patch_iar
) else (
    echo 请输入1/2/3！！！
)
pause
exit /b 0


: patch_jlink_gt_7.60
echo 1. Patch
echo 2. Remove
set /p option=请选择（1/2）:
:: 获取appdata路径
set appdata_path=%APPDATA%
set patch_path=%appdata_path%\SEGGER
if "%option%"=="1" (
    call :patch_jlink_gt_7.60_patch
) else if "%option%"=="2" (
    call :patch_jlink_gt_7.60_remove
) else (
    echo 请输入1/2！！！
)
goto :eof


: patch_jlink_gt_7.60_patch
xcopy /s /y /q jlink\Devices %patch_path%\JLinkDevices
:: 获取当前目录
set current_path=%cd%
set jlink_path=
echo 1. C:\Program Files\SEGGER\JLink
echo 2. 自定义
set /p number=请输入JLink安装目录(1/2):
if "%number%"=="1" (
    set jlink_path=C:\Program Files\SEGGER\JLink
) else (
    set /p jlink_path=请输入JLink安装目录:
)
chdir %jlink_path%
JLinkDLLUpdater.exe
chdir %current_path%
echo patch成功！！！
goto :eof


: patch_jlink_gt_7.60_remove
rd /s /q %patch_path%\JLinkDevices\YTMicro
echo remove成功！！！
goto :eof


: patch_jlink_lt_7.60
echo 1. Patch
echo 2. Remove
set /p option=请选择（1/2）:
if "%option%"=="1" (
    call :patch_jlink_lt_7.60_patch
) else if "%option%"=="2" (
    call :patch_jlink_lt_7.60_remove
) else (
    echo 请输入1/2！！！
)
goto :eof


: patch_jlink_lt_7.60_patch
set current_path=%cd%
echo 1. C:\Program Files (x86)\SEGGER\JLink
echo 2. C:\Program Files\SEGGER\JLink
echo 3. 自定义
set /p number=请输入JLink安装目录(1/2/3):

set jlink_path=C:\Program Files (x86)\SEGGER\JLink

if "%number%"=="1" (
    set jlink_path="C:\Program Files (x86)\SEGGER\JLink"
) else if "%number%"=="2" (
    set jlink_path=C:\Program Files\SEGGER\JLink
) else (
    set /p jlink_path=请输入JLink安装目录:
)

echo %jlink_path%
xcopy /s /y /q jlink\JLinkDevices.xml %jlink_path%
xcopy /s /y /q jlink\Devices %jlink_path%\Devices
chdir %jlink_path%
JLinkDLLUpdater.exe
chdir %current_path%
echo patch成功！！！
goto :eof


: patch_jlink_lt_7.60_remove
echo 1. C:\Program Files (x86)\SEGGER\JLink
echo 2. C:\Program Files\SEGGER\JLink
echo 3. 自定义
set /p number=请输入JLink安装目录(1/2/3):

set jlink_path=C:\Program Files (x86)\SEGGER\JLink

if "%number%"=="1" (
    set jlink_path="C:\Program Files (x86)\SEGGER\JLink"
) else if "%number%"=="2" (
    set jlink_path=C:\Program Files\SEGGER\JLink
) else (
    set /p jlink_path=请输入JLink安装目录:
)
:: remove patch
rd /s /q %jlink_path%\Devices\YTMicro
echo remove成功！！！
goto :eof


: patch_iar
echo 1. Patch
echo 2. Remove
set /p option=请选择（1/2）:
if "%option%"=="1" (
    call :patch_iar_patch
) else if "%option%"=="2" (
    call :patch_iar_remove
) else (
    echo 请输入1/2！！！
)
goto :eof


: patch_iar_patch
:: obtain current path
set current_path=%cd%

echo 1. C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.3
echo 2. C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.4
echo 3. C:\Program Files\IAR Systems\Embedded Workbench 9.0
echo 4. C:\Program Files\IAR Systems\Embedded Workbench 9.2
echo 5. 自定义
set /p number=请输入IAR安装目录(1/2/3/4/5):

set iar_path=C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.3

if "%number%"=="1" (
    set iar_path="C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.3"
) else if "%number%"=="2" (
    set iar_path="C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.4"
) else if "%number%"=="3" (
    set iar_path="C:\Program Files\IAR Systems\Embedded Workbench 9.0"
) else if "%number%"=="4" (
    set iar_path="C:\Program Files\IAR Systems\Embedded Workbench 9.2"
) else (
    set /p iar_path=请输入IAR安装目录:
)

:: 将iar\arm\config\debugger目录下的文件拷贝到iar_path\arm\config\debugger目录下
xcopy /s /y /q iar\arm\config\debugger %iar_path%\arm\config\debugger
xcopy /s /y /q iar\arm\config\flashloader %iar_path%\arm\config\flashloader
xcopy /s /y /q iar\arm\config\devices %iar_path%\arm\config\devices
xcopy /s /y /q iar\arm\config\linker %iar_path%\arm\config\linker
xcopy /s /y /q iar\arm\src\flashloader %iar_path%\arm\src\flashloader
xcopy /s /y /q iar\arm\bin\pemicro\P^&E\supportFiles_ARM %iar_path%\arm\bin\pemicro\P^&E\supportFiles_ARM
echo patch成功！！！
goto :eof


: patch_iar_remove
:: obtain current path
set current_path=%cd%

echo 1. C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.3
echo 2. C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.4
echo 3. C:\Program Files\IAR Systems\Embedded Workbench 9.0
echo 4. C:\Program Files\IAR Systems\Embedded Workbench 9.2
echo 5. 自定义
set /p number=请输入IAR安装目录(1/2/3/4/5):

set iar_path=C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.3

if "%number%"=="1" (
    set iar_path="C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.3"
) else if "%number%"=="2" (
    set iar_path="C:\Program Files (x86)\IAR Systems\Embedded Workbench 8.4"
) else if "%number%"=="3" (
    set iar_path="C:\Program Files\IAR Systems\Embedded Workbench 9.0"
) else if "%number%"=="4" (
    set iar_path="C:\Program Files\IAR Systems\Embedded Workbench 9.2"
) else (
    set /p iar_path=请输入IAR安装目录:
)

:: remove patch
rd /s /q %iar_path%\arm\config\debugger\YTMicro
rd /s /q %iar_path%\arm\config\flashloader\YTMicro
rd /s /q %iar_path%\arm\config\devices\YTMicro
rd /s /q %iar_path%\arm\config\linker\YTMicro
rd /s /q %iar_path%\arm\src\flashloader\YTMicro
rd /s /q %iar_path%\arm\bin\pemicro\P^&E\supportFiles_ARM\YTMicro
echo remove成功！！！
goto :eof
