@echo off

if exist "C:\MAN cats II\Runtime" set "MCDIR=C:\MAN cats II"
if exist "D:\MAN cats II\Runtime" set "MCDIR=D:\MAN cats II"
if exist "E:\MAN cats II\Runtime" set "MCDIR=E:\MAN cats II"

ver | findstr "5.1" > nul && set "v=x"
ver | findstr "5.2" > nul && set "v=x"
ver | findstr "6.1" > nul && set "v=7"
ver | findstr "10.0" > nul && set "v=10"

if not "%v%"=="10" goto not10
reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System /v ConsentPromptBehaviorAdmin | findstr "0x0" >nul
if not %errorlevel%==0 echo "!!! Windows 10: set UAC lowest level" & pause >nul & goto quit
echo test > "%ProgramFiles%\Teradyne\_wtest.tmp" 2>nul
del /Q "%ProgramFiles%\Teradyne\_wtest.tmp" 2>nul
if not %errorlevel%==0 echo "!!! Windows 10: set Teradyne folder full control for Everyone" & pause >nul & goto quit
net session >nul 2>&1
if not %errorlevel%==0 echo "!!! Windows 10: set Shortcut run as Administrator" & pause >nul & goto quit

:not10
set "LOG=%MCDIR%\selector.log"

echo --------------------------------------------- >>"%LOG%"
echo %date% %time% >>"%LOG%"
echo --------------------------------------------- >>"%LOG%"
echo.
echo                         *** INTERFACE SELECTOR ***
echo.
tasklist | findstr /i "sm2534" >>"%LOG%" 2>&1
IF %ERRORLEVEL% EQU 0 goto onlyrestart
set live=0
tasklist | findstr /i "TABMAN.exe" >>"%LOG%" 2>&1
IF %ERRORLEVEL% EQU 0 set live=1
del /Q "%MCDIR%\Runtime\needrest" >>"%LOG%" 2>&1
if %live% equ 1 call "%MCDIR%\Runtime\MCIIKill.cmd"
cls
echo.
echo                      MAN T200   SCANMATIK2/PRO   DPA5/MCOM
echo                         [1]           [2]           [3]
echo.
echo                      WAHLEN eine Schnittstelle aus
echo                      SELECT interface
echo                      WYBIERZ interfejs
echo.
if "%v%"=="x"  choice /C:123l ">>> Drucken Press Nacisnij "
if "%v%"=="7"  choice /C 123l /M ">>> Drucken Press Nacisnij "
if "%v%"=="10" choice /C 123l /M ">>> Drucken Press Nacisnij "
IF %ERRORLEVEL% EQU 1 goto t200
IF %ERRORLEVEL% EQU 2 goto sm2
IF %ERRORLEVEL% EQU 3 goto dpa5
IF %ERRORLEVEL% EQU 4 start notepad.exe "%LOG%" & start notepad.exe "%MCDIR%\mc2launcher.log"

goto end



:dpa5
echo.
echo MANCATS with DPA5 interface
echo.
if not exist "%ProgramFiles%\Scanmatik\smj2534.dll" goto nosm2
if "%v%"=="x" copy /Y "%ProgramFiles%\Scanmatik\smj2534.dll" "%windir%\System32\sm2534.dll"  >>"%LOG%" 2>&1
if "%v%"=="7" nircmd elevatecmd execmd copy /y "%ProgramFiles%\Scanmatik\smj2534.dll" "%windir%\System32\sm2534.dll"  >>"%LOG%" 2>&1
if "%v%"=="10" nircmd elevatecmd execmd copy /y "%ProgramFiles%\Scanmatik\smj2534.dll" "%windir%\System32\sm2534.dll"  >>"%LOG%" 2>&1

