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

set start_service_output=log\start_service.log
if exist %start_service_output% ( del %start_service_output% /f )

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

:AsAdmin
set root=%cd%
tasklist|find /i "converter.exe" >> %start_service_output%
if %errorlevel% equ 0 (
	goto CleanConverter
) else (
	goto CheckServerPort
)

:CleanConverter
echo [32m[start_service INFO] Converter is running. Need to clean up the converter and related processes.[0m
echo [start_service INFO] Converter is running. Need to clean up the converter and related processes. >> %start_service_output%
echo [32m[start_service INFO] Clean up the converter process.[0m
echo [start_service INFO] Clean up the converter process. >> %start_service_output%
taskkill /im converter.exe /f
if %errorlevel% gtr 0 (
	goto Warning
)

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

:CheckServerPort
echo [32m[start_service INFO] Checking service port...[0m
echo [start_service INFO] Checking service port. >> %start_service_output%
set export_content_port=
for /f "delims=!" %%i in ('findstr /R /C:"^Port: " .\etc\converter-api.yaml') do (
	set export_content_port=%%i
)
if "%export_content_port%"=="" (
	goto ServerPortNotFound
)

set server_port=%export_content_port:~6,5%

netstat -ano | findstr /R /C:":%server_port% " | findstr LISTENING > nul
if %errorlevel% equ 0 (
	goto ServerPortAlreadyUsed
)
echo [32m[start_service INFO] Checking service port completed.[0m
echo [start_service INFO] Checking service port completed. >> %start_service_output%

:StartConverter
echo [32m[start_service INFO] Starting converter...[0m
echo [start_service INFO] Starting converter... >> %start_service_output%
.\bin\converter.exe -start -daemon -forever

if %errorlevel% gtr 0 (
	goto StartConverterFail
)

set /a temp_time_count=0
:CheckConverterIsStarted
if %temp_time_count%==300 (
	goto StartConverterTimeoutError
)
netstat -ano | findstr /R /C:"0.0.0.0:%server_port% " | findstr LISTENING >> %start_service_output%
if %errorlevel% equ 0 (
	echo [32m100%%[0m
	goto StartConverterTips
)
set /p=[32m=[0m<nul&timeout /t 1 /NOBREAK>nul
echo [start_service INFO] The service is starting. please wait. >> %start_service_output%
set /a temp_time_count+=1
goto CheckConverterIsStarted

:StartConverterTips
echo [32m[start_service INFO] The service is started at 0.0.0.0:%server_port%[0m
echo [start_service INFO] The service is started at 0.0.0.0:%server_port%] >> %start_service_output%
start http://localhost:%server_port%
echo [32m[start_service INFO] Completed.[0m
echo [start_service INFO] completed. >> %start_service_output%
goto End

:ServerPortNotFound
echo [31m[start_service ERROR] Failed to find service port. Please check .\etc\converter-api.yaml.[0m
echo [start_service ERROR] Failed to find service port. Please check .\etc\converter-api.yaml. >> %start_service_output%
goto End

:ServerPortAlreadyUsed
echo [31m[start_service ERROR] The service port:%server_port% conflict. Please check .\etc\converter-api.yaml and switch to another port.[0m
echo [start_service ERROR] The service port:%server_port% conflict. Please check .\etc\converter-api.yaml and switch to another port. >> %start_service_output%
goto End

:StartConverterFail
echo [31m[start_service ERROR] Failed to start converter. Possibly because the specified program execution path could not be found.[0m
echo [start_service ERROR] Failed to start converter. Possibly because the specified program execution path could not be found. >> %start_service_output%
goto End

:StartConverterTimeoutError
echo [31m[start_service ERROR] Start converter timeout.[0m
echo [start_service ERROR] Start converter timeout. >> %start_service_output%
echo [32m[start_service ERROR] Failed to start converter. Need to clean up the converter process.[0m
echo [start_service ERROR] Failed to start converter. Need to clean up the converter process. >> %start_service_output%
taskkill /im converter.exe /f
if %errorlevel% gtr 0 (
	goto Warning
)

echo [32m[start_service ERROR] Failed to start converter. Need to clean up plugin processes.[0m
echo [start_service ERROR] Failed to start converter. Need to clean up plugin processes. >> %start_service_output%
for /r .\plugin %%a in (*.exe) do (
	tasklist | find /i "%%~na.exe" >> %start_service_output%
	if %errorlevel% equ 0 (
		taskkill /im "%%~na.exe" /f 
		if %errorlevel% gtr 0 (
		 goto Warning
		)
	)
)
goto End

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

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

:End

pause

