# Fix startup.json WebDir paths
# This script finds and fixes incorrect WebDir paths in startup.json files

Write-Host "=================================================" -ForegroundColor Cyan
Write-Host "Jellyfin startup.json WebDir Fixer" -ForegroundColor Cyan
Write-Host "=================================================" -ForegroundColor Cyan
Write-Host ""

$searchPaths = @(
    ".",
    ".\bin\Debug\net11.0",
    ".\bin\Release\net11.0",
    ".\bin\Debug\net11.0\config",
    ".\bin\Release\net11.0\config"
)

$filesFound = 0
$filesFixed = 0

foreach ($path in $searchPaths) {
    $jsonPath = Join-Path $path "startup.json"
    
    if (Test-Path $jsonPath) {
        $filesFound++
        Write-Host "Found: $jsonPath" -ForegroundColor Yellow
        
        try {
            $content = Get-Content $jsonPath -Raw
            $originalContent = $content
            
            # Check for incorrect WebDir
            if ($content -match '"WebDir":\s*"[^"]*C:/ProgramData/jellyfin/wwwroot[^"]*"') {
                Write-Host "  Status: Has incorrect WebDir path" -ForegroundColor Red
                
                # Create backup
                $backupPath = "$jsonPath.backup.$(Get-Date -Format 'yyyyMMdd_HHmmss')"
                Copy-Item $jsonPath $backupPath
                Write-Host "  Backup: $backupPath" -ForegroundColor Gray
                
                # Fix the path
                $content = $content -replace '"WebDir":\s*"C:/ProgramData/jellyfin/wwwroot"', '"WebDir": "wwwroot"'
                
                # Also fix any data path if it has /data appended
                $content = $content -replace '"WebDir":\s*"C:/ProgramData/jellyfin/data/wwwroot"', '"WebDir": "wwwroot"'
                
                Set-Content -Path $jsonPath -Value $content -NoNewline
                $filesFixed++
                
                Write-Host "  Result: FIXED!" -ForegroundColor Green
            }
            elseif ($content -match '"WebDir":\s*"wwwroot"') {
                Write-Host "  Status: Already correct (relative path)" -ForegroundColor Green
            }
            else {
                Write-Host "  Status: Has custom path (not changed)" -ForegroundColor Cyan
            }
        }
        catch {
            Write-Host "  ERROR: Failed to process - $_" -ForegroundColor Red
        }
        
        Write-Host ""
    }
}

Write-Host "=================================================" -ForegroundColor Cyan

if ($filesFound -eq 0) {
    Write-Host "No startup.json files found." -ForegroundColor Yellow
    Write-Host "Run Jellyfin once to create the configuration file." -ForegroundColor White
}
elseif ($filesFixed -eq 0) {
    Write-Host "All files are already correct!" -ForegroundColor Green
}
else {
    Write-Host "Fixed $filesFixed of $filesFound file(s)" -ForegroundColor Green
    Write-Host ""
    Write-Host "Next steps:" -ForegroundColor Yellow
    Write-Host "1. Rebuild your solution (if needed)" -ForegroundColor White
    Write-Host "2. Run Jellyfin to verify WebDir path is correct" -ForegroundColor White
}

Write-Host ""
