$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"

# Fix vhosts
try {
    $vhostsContent = Get-Content -Path $vhostsPath -Raw
    # Fix the specific syntax error
    $vhostsContent = $vhostsContent -replace "</VirtualHost><VirtualHost \*:8090>", "</VirtualHost>`r`n<VirtualHost *:8090>"
    
    # Ensure the 8090 block has the correct proxy config if it was messed up or partial
    # (Assuming the previous append might have been partial or we just want to ensure correctness)
    # We will just fix the syntax error primarily. The content I saw earlier looked mostly correct except for the line break.
    
    Set-Content -Path $vhostsPath -Value $vhostsContent -Encoding UTF8
    Write-Host "Fixed httpd-vhosts.conf"
} catch {
    Write-Error "Failed to fix vhosts: $_"
}

# Fix httpd.conf
try {
    $httpdContent = Get-Content -Path $httpdPath -Raw
    
    # Add Listen 8090 if missing
    if ($httpdContent -notmatch "Listen 8090") {
        $httpdContent = $httpdContent -replace "(Listen 8333)", "$1`r`nListen 8090"
    }
    
    # Enable proxy modules
    $httpdContent = $httpdContent -replace "#LoadModule proxy_module", "LoadModule proxy_module"
    $httpdContent = $httpdContent -replace "#LoadModule proxy_http_module", "LoadModule proxy_http_module"
    
    Set-Content -Path $httpdPath -Value $httpdContent -Encoding UTF8
    Write-Host "Fixed httpd.conf"
} catch {
    Write-Error "Failed to fix 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: $_"
}