reg ADD HKCU\Software\GenRad\Grade\Testman\Configuration /v SyscallTimeout /t REG_DWORD /d 0x000dbba0 /f >>"%LOG%" 2>&1
echo n|comp "%ProgramFiles%\Teradyne\T-MVCI J2534\TMVCI32.dll_dpa5" "%ProgramFiles%\Teradyne\T-MVCI J2534\TMVCI32.dll" >>"%LOG%" 2>&1
IF %ERRORLEVEL% EQU 0 echo n|comp "%MCDIR%\Runtime\T200Syscall.dll_dpa5" "%MCDIR%\Runtime\T200Syscall.dll" >>"%LOG%" 2>&1
IF %ERRORLEVEL% EQU 0 echo n|comp "%ProgramFiles%\Teradyne\T-MVCI Device Manager\TMVCISysCon.dll_dpa5" "%ProgramFiles%\Teradyne\T-MVCI Device Manager\TMVCISysCon.dll" >>"%LOG%" 2>&1
IF %ERRORLEVEL% EQU 0 goto end
tasklist /NH 2>&1 | findstr "TMVCIDevMgrD.exe" >>"%LOG%"
IF %ERRORLEVEL% EQU 0 goto restart
copy /Y "%ProgramFiles%\Teradyne\T-MVCI J2534\TMVCI32.dll_dpa5" "%ProgramFiles%\Teradyne\T-MVCI J2534\TMVCI32.dll" >>"%LOG%" 2>&1
copy /Y "%MCDIR%\Runtime\T200Syscall.dll_dpa5" "%MCDIR%\Runtime\T200Syscall.dll" >>"%LOG%" 2>&1
copy /Y "%ProgramFiles%\Teradyne\T-MVCI Device Manager\TMVCISysCon.dll_dpa5" "%ProgramFiles%\Teradyne\T-MVCI Device Manager\TMVCISysCon.dll" >>"%LOG%" 2>&1
ping -n 2 127.0.0.1 >nul
goto end



:sm2
echo.
echo MANCATS with SCANMATIK2 interface
echo.
if not exist "%ProgramFiles%\Scanmatik\smj2534.dll" goto nosm2
if "%v%"=="x" copy /Y "%ProgramFiles%\Scanmatik\smj2534.dll" "%windir%\System32\sm2534.dll"  >>"%LOG%" 2>&1
if "%v%"=="7" nircmd elevatecmd execmd copy /y "%ProgramFiles%\Scanmatik\smj2534.dll" "%windir%\System32\sm2534.dll"  >>"%LOG%" 2>&1
if "%v%"=="10" nircmd elevatecmd execmd copy /y "%ProgramFiles%\Scanmatik\smj2534.dll" "%windir%\System32\sm2534.dll"  >>"%LOG%" 2>&1

reg ADD HKCU\Software\GenRad\Grade\Testman\Configuration /v SyscallTimeout /t REG_DWORD /d 0x000dbba0 /f  >>"%LOG%" 2>&1
echo n|comp "%ProgramFiles%\Teradyne\T-MVCI J2534\TMVCI32.dll_sm" "%ProgramFiles%\Teradyne\T-MVCI J2534\TMVCI32.dll"  >>"%LOG%" 2>&1
IF %ERRORLEVEL% EQU 0 echo n|comp "%MCDIR%\Runtime\T200Syscall.dll_sm" "%MCDIR%\Runtime\T200Syscall.dll" >>"%LOG%" 2>&1
IF %ERRORLEVEL% EQU 0 echo n|comp "%ProgramFiles%\Teradyne\T-MVCI Device Manager\TMVCISysCon.dll_sm" "%ProgramFiles%\Teradyne\T-MVCI Device Manager\TMVCISysCon.dll" >>"%LOG%" 2>&1
IF %ERRORLEVEL% EQU 0 goto end
tasklist /NH 2>&1 | findstr "TMVCIDevMgrD.exe" >>"%LOG%"
IF %ERRORLEVEL% EQU 0 goto restart
copy /Y "%ProgramFiles%\Teradyne\T-MVCI J2534\TMVCI32.dll_sm" "%ProgramFiles%\Teradyne\T-MVCI J2534\TMVCI32.dll" >>"%LOG%" 2>&1
copy /Y "%MCDIR%\Runtime\T200Syscall.dll_sm" "%MCDIR%\Runtime\T200Syscall.dll" >>"%LOG%" 2>&1
copy /Y "%ProgramFiles%\Teradyne\T-MVCI Device Manager\TMVCISysCon.dll_sm" "%ProgramFiles%\Teradyne\T-MVCI Device Manager\TMVCISysCon.dll" >>"%LOG%" 2>&1
ping -n 2 127.0.0.1 >nul
goto end



