$vhostsPath = "C:\laragon\bin\apache\httpd-2.4.54-win64-VS16\conf\extra\httpd-vhosts.conf"
$httpdPath = "C:\laragon\bin\apache\httpd-2.4.54-win64-VS16\conf\httpd.conf"

# Revert httpd-vhosts.conf
try {
    $vhostsContent = Get-Content -Path $vhostsPath -Raw
    
    # Remove the VirtualHost 8090 block
    # We look for the block starting with <VirtualHost *:8090> and ending with </VirtualHost>
    # We use regex with single-line mode (?s)
    if ($vhostsContent -match "(?s)<VirtualHost \*:8090>.*?</VirtualHost>") {
        $vhostsContent = $vhostsContent -replace "(?s)\s*<VirtualHost \*:8090>.*?</VirtualHost>", ""
        Set-Content -Path $vhostsPath -Value $vhostsContent -Encoding UTF8
        Write-Host "Removed VirtualHost *:8090 from httpd-vhosts.conf"
    } else {
        Write-Host "VirtualHost *:8090 not found in httpd-vhosts.conf"
    }
    
    # Also clean up any potential leftover syntax error if the previous fix failed
    # e.g. </VirtualHost><VirtualHost... if we didn't match the block above
    # But the block removal should catch it if regex matches.
} catch {
    Write-Error "Failed to revert vhosts: $_"
}

# Revert httpd.conf
try {
    $httpdContent = Get-Content -Path $httpdPath -Raw
    
    # Remove Listen 8090
    if ($httpdContent -match "Listen 8090") {
        $httpdContent = $httpdContent -replace "(?m)^Listen 8090\s*(\r\n|\n)?", ""
        Write-Host "Removed Listen 8090 from httpd.conf"
    }
    
    # Re-comment proxy modules
    if ($httpdContent -match "^LoadModule proxy_module") {
        $httpdContent = $httpdContent -replace "(?m)^LoadModule proxy_module", "#LoadModule proxy_module"
        Write-Host "Disabled proxy_module"
    }
    if ($httpdContent -match "^LoadModule proxy_http_module") {
        $httpdContent = $httpdContent -replace "(?m)^LoadModule proxy_http_module", "#LoadModule proxy_http_module"
        Write-Host "Disabled proxy_http_module"
    }
    
    Set-Content -Path $httpdPath -Value $httpdContent -Encoding UTF8
    Write-Host "Reverted httpd.conf"
} catch {
    Write-Error "Failed to revert httpd.conf: $_"
}

# Restart Apache
try {
    & "C:\laragon\bin\apache\httpd-2.4.54-win64-VS16\bin\httpd.exe" -k restart
    Write-Host "Apache restarted"
} catch {
    Write-Error "Failed to restart Apache: $_"
}
