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

set app_path="%cd%"
set nssm_exe=bin\nssm.exe
set service_name=Converter
set app_name=bin\start_service.bat

set install_service_output=log\install_service.log 
if exist %install_service_output% ( del %install_service_output% /f )

echo [32m[install_service INFO] Start to install service: %service_name%, path: %app_path%\%app_name%[0m
echo [install_service INFO] Start to install service: %service_name%, path: %app_path%\%app_name% >> %install_service_output%
%nssm_exe% install %service_name% %app_path%\%app_name%
set install_error_code=%errorlevel%
if %install_error_code% equ 3 (
	echo [31m[install_service ERROR] Can not find the corresponding path.[0m
	echo [install_service ERROR] Can not find the corresponding path. >> %install_service_output%
	goto Fail
) else if %install_error_code% equ 5 (
	echo [33m[install_service WARN] The service "Converter" is already installed.[0m
	echo [install_service WARN] The service "Converter" is already installed. >> %install_service_output%
) else if %install_error_code% gtr 0 (
	echo [31m[install_service ERROR] Error code %install_error_code%[0m
	echo [install_service ERROR] Error code %install_error_code% >> %install_service_output%
	goto Fail
)

echo [32m[install_service INFO] Set service properties.[0m
echo [install_service INFO] Set service properties. >> %install_service_output%
%nssm_exe% set %service_name% AppDirectory %app_path%
if %errorlevel% gtr 0 (
	echo [31m[install_service ERROR] Failed to set AppDirectory. The Converter requires administrator permission to manage services.[0m
	echo [install_service ERROR] Failed to set AppDirectory. The Converter requires administrator permission to manage services >> %install_service_output%
	goto Fail
)

%nssm_exe% set %service_name% DisplayName %service_name%
if %errorlevel% gtr 0 (
	echo [32m[install_service ERROR] Failed to set DisplayName. The Converter requires administrator permission to manage services.[0m
	echo [install_service ERROR] Failed to set DisplayName. The Converter requires administrator permission to manage services. >> %install_service_output%
	goto Fail
)
%nssm_exe% set %service_name% Description Converter
if %errorlevel% gtr 0 (
	echo [31m[install_service ERROR] Failed to set Description. The Converter requires administrator permission to manage services.[0m
	echo [install_service ERROR] Failed to set Description. The Converter requires administrator permission to manage services. >> %install_service_output%
	goto Fail
)

%nssm_exe% set %service_name% AppNoConsole 0
if %errorlevel% gtr 0 (
	echo [31m[install_service Error] Failed to set AppNoConsole. The Converter requires administrator permission to manage services.][0m
	echo [install_service Error] Failed to set AppNoConsole. The Converter requires administrator permission to manage services.] >> %install_service_output%
	goto Fail
)

echo [32m[install_service INFO] Set service startup type.[0m
%nssm_exe% set %service_name% Start SERVICE_DELAYED_AUTO_START
if %errorlevel% gtr 0 (
	echo [31m[install_service ERROR] Failed to set autostart. The Converter requires administrator permission to manage services.[0m
	echo [install_service ERROR] Failed to set autostart. The Converter requires administrator permission to manage services. >> %install_service_output%
	goto Fail
)

:Success
echo [32m[install_service INFO] The service "Converter" has been installed.[0m
echo [install_service INFO] The service "Converter" has been installed. >> %install_service_output%
goto End

:Fail
goto End

:End
