param(
    [string]$TaskName = "RemoteDevops Agent",
    [string]$ConfigPath = "$env:LOCALAPPDATA\RemoteDevops\agent.json",
    [switch]$RemoveConfig
)

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"

if (Get-Command Get-ScheduledTask -ErrorAction SilentlyContinue) {
    $task = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
    if ($task) {
        Stop-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
        Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
        Write-Host "Removed scheduled task '$TaskName'"
    } else {
        Write-Host "Scheduled task '$TaskName' was not found"
    }
} else {
    schtasks.exe /Query /TN $TaskName 1>$null 2>$null
    if ($LASTEXITCODE -eq 0) {
        schtasks.exe /End /TN $TaskName 1>$null 2>$null
        schtasks.exe /Delete /TN $TaskName /F | Out-Null
        Write-Host "Removed scheduled task '$TaskName'"
    } else {
        Write-Host "Scheduled task '$TaskName' was not found"
    }
}

if ($RemoveConfig -and (Test-Path $ConfigPath)) {
    Remove-Item $ConfigPath -Force
    Write-Host "Removed config: $ConfigPath"
}