:t200
echo.
echo MANCATS with T200 interface
echo.
reg ADD HKCU\Software\GenRad\Grade\Testman\Configuration /v SyscallTimeout /t REG_DWORD /d 0x00002710 /f  >>"%LOG%" 2>&1
takeown /f %windir%\System32\sm2534.dll >>"%LOG%" 2>&1
icacls %windir%\System32\sm2534.dll /grant administrators:F >>"%LOG%" 2>&1
del /Q %windir%\System32\sm2534.dll >>"%LOG%" 2>&1
if "%v%"=="10" nircmd elevatecmd execmd del /Q %windir%\System32\sm2534.dll >>"%LOG%" 2>&1

echo n|comp "%ProgramFiles%\Teradyne\T-MVCI J2534\TMVCI32.dll_t200" "%ProgramFiles%\Teradyne\T-MVCI J2534\TMVCI32.dll" >>"%LOG%" 2>&1
IF %ERRORLEVEL% EQU 0 echo n|comp "%MCDIR%\Runtime\T200Syscall.dll_t200" "%MCDIR%\Runtime\T200Syscall.dll" >>"%LOG%" 2>&1
IF %ERRORLEVEL% EQU 0 echo n|comp "%ProgramFiles%\Teradyne\T-MVCI Device Manager\TMVCISysCon.dll_t200" "%ProgramFiles%\Teradyne\T-MVCI Device Manager\TMVCISysCon.dll" >>"%LOG%" 2>&1
IF %ERRORLEVEL% EQU 0 goto end
tasklist /NH 2>&1 | findstr "TMVCIDevMgrD.exe" >>"%LOG%"
IF %ERRORLEVEL% EQU 0 goto restart
copy /Y "%ProgramFiles%\Teradyne\T-MVCI J2534\TMVCI32.dll_t200" "%ProgramFiles%\Teradyne\T-MVCI J2534\TMVCI32.dll" >>"%LOG%" 2>&1
copy /Y "%MCDIR%\Runtime\T200Syscall.dll_t200" "%MCDIR%\Runtime\T200Syscall.dll" >>"%LOG%" 2>&1
copy /Y "%ProgramFiles%\Teradyne\T-MVCI Device Manager\TMVCISysCon.dll_t200" "%ProgramFiles%\Teradyne\T-MVCI Device Manager\TMVCISysCon.dll" >>"%LOG%" 2>&1

ping -n 2 127.0.0.1 >nul
goto quit

:restart
echo "1" > "%MCDIR%\Runtime\needrest"
nircmd infobox "W CELU ZMIANY INTERFEJSU KOMPUTER ZOSTANIE PONOWNIE URUCHOMIONY~n~nTO SWITCH INTERFACE COMPUTER WILL BE RESTARTED NOW~n~nUM DIE SCHNITTSTELLE ZU WECHSELN WIRD DER COMPUTER JETZT NEU GESTARTET" "Interface switch"
nircmd exitwin reboot
goto quit

:onlyrestart
nircmd infobox "KOMPUTER WYMAGA PONOWNEGO URUCHOMIENIA~n~nCOMPUTER NEEDS TO BE RESTARTED NOW~n~nCOMPUTER MUSS NEU GESTARTET WERDEN" "Restart needed"
nircmd exitwin reboot
goto quit

:close
echo.
echo Nale¾y zamkn„ MAN-cats i inne programy lub zrestartowa komputer
echo Bitte schlieįen MAN-cats und alles programmen oder starten Ihren Computer neu
echo Please close MAN-cats and all programs or reboot your computer
echo.
ping -n 5 127.0.0.1 >nul
goto quit

:nosm2
echo ...Missing Scanmatik dll ! Please install
pause > nul
goto quit

:end
if %live% equ 0 goto quit
mode con: cols=60 lines=3
cls
call "%MCDIR%\Runtime\MCIIlauncher.bat"

:quit
ping -n 1 127.0.0.1 >nul
