
# Safely wipes any Lx registrations from the Windows Registry
# Use this to wipe stale items as well

Set-StrictMode -Version Latest

# 1 = preview changes without deleting anything
# 0 = actually remove matching registry values and keys
$dryRun = 0

if ($dryRun) {
	Write-Host "Simulating Lx registry cleanup:"
} else {
	Write-Host "Running Lx registry cleanup:"
}

Write-Host "Removing Lx classes..."

$classesRoot =
	"Registry::HKEY_CURRENT_USER\Software\Classes"

$settingsKeys = @(
	"Registry::HKEY_CURRENT_USER\Software\Lx",
	"Registry::HKEY_CURRENT_USER\Software\LxPortable"
)

$applicationKeys = @(
	"Registry::HKEY_CURRENT_USER\Software\Classes\Applications\lx.exe",
	"Registry::HKEY_CURRENT_USER\Software\Classes\Applications\LxPortable.exe"
)

$registeredApplicationNames = @("Lx", "LxPortable")

$registeredApplicationsKey =
	"Registry::HKEY_CURRENT_USER\Software\RegisteredApplications"

# Remove all Lx and LxPortable file handler keys
Get-ChildItem $classesRoot -ErrorAction SilentlyContinue |
    Where-Object {
        $_.PSChildName -like "Lx.File.*" -or
            $_.PSChildName -like "LxPortable.File.*"
    } |
    ForEach-Object {
        if ($dryRun) {
            Write-Host "  Would remove: $($_.Name)"
        } else {
            Write-Host "  Remove: $($_.Name)"
            Remove-Item $_.PSPath -Recurse -Force
        }
    }

Write-Host "Removing Lx links..."

# Remove all Lx and LxPortable OpenWithProgids values
# from HKCU\Software\Classes\.<extension>\OpenWithProgids

Get-ChildItem $classesRoot -ErrorAction SilentlyContinue |
    Where-Object {
        $_.PSChildName -like ".*"
    } |
    ForEach-Object {
        $openWithProgids = Join-Path $_.PSPath "OpenWithProgids"

        if (Test-Path $openWithProgids) {
            $key = Get-Item $openWithProgids

            foreach ($valueName in $key.GetValueNames()) {
                if ($valueName -like "Lx.File.*" -or
                    $valueName -like "LxPortable.File.*") {
                    if ($dryRun) {
                        Write-Host "  Would remove: $openWithProgids -> $valueName"
                    } else {
                        Write-Host "  Remove: $openWithProgids -> $valueName"

                        Remove-ItemProperty `
                            -Path $openWithProgids `
                            -Name $valueName `
                            -ErrorAction SilentlyContinue
                    }
                }
            }
        }
    }

Write-Host "Removing Lx registration..."

# Remove both application settings trees
foreach ($settingsKey in $settingsKeys) {
    if (Test-Path $settingsKey) {
        if ($dryRun) {
            Write-Host "  Would remove: $settingsKey"
        } else {
            Write-Host "  Remove: $settingsKey"
            Remove-Item $settingsKey -Recurse -Force
        }
    }
}

# Remove both application handler trees
foreach ($applicationKey in $applicationKeys) {
    if (Test-Path $applicationKey) {
        if ($dryRun) {
            Write-Host "  Would remove: $applicationKey"
        } else {
            Write-Host "  Remove: $applicationKey"
            Remove-Item $applicationKey -Recurse -Force
        }
    }
}

# Remove both RegisteredApplications values
if (Test-Path $registeredApplicationsKey) {
    $key = Get-Item $registeredApplicationsKey

    foreach ($applicationName in $registeredApplicationNames) {
        if ($key.GetValueNames() -ccontains $applicationName) {
            if ($dryRun) {
                Write-Host "  Would remove: $registeredApplicationsKey -> $applicationName"
            } else {
                Write-Host "  Remove: $registeredApplicationsKey -> $applicationName"

                Remove-ItemProperty `
                    -Path $registeredApplicationsKey `
                    -Name $applicationName `
                    -ErrorAction SilentlyContinue
            }
        }
    }
}

Write-Host "All done."
