@echo off
setlocal EnableExtensions

rem Automatically request Administrator privileges through Windows UAC.
fltmc >nul 2>&1
if errorlevel 1 (
    set "FIREWALL_SCRIPT_PATH=%~f0"
    echo Requesting Administrator privileges...
    powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath $env:FIREWALL_SCRIPT_PATH -Verb RunAs"
    if errorlevel 1 (
        echo ERROR: Administrator privileges were not granted.
        pause
        exit /b 1
    )
    exit /b 0
)

echo Configuring Windows Firewall rules for TCP ports 5995, 50063 and 8080...

for %%P in (5995 50063 8080) do (
    rem Remove only rules previously created by this script, making reruns safe.
    netsh advfirewall firewall delete rule name="Allow TCP %%P Inbound"  dir=in  protocol=TCP localport=%%P >nul 2>&1
    netsh advfirewall firewall delete rule name="Allow TCP %%P Outbound" dir=out protocol=TCP remoteport=%%P >nul 2>&1

    netsh advfirewall firewall add rule name="Allow TCP %%P Inbound"  dir=in  action=allow protocol=TCP localport=%%P  profile=any enable=yes >nul
    if errorlevel 1 goto :error

    netsh advfirewall firewall add rule name="Allow TCP %%P Outbound" dir=out action=allow protocol=TCP remoteport=%%P profile=any enable=yes >nul
    if errorlevel 1 goto :error
)

echo.
echo Done. Inbound and outbound TCP rules were added successfully.
pause
exit /b 0

:error
echo.
echo ERROR: A firewall rule could not be created.
echo Make sure the Windows Firewall service is enabled and try again.
pause
exit /b 1
