<#
    Removes Internoodle PDF Tool: files, registry entries, shortcuts and the
    Add/Remove Programs entry. Your saved filing folders are left alone, in
    %APPDATA%\Internoodle\PdfTool\settings.json, in case you reinstall.
#>
param(
    [string]$InstallRoot = (Join-Path $env:USERPROFILE 'Internoodle\PdfTool'),
    [string]$ProgId      = 'Internoodle.PdfTool.pdf',
    [string]$UninstallId = 'InternoodlePdfTool',
    [switch]$NoPrompt
)

$AppName = 'Internoodle PDF Tool'
$exeName = 'Internoodle PDF Tool.exe'
$classes = 'HKCU:\Software\Classes'

Write-Host ""
Write-Host "  Removing $AppName..." -ForegroundColor Cyan

Get-Process -Name ([IO.Path]::GetFileNameWithoutExtension($exeName)) -ErrorAction SilentlyContinue |
    Stop-Process -Force -ErrorAction SilentlyContinue
Start-Sleep -Milliseconds 500

foreach ($dir in @([Environment]::GetFolderPath('Programs'), [Environment]::GetFolderPath('Desktop'))) {
    if ($dir) { Remove-Item (Join-Path $dir "$AppName.lnk") -Force -ErrorAction SilentlyContinue }
}

Remove-Item "$classes\$ProgId" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "$classes\Applications\$exeName" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item 'HKCU:\Software\Internoodle\PdfTool' -Recurse -Force -ErrorAction SilentlyContinue
Remove-ItemProperty 'HKCU:\Software\RegisteredApplications' -Name $AppName -ErrorAction SilentlyContinue
Remove-ItemProperty "$classes\.pdf\OpenWithProgids" -Name $ProgId -ErrorAction SilentlyContinue
Remove-Item "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\$UninstallId" -Recurse -Force -ErrorAction SilentlyContinue

# the folder we are running from cannot delete itself while PowerShell holds it,
# so hand the last step to a detached command
if (Test-Path $InstallRoot) {
    Start-Process -WindowStyle Hidden -FilePath "cmd.exe" `
        -ArgumentList "/c timeout /t 2 /nobreak > nul & rmdir /s /q `"$InstallRoot`""
}

Write-Host "  Removed. PDFs will go back to whichever app you choose next." -ForegroundColor Green
Write-Host "  (Your saved filing folders are kept in %APPDATA%\Internoodle\PdfTool.)" -ForegroundColor DarkGray
Write-Host ""
if (-not $NoPrompt) { Read-Host "  Press Enter to close" }
