54 lines
4.9 KiB
PowerShell
54 lines
4.9 KiB
PowerShell
param([string]$BpqRoot,[int]$Port,[int]$HttpPort,[int]$RadioPort,[string]$SysopUser,[string]$ForwardPeer,[string]$CmsPeer,[string]$VaraLogPath,[string]$NodeExe,[ValidateSet("it","en")][string]$Language,[switch]$EnableRadioCAT,[switch]$DisableRadioCAT,[switch]$CheckOnly,[switch]$AutoStart,[switch]$EnableLAN,[switch]$Restart)
|
|
$ErrorActionPreference='Stop'
|
|
if($EnableRadioCAT -and $DisableRadioCAT){throw 'Scegliere una sola opzione CAT.'}
|
|
if($AutoStart -and !$CheckOnly -and ($env:BPQ_CONSOLE_CONFIG -or $env:BPQ_ROOT -or $env:BPQ_CONSOLE_PORT)){throw 'Per -AutoStart usare console.config.json nella cartella del progetto, senza override BPQ_* temporanei.'}
|
|
$configPath=if($env:BPQ_CONSOLE_CONFIG){$env:BPQ_CONSOLE_CONFIG}else{Join-Path $PSScriptRoot 'console.config.json'}
|
|
$saved=$null
|
|
if(Test-Path -LiteralPath $configPath){try{$saved=Get-Content -LiteralPath $configPath -Raw | ConvertFrom-Json}catch{throw 'Configurazione locale non leggibile: correggere il JSON prima di reinstallare.'}}
|
|
if(!$NodeExe){$NodeExe=if($saved.nodeExe -and (Test-Path -LiteralPath $saved.nodeExe)){$saved.nodeExe}else{(Get-Command node.exe -ErrorAction SilentlyContinue).Source}}
|
|
if(!$NodeExe -or !(Test-Path -LiteralPath $NodeExe)){throw 'Installare Node.js 20 o successivo, oppure specificare -NodeExe con il percorso di node.exe.'}
|
|
$version=& $NodeExe --version
|
|
if($LASTEXITCODE -ne 0 -or $version -notmatch '^v(\d+)\.' -or [int]$Matches[1] -lt 20){throw 'Serve Node.js 20 o successivo.'}
|
|
if(!$BpqRoot){
|
|
if($env:BPQ_ROOT){$BpqRoot=$env:BPQ_ROOT}
|
|
elseif($saved.bpqRoot){$BpqRoot=$saved.bpqRoot}
|
|
else{
|
|
$candidates=@((Join-Path $env:APPDATA 'BPQ32'))
|
|
$running=Get-CimInstance Win32_Process | Where-Object {$_.Name -match '^(BPQTerminal|BPQMail|BPQChat|bpq32)\.exe$'}
|
|
foreach($proc in $running){if($proc.ExecutablePath){$candidates+=Split-Path $proc.ExecutablePath}}
|
|
$BpqRoot=$candidates | Where-Object {Test-Path -LiteralPath (Join-Path $_ 'BPQ32.cfg')} | Select-Object -First 1
|
|
}
|
|
if(!$BpqRoot){throw 'BPQ32.cfg non trovato. Usare -BpqRoot con la cartella dati BPQ.'}
|
|
}
|
|
if(!$CheckOnly -and $Restart){
|
|
$pending=Join-Path $PSScriptRoot 'winlink-radio.session.json'
|
|
if((Test-Path -LiteralPath $pending) -and (Get-Content -LiteralPath $pending -Raw | ConvertFrom-Json).restoring){throw 'Attendere il ripristino della sessione Winlink radio prima di reinstallare e riavviare.'}
|
|
}
|
|
$targetPort=if($PSBoundParameters.ContainsKey('Port')){$Port}elseif($env:BPQ_CONSOLE_PORT){[int]$env:BPQ_CONSOLE_PORT}elseif($saved.port){[int]$saved.port}else{8090}
|
|
if($targetPort -ge 1024 -and $targetPort -le 65535){
|
|
$listener=Get-NetTCPConnection -LocalPort $targetPort -State Listen -ErrorAction SilentlyContinue
|
|
if($listener){$owner=Get-CimInstance Win32_Process -Filter "ProcessId=$($listener[0].OwningProcess)";if($owner.Name -ne 'node.exe' -or !$owner.CommandLine -or $owner.CommandLine.IndexOf((Join-Path $PSScriptRoot 'server.mjs'),[StringComparison]::OrdinalIgnoreCase) -lt 0){throw "Porta $targetPort occupata da un altro processo. Specificare -Port con una porta libera."}}
|
|
}
|
|
$nodeArgs=@((Join-Path $PSScriptRoot 'install.mjs'),'--bpq-root',$BpqRoot,'--config-path',$configPath)
|
|
$mapping=@{Port='port';HttpPort='http-port';RadioPort='radio-port';SysopUser='sysop-user';ForwardPeer='forward-peer';CmsPeer='cms-peer';VaraLogPath='vara-log';Language='language'}
|
|
foreach($name in $mapping.Keys){if($PSBoundParameters.ContainsKey($name)){$nodeArgs+=@(('--'+$mapping[$name]),[string](Get-Variable -Name $name -ValueOnly))}}
|
|
if(!$VaraLogPath){$vara=Get-Process -Name VARA -ErrorAction SilentlyContinue | Select-Object -First 1;if($vara.Path){$varaLog=Join-Path (Split-Path $vara.Path) 'VARAHF.log';if(Test-Path -LiteralPath $varaLog){$nodeArgs+=@('--vara-log',$varaLog)}}}
|
|
if($EnableRadioCAT){$nodeArgs+='--enable-radio-cat'}
|
|
if($DisableRadioCAT){$nodeArgs+='--disable-radio-cat'}
|
|
if($CheckOnly){$nodeArgs+='--check-only'}
|
|
& $NodeExe @nodeArgs
|
|
if($LASTEXITCODE -ne 0){throw 'Installazione interrotta: verifica BPQ fallita.'}
|
|
if($CheckOnly){return}
|
|
if($AutoStart){
|
|
$identity=[Security.Principal.WindowsIdentity]::GetCurrent().Name
|
|
$action=New-ScheduledTaskAction -Execute (Join-Path $env:SystemRoot 'System32\WindowsPowerShell\v1.0\powershell.exe') -Argument ('-NoProfile -ExecutionPolicy RemoteSigned -WindowStyle Hidden -File "'+(Join-Path $PSScriptRoot 'Start-Console.ps1')+'"') -WorkingDirectory $PSScriptRoot
|
|
$trigger=New-ScheduledTaskTrigger -AtLogOn -User $identity
|
|
$principal=New-ScheduledTaskPrincipal -UserId $identity -LogonType Interactive -RunLevel Limited
|
|
Register-ScheduledTask -TaskName 'BPQ-WebConsole' -Action $action -Trigger $trigger -Principal $principal -Description 'BPQ web console locale' -Force | Out-Null
|
|
}
|
|
& (Join-Path $PSScriptRoot 'Start-Console.ps1') -Restart:$Restart
|
|
if($EnableLAN){& (Join-Path $PSScriptRoot 'Enable-LAN.ps1')}
|
|
$current=Get-Content -LiteralPath $configPath -Raw | ConvertFrom-Json
|
|
$displayPort=if($env:BPQ_CONSOLE_PORT){$env:BPQ_CONSOLE_PORT}else{$current.port}
|
|
Write-Host "Pannello: http://127.0.0.1:$displayPort"
|