param(
  [string]$AppName = 'MotorCADParameterScan'
)

$ErrorActionPreference = 'Stop'
Set-Location -LiteralPath $PSScriptRoot

$safeDir = $PSScriptRoot.Replace('\', '/')
$commit = git -c "safe.directory=$safeDir" rev-parse --short HEAD
if ($LASTEXITCODE -ne 0) {
    throw 'No Git commit exists.'
}
$dirty = git -c "safe.directory=$safeDir" status --porcelain --untracked-files=no
if ($dirty) {
    throw 'Tracked files have uncommitted changes. Commit before building.'
}

$buildInfo = "BUILD_COMMIT = '$commit'`n"
[System.IO.File]::WriteAllText((Join-Path $PSScriptRoot 'build_info.py'), $buildInfo, [System.Text.Encoding]::ASCII)

python -m PyInstaller `
  --noconfirm `
  --clean `
  --windowed `
  --name $AppName `
  --collect-all ansys.motorcad.core `
  --add-data "scan_parameters.json;." `
  motorcad_scan_gui.py

Copy-Item -LiteralPath '.\scan_parameters.json' -Destination ".\dist\$AppName\scan_parameters.json" -Force
Write-Host "Build complete: $PSScriptRoot\dist\$AppName\$AppName.exe"
