@echo off
@echo off
@setlocal enableextensions
@cd /d "%~dp0"
setlocal disabledelayedexpansion
goto check_Permissions

:check_Permissions
    net session >nul 2>&1
    if %errorLevel% == 0 (
			echo This user must be either a domain account with local admin priviledges and should be able to logon as a service.
			set /p "username=Enter the username with domain: "
			echo Enter password:
			
			for /f "delims=" %%a in ('powershell -command "$password = Read-Host -AsSecureString; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password); [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)"') do set "password=%%a"
			echo Stopping "APIGateway Service...
			sc query APIGateway| find "STATE" | find "RUNNING" > nul
			if %errorlevel% equ 0 (
				sc stop APIGateway
			)
			setlocal enabledelayedexpansion
			sc config APIGateway obj= "!username!" password= "!password!"
			
			if %errorlevel% neq 0 (
				echo Error: Failed to set the new username/password for the service.
				pause
			)

			sc start APIGateway

			set "serviceStatus="
			for /f "tokens=3 delims=: " %%A in ('sc query APIGateway ^| find "STATE"') do set "serviceStatus=%%A"

			echo Service modified, restarted, and current status is: "!serviceStatus!"
    ) else (
			echo ERROR: You need administrator privileges in order to start APIGateway service!
    )
pause

