# uninstall-service.ps1
# Stops + removes the WindroseServerManager Windows service. Does NOT delete the install
# directory or its data/ + logs/ subdirs — pass -RemoveInstallDir to nuke those too.
#
# Usage (from an Administrator PowerShell):
#   .\scripts\uninstall-service.ps1
#   .\scripts\uninstall-service.ps1 -ServiceName "WSM" -RemoveInstallDir

[CmdletBinding()]
param(
    [string]$ServiceName = "WindroseServerManager",
    [string]$InstallDir = "C:\Program Files\WindroseServerManager",
    [switch]$RemoveInstallDir
)

$ErrorActionPreference = "Stop"

if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Write-Error "This script must be run from an elevated (Administrator) PowerShell."
    exit 1
}

$svc = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if (-not $svc) {
    Write-Host "Service '$ServiceName' not found — nothing to uninstall."
}
else {
    if ($svc.Status -ne 'Stopped') {
        Write-Host "=== Stopping '$ServiceName' ==="
        Stop-Service -Name $ServiceName -Force
        Start-Sleep -Seconds 2
    }
    Write-Host "=== Deleting service '$ServiceName' ==="
    sc.exe delete $ServiceName | Out-Null
}

if ($RemoveInstallDir -and (Test-Path $InstallDir)) {
    Write-Host "=== Removing install directory $InstallDir ==="
    Remove-Item -Recurse -Force $InstallDir
}
elseif (Test-Path $InstallDir) {
    Write-Host ""
    Write-Host "Install dir kept: $InstallDir"
    Write-Host "Pass -RemoveInstallDir to delete it (this nukes data/ + logs/ too)."
}

Write-Host ""
Write-Host "Done."
