@echo off
setlocal EnableExtensions

rem Requires Administrator.
rem Removes the custom syncservice.groovestats.com hosts redirect.

set "ALIAS=syncservice.groovestats.com"
set "MARKER=# itgmania-custom-sync"
set "HOSTS=%SystemRoot%\System32\drivers\etc\hosts"

net session >nul 2>&1
if errorlevel 1 (
  echo Run this script as Administrator.
  pause
  exit /b 1
)

powershell -NoProfile -Command ^
  "$p='%HOSTS%'; $alias='%ALIAS%'; $marker='%MARKER%';" ^
  "$lines = Get-Content -LiteralPath $p -ErrorAction Stop;" ^
  "$kept = $lines | Where-Object { $_ -notmatch [regex]::Escape($alias) -and $_ -notmatch [regex]::Escape($marker) };" ^
  "$kept | Set-Content -LiteralPath $p -Encoding ascii"
if errorlevel 1 (
  echo Failed to update hosts file.
  pause
  exit /b 1
)

ipconfig /flushdns >nul
echo Disabled: removed %ALIAS% redirect
echo.
echo Checking...

findstr /C:"%ALIAS%" "%HOSTS%" >nul
if not errorlevel 1 (
  echo FAIL: %ALIAS% is still present in hosts:
  findstr /C:"%ALIAS%" "%HOSTS%"
  pause
  exit /b 1
)
echo OK: no %ALIAS% entry in hosts

for /f "tokens=1" %%R in ('powershell -NoProfile -Command "try { (Resolve-DnsName -Name '%ALIAS%' -Type A -ErrorAction Stop | Select-Object -First 1).IPAddress } catch { 'DNS' }"') do set "RESOLVED=%%R"
echo Info: %ALIAS% now resolves via DNS to %RESOLVED%

echo.
pause
endlocal
