$ErrorActionPreference = "Stop"

$appDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$input = Join-Path $appDir "FluxAnalyzer.ps1"
$output = Join-Path $appDir "FluxAnalyzer.exe"
$icon = Join-Path $appDir "logo.ico"

$ps2exeCommand = Get-Command Invoke-ps2exe -ErrorAction SilentlyContinue
if (-not $ps2exeCommand) {
  $module = Get-Module -ListAvailable -Name ps2exe | Select-Object -First 1
  if ($module) {
    Import-Module ps2exe
    $ps2exeCommand = Get-Command Invoke-ps2exe -ErrorAction SilentlyContinue
  }
}

if (-not $ps2exeCommand) {
  Write-Host "ps2exe is not installed."
  Write-Host "Install it in PowerShell with:"
  Write-Host "Install-Module ps2exe -Scope CurrentUser"
  Write-Host ""
  Write-Host "You can still run Flux with Launch Flux.vbs or Start-FluxAnalyzer.bat."
  exit 0
}

$params = @{
  InputFile = $input
  OutputFile = $output
  NoConsole = $true
  Title = "Flux Analyzer"
  Product = "Flux Analyzer"
  Company = "Plokar"
  Version = "1.0.7"
}

if (Test-Path $icon) {
  $params.IconFile = $icon
}

Invoke-ps2exe @params
Write-Host "Created:"
Write-Host $output
