@echo off
setlocal EnableExtensions EnableDelayedExpansion

title FIXALL
mode con cols=65 lines=25
color 0F

:: ============================================================
:: Check Administrator
:: ============================================================

net session >nul 2>&1

if errorlevel 1 (
    echo [INFO] Requesting Administrator privileges...

    powershell -NoProfile -Command ^
    "Start-Process -FilePath '%~f0' -Verb RunAs"

    if errorlevel 1 (
        echo [ERROR] Failed to request Administrator privileges.
        pause
        exit /b 1
    )

    exit /b
)

echo [SUCCESS] Running as Administrator.

:: ============================================================
:: Reset HOSTS
:: ============================================================

set "HOSTS=%windir%\System32\drivers\etc\hosts"

echo.
echo [INFO] Resetting Hosts file...

:: Remove attributes if present
attrib -R -S -H "%HOSTS%" >nul 2>&1

:: Delete old hosts
if exist "%HOSTS%" (
    del /F /Q "%HOSTS%" >nul 2>&1
)

if exist "%HOSTS%" (
    echo [ERROR] Failed to delete Hosts file.
    pause
    exit /b 1
)

echo [SUCCESS] Old Hosts file deleted.

:: Create new empty hosts
type nul > "%HOSTS%" 2>nul

if errorlevel 1 (
    echo [ERROR] Failed to create new Hosts file.
    pause
    exit /b 1
)

if exist "%HOSTS%" (
    echo [SUCCESS] New Hosts file created successfully.
) else (
    echo [ERROR] Hosts file was not created.
    pause
    exit /b 1
)

:: ============================================================
:: Delete keyauth.win certificates
:: ============================================================

echo.
echo [INFO] Removing keyauth.win certificates...

set /a CERT_DELETED=0

for %%S in (Root My CA AuthRoot) do (

    certutil -delstore "%%S" "keyauth.win" >nul 2>&1
    if not errorlevel 1 (
        set /a CERT_DELETED+=1
    )

    certutil -user -delstore "%%S" "keyauth.win" >nul 2>&1
    if not errorlevel 1 (
        set /a CERT_DELETED+=1
    )
)

if !CERT_DELETED! GTR 0 (
    echo [SUCCESS] Certificate removal completed.
    echo [INFO] Successful delete operations: !CERT_DELETED!
) else (
    echo [INFO] No matching certificate was found or deletion failed.
)

:: ============================================================
:: Done
:: ============================================================

echo.
echo ============================================
echo              FIXALL COMPLETED
echo ============================================
echo.

pause
exit /b 0