@echo off

title Start MAN-cats II

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
if defined ProgramFiles(x86) echo "!!! Not tested on Windows 10 64bit"
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%\mc2launcher.log"
echo --------------------------------------------- >>"%LOG%"
echo %date% %time% >>"%LOG%"
echo --------------------------------------------- >>"%LOG%"

cd %~d0%~p0

tasklist /NH 2>&1 | findstr "TABMAN.exe" >>"%LOG%"
IF %ERRORLEVEL% EQU 0 goto close

taskkill /T /F /IM smwin.exe >>"%LOG%" 2>&1

set ups=0
dir "%MCDIR%\Runtime\needrest" >>"%LOG%" 2>&1
IF %ERRORLEVEL% EQU 0 goto doups

:ups
dir %windir%\system32\sm2534.dll >>"%LOG%" 2>&1
IF %ERRORLEVEL% EQU 0 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 dpa5
dir %windir%\system32\sm2534.dll >>"%LOG%" 2>&1
IF %ERRORLEVEL% EQU 0 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 sm2
dir %windir%\system32\sm2534.dll >>"%LOG%" 2>&1
IF NOT %ERRORLEVEL% EQU 0 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 t200
echo "DLLs compared" >>"%LOG%"
if %ups% equ 1 goto close
:doups
set ups=1
mode con: cols=80 lines=15
call "%MCDIR%\Runtime\MCIISelector.bat"
mode con: cols=60 lines=3
goto ups




:dpa5
echo.
echo.
echo STARTING MAN-Cats II with DPA5 interface

reg ADD HKCU\Software\GenRad\Grade\Testman\Configuration /v SyscallTimeout /t REG_DWORD /d 0x000dbba0 /f >>"%LOG%" 2>&1
ping -n 1 127.0.0.1 > nul
start MANkey.exe
rem start nircmd cmdwait 4000 win move ititle "MAN Immo" 65 10
start nircmd cmdwait 4000 win move ititle "Key state" 450 -320
start mckiller.exe
TABMAN.exe
nircmd.exe waitprocess TABMAN.exe cmdwait 1500
taskkill /T /F /IM TABMAN.exe >>"%LOG%" 2>&1
taskkill /T /F /IM mckiller.exe >>"%LOG%" 2>&1
taskkill /T /F /IM testman.exe >>"%LOG%" 2>&1
taskkill /T /F /IM WEBMAN.exe >>"%LOG%" 2>&1
taskkill /T /F /IM FlukeView.exe >>"%LOG%" 2>&1
taskkill /T /F /IM MANkey.exe >>"%LOG%" 2>&1
taskkill /T /F /IM ManImmo.exe >>"%LOG%" 2>&1
taskkill /T /F /IM Communicationswitcher.exe >>"%LOG%" 2>&1
taskkill /T /F /IM MANCats_Systray.exe >>"%LOG%" 2>&1
goto end

:sm2
echo.
echo.
echo STARTING MAN-Cats II with SCANMATIK2 interface

reg ADD HKCU\Software\GenRad\Grade\Testman\Configuration /v SyscallTimeout /t REG_DWORD /d 0x000dbba0 /f  >>"%LOG%" 2>&1
ping -n 1 127.0.0.1 > nul
start MANkey.exe
rem start nircmd cmdwait 4000 win move ititle "MAN Immo" 65 10
start nircmd cmdwait 4000 win move ititle "Key state" 450 -320
start mckiller.exe
TABMAN.exe
nircmd.exe waitprocess TABMAN.exe cmdwait 1500
taskkill /T /F /IM TABMAN.exe >>"%LOG%" 2>&1
taskkill /T /F /IM mckiller.exe >>"%LOG%" 2>&1
taskkill /T /F /IM testman.exe >>"%LOG%" 2>&1
taskkill /T /F /IM WEBMAN.exe >>"%LOG%" 2>&1
taskkill /T /F /IM FlukeView.exe >>"%LOG%" 2>&1
taskkill /T /F /IM MANkey.exe >>"%LOG%" 2>&1
taskkill /T /F /IM ManImmo.exe >>"%LOG%" 2>&1
taskkill /T /F /IM Communicationswitcher.exe >>"%LOG%" 2>&1
taskkill /T /F /IM MANCats_Systray.exe >>"%LOG%" 2>&1
goto end

:t200
echo.
echo.
echo STARTING MAN-Cats II with T200 interface

reg ADD HKCU\Software\GenRad\Grade\Testman\Configuration /v SyscallTimeout /t REG_DWORD /d 0x00002710 /f  >>"%LOG%" 2>&1
ping -n 1 127.0.0.1 > nul
start mckiller.exe
TABMAN.exe
nircmd.exe waitprocess TABMAN.exe cmdwait 1500
taskkill /T /F /IM TABMAN.exe >>"%LOG%" 2>&1
taskkill /T /F /IM testman.exe >>"%LOG%" 2>&1
taskkill /T /F /IM mckiller.exe >>"%LOG%" 2>&1
taskkill /T /F /IM WEBMAN.exe >>"%LOG%" 2>&1
taskkill /T /F /IM FlukeView.exe >>"%LOG%" 2>&1
taskkill /T /F /IM MANkey.exe >>"%LOG%" 2>&1
taskkill /T /F /IM ManImmo.exe >>"%LOG%" 2>&1
taskkill /T /F /IM Communicationswitcher.exe >>"%LOG%" 2>&1
taskkill /T /F /IM MANCats_Systray.exe >>"%LOG%" 2>&1
goto end


:close
echo.
echo Bitte schlieįen das MAN-Cats-programm oder Starten das system neu
echo Please close MAN-Cats program first or restart system
echo|set /p="Nale¾y najpierw zamkn„ program MAN-Cats lub zrestartowa komputer"
ping -n 5 127.0.0.1 > nul
taskkill /T /F /IM TABMAN.exe >>"%LOG%" 2>&1
taskkill /T /F /IM mckiller.exe >>"%LOG%" 2>&1
taskkill /T /F /IM testman.exe >>"%LOG%" 2>&1
taskkill /T /F /IM WEBMAN.exe >>"%LOG%" 2>&1
taskkill /T /F /IM FlukeView.exe >>"%LOG%" 2>&1
taskkill /T /F /IM MANkey.exe >>"%LOG%" 2>&1
taskkill /T /F /IM ManImmo.exe >>"%LOG%" 2>&1
taskkill /T /F /IM Communicationswitcher.exe >>"%LOG%" 2>&1
taskkill /T /F /IM MANCats_Systray.exe >>"%LOG%" 2>&1

:end
