$ErrorActionPreference = "SilentlyContinue"

$installRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$desktopShortcut = Join-Path ([Environment]::GetFolderPath("Desktop")) "Know God More.lnk"
$startMenuFolder = Join-Path ([Environment]::GetFolderPath("Programs")) "Know God More"
$uninstallKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\Know God More"
$pidFile = Join-Path $installRoot "server.pid"

if (Test-Path -LiteralPath $pidFile) {
  $serverPid = [int](Get-Content -LiteralPath $pidFile | Select-Object -First 1)
  if ($serverPid -gt 0) {
    Stop-Process -Id $serverPid -Force
  }
}

Remove-Item -LiteralPath $desktopShortcut -Force
Remove-Item -LiteralPath $startMenuFolder -Recurse -Force
Remove-Item -Path $uninstallKey -Recurse -Force

$removeScript = Join-Path $env:TEMP "remove-know-god-more.cmd"
Set-Content -LiteralPath $removeScript -Encoding ASCII -Value "@echo off`r`ntimeout /t 2 /nobreak > nul`r`nrmdir /s /q `"$installRoot`"`r`ndel `"%~f0`"`r`n"
Start-Process -FilePath $removeScript -WindowStyle Minimized
