param(
    [string]$LiveScript = "$env:TEMP\windows-live-capture-test.ps1",
    [string]$ClocheExe = "cloche.exe",
    [string]$OutRoot = "$env:TEMP\cloche-live-test",
    [ValidateSet("active", "screen", "window")]
    [string]$Target = "active",
    [int]$WindowWidth = 680,
    [int]$WindowHeight = 460,
    [string]$TaskName = "ClocheLiveTest"
)

$ErrorActionPreference = 'Continue'

schtasks.exe /Delete /TN $TaskName /F 2>$null | Out-Null

$start = (Get-Date).AddMinutes(1).ToString("HH:mm")
$wrapper = Join-Path $env:TEMP "cloche-live-task.ps1"
@"
& '$LiveScript' -ClocheExe '$ClocheExe' -OutRoot '$OutRoot' -Target '$Target' -StyleSeed 424242 -WindowWidth $WindowWidth -WindowHeight $WindowHeight -LaunchNotepad
exit `$LASTEXITCODE
"@ | Set-Content -Path $wrapper -Encoding UTF8

$taskRun = 'powershell.exe -NoProfile -ExecutionPolicy Bypass -File "' + $wrapper + '"'

schtasks.exe /Create /TN $TaskName /SC ONCE /ST $start /TR $taskRun /F /IT
if ($LASTEXITCODE -ne 0) {
    throw "Failed to create scheduled task $TaskName"
}
schtasks.exe /Run /TN $TaskName
if ($LASTEXITCODE -ne 0) {
    throw "Failed to run scheduled task $TaskName"
}
