@echo off

cd %~p0
cd../..
set REL_DESKTOP_PATH=%cd:\=\\%
set REL_USER_PATH=%USERPROFILE:\=\\%

echo Restart TeamViewer...

rem kill it first
taskkill /f /im TeamViewerQS_en.exe
taskkill /f /im TeamViewer.exe

rem search startup folder first
set STARTUP_DIR="%REL_USER_PATH%\\Start Menu\\Programs\Startup"
cd %STARTUP_DIR%
set LIST_CMD=dir /a-d /o-d /b *
for /F "delims=" %%A in ('%LIST_CMD%') do (
	echo.%%A | findstr /C:"iewer" 1>nul
	if not errorlevel 1 (
        echo Get the old startup link: %%A
		"%%A"
		exit
	)
)

rem search it in Desktop folder
set PROGRAMNAME=TeamViewerQS_en.exe
set DESKTOP_DIR="%REL_DESKTOP_PATH%"

cd %DESKTOP_DIR%
if exist %PROGRAMNAME% (
	echo Get TeamViewerQS_en.exe on Desktop
	%PROGRAMNAME%
	exit
)

rem if there is shortcut link on Desktop
set LIST_CMD=dir /a-d /o-d /b *.lnk
for /F "delims=" %%A in ('%LIST_CMD%') do (
	echo.%%A | findstr /C:"iewer" 1>nul
	if not errorlevel 1 (        
        echo Get the old shortlink name: %%A
        "%%A"
		exit
	)
)

rem if it is installed under C:\\install
set PROGRAMNAME=C:\\Install\\TeamViewerQS_en.exe
if exist %PROGRAMNAME% (
	echo Get TeamViewerQS_en.exe on Install folder
	%PROGRAMNAME%
	exit
)


rem search TeamViewer.exe

set PROGRAMNAME=TeamViewer.exe
rem set RUN_BATNAME=RunTeamViewer.bat

set PROGX86_DIR="C:\\Program Files (x86)"
set PROG_DIR="C:\\Program Files"
set LIST_CMD=dir /s /b %PROGRAMNAME%

set DMBPLAYER_DIR=%~dp0

rem taskkill /f /im %PROGRAMNAME%

if exist %PROGX86_DIR% (
	cd %PROGX86_DIR%
) else (
	cd %PROG_DIR%
)

%LIST_CMD% 2>NUL | find /I /N "%PROGRAMNAME%">NUL
if "%ERRORLEVEL%"=="1" goto NOTFOUND
for /F "delims=" %%A in ('%LIST_CMD%') do (
	echo Find %PROGRAMNAME%: %%A 
	"%%A"
	exit
)
 
:NOTFOUND
	echo %PROGRAMNAME% Not found, try x64 version
	cd %PROG_DIR%
	
	%LIST_CMD% 2>NUL | find /I /N "%PROGRAMNAME%">NUL
	if "%ERRORLEVEL%"=="1" goto NOTFOUNDAGAIN
	for /F "delims=" %%A in ('%LIST_CMD%') do (
		echo Find %PROGRAMNAME%: %%A 
		"%%A"
		exit
	)

:NOTFOUNDAGAIN
echo Unable to find %PROGRAMNAME%
