param(
    [int]$Port = 3210
)

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

Set-Location $PSScriptRoot

$pythonCommand = $null
$pythonArgs = $null

if (Get-Command py -ErrorAction SilentlyContinue) {
    $pythonCommand = 'py'
    $pythonArgs = @('-3', '-m', 'http.server', "$Port", '--bind', '127.0.0.1')
} elseif (Get-Command python -ErrorAction SilentlyContinue) {
    $pythonCommand = 'python'
    $pythonArgs = @('-m', 'http.server', "$Port", '--bind', '127.0.0.1')
}

if (-not $pythonCommand) {
    throw 'Python 3 is required to run the offline controller package.'
}

Write-Host "Serving offline controller at http://127.0.0.1:$Port/index.html"
Write-Host 'Press Ctrl+C to stop.'
& $pythonCommand @pythonArgs
