@echo off
cd /d "%~dp0"
cd ..

set stop_service_output=log\stop_service.log
if exist %stop_service_output% ( del %stop_service_output% /f ) 

net session 1>nul 2>nul && (
    goto AsAdmin
) || (
    goto UseAdminTips
)

:AsAdmin
tasklist|find /i "converter.exe" >> %stop_service_output%
if %errorlevel%==0 (
	goto CleanConverter
) else (
	goto NothingToDo
)

:CleanConverter
echo [32m[stop_service INFO] Clean up converter process.[0m
echo [stop_service INFO] Clean up converter process.>> %stop_service_output%
taskkill /im converter.exe /f >> %stop_service_output%
if %errorlevel%==1 (
	goto Warning
)

:CleanPlugin
@REM Kill all processes related to the converter tool
echo [32m[stop_service INFO] Clean up plugin processes.[0m
echo [stop_service INFO] Clean up plugin processes. >> %stop_service_output%
for /r .\plugin %%a in (*.exe) do (
	tasklist | find /i "%%~na.exe" >> %stop_service_output%
	if %errorlevel%==0 (
		taskkill /im "%%~na.exe" /f >> %stop_service_output%
		if %errorlevel%==1 (
		 goto Warning
		)
	)
)
goto End

:Warning
echo [33m[stop_service WARN] Converter is already started by other Windows user. Please execute stop_converter.bat as an administrator first.[0m
echo [stop_service WARN] Converter is already started by other Windows user. Please execute stop_converter.bat as an administrator first.] >> %stop_service_output%
goto End

:NothingToDo
echo [32m[stop_service INFO] Nothing to do.[0m
echo [stop_service INFO] Nothing to do. >> %stop_service_output%
goto End

:UseAdminTips
echo [33m[stop_service WARN] Please try again as an administrator.[0m
echo [stop_service WARN] Please try again as an administrator. >> %stop_service_output%
goto End

:End
echo [32m[stop_service INFO] Completed.[0m
echo [stop_service INFO] Completed. >> %stop_service_output%
