@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 TargetFramework=netcoreapp3.1
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
if /i "%ApplicationProcArch%"=="x86" goto DOTNET_X86
%ExeFilePath% %*
if %errorlevel% neq 0 goto ERROR
goto FINISH

:DOTNET_X86
if not defined ProgramFiles(x86) goto PROGRAMFILES_X86_NOT_DEF
"%ProgramFiles(x86)%\dotnet\dotnet" %ExeFilePath% %*
if %errorlevel% neq 0 goto ERROR
goto FINISH

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

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

:PROGRAMFILES_X86_NOT_DEF
echo.
echo ## Environment variable not defined: "ProgramFiles(x86)".
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 not "%~0"=="CONVERT.cmd" (
echo 
pause )
endlocal
verify # 2>nul
goto :EOF
:FINISH
endlocal
if not "%~0"=="CONVERT.cmd" (
echo.
pause )
call :RESET_ERROR
goto :EOF
::==================================================================================================
