@echo off
setlocal

if "%~0"=="CONVERT.cmd" goto START
cls

:START
if not defined PROCESSOR_ARCHITECTURE goto PROC_ARC_NOT_DEF
set ProcessorArchitecture=%PROCESSOR_ARCHITECTURE%
if /i "%ProcessorArchitecture%"=="AMD64" set ProcessorArchitecture=x64

set ApplicationProcArch=%ProcessorArchitecture%
rem set ApplicationProcArch=x86
rem WOW64-mode may cause "Path"-property corruption: "Program Files"+" (x86)"

set ExeDir=..\Assemblies\LnkConverter\%ApplicationProcArch%\
set ExeFileName=LnkConverter.exe
set ExeFilePath=%ExeDir%%ExeFileName%

if not exist "%ExeFilePath%" goto TARGET_MODULE_NOT_FOUND

call :RESET_ERROR
%ExeFilePath% %*
if %errorlevel% neq 0 goto ERROR

goto FINISH

::==================================================================================================

:PROC_ARC_NOT_DEF
echo.
echo ## Environment variable is not defined: "PROCESSOR_ARCHITECTURE".
goto ERROR

::==================================================================================================

:TARGET_MODULE_NOT_FOUND
echo.
echo ## Target module not found: "%ExeFilePath%".
goto ERROR

::==================================================================================================

:RESET_ERROR
set errorlevel=0
set errorlevel=
goto :EOF

::==================================================================================================

:ERROR
if "%~0"=="CONVERT.cmd" goto ERROR_EXIT
echo 
pause

:ERROR_EXIT
endlocal
verify # 2>nul
goto :EOF

:FINISH
endlocal
call :RESET_ERROR
if "%~0"=="CONVERT.cmd" goto :EOF
echo.
pause
goto :EOF

::==================================================================================================
