#!/bin/pwsh
param($context)

. "$PSScriptRoot/../../Scripts/Context.ps1";

[Context] $context = $context;
$tempDir = $context.GetTempDirectory();

Push-Location $tempDir;
$configFile = "$tempDir/DefaultAssociations.xml";

Write-Information "Making Firefox the default browser";
DISM /Online "/Export-DefaultAppAssociations:$configFile";

[xml]$defaultAssociations = [xml]::new();
$defaultAssociations.PreserveWhitespace = $true;
$reader = [System.Xml.XmlReader]::Create("$configFile", $readerSettings);
$defaultAssociations.Load($reader);
$reader.Dispose();

$extensions = @(
    ".htm",
    ".html",
    ".shtml",
    ".svg",
    ".xht",
    ".xhtml"
);

$schemes = @(
    "http",
    "https"
);

foreach ($association in $defaultAssociations.SelectNodes("/DefaultAssociations/Association")) {
    [string] $className = $null;

    if (($extensions + $schemes) -contains $association.Identifier) {
        $association.ApplicationName = "Firefox";

        if ($extensions -contains $association.Identifier) {
            $className = "FirefoxHTML";
        } else {
            $className = "FirefoxURL";
        }

        $association.ProgId = "$className-308046B0AF4A39CB";
    }
}

$defaultAssociations.Save($configFile);
DISM /Online "/Import-DefaultAppAssociations:$configFile";

Pop-Location;
Remove-Item -Recurse $tempDir;