@echo off
setlocal
set "BASE_URL=https://www.iavelora.it"
set "INSTALL_DIR=%LOCALAPPDATA%\VeloraAgent"
set "LAUNCH_URL=velora-agent://start?port=8765&token=&workspace=&fullAccess=false"
mkdir "%INSTALL_DIR%" >nul 2>nul
where node >nul 2>nul
if errorlevel 1 (
  echo Node.js non risulta installato. Installa Node.js LTS da https://nodejs.org/ e rilancia questo installer.
  pause
  exit /b 1
)
powershell -NoProfile -ExecutionPolicy Bypass -Command "Invoke-WebRequest -UseBasicParsing -Uri '%BASE_URL%/desktop-agent/velora-agent.mjs' -OutFile '%INSTALL_DIR%\velora-agent.mjs'"
if errorlevel 1 (
  echo Download agent non riuscito.
  pause
  exit /b 1
)
> "%INSTALL_DIR%\launch.ps1" (
  echo $url = if ^($args.Count -gt 0^) { $args[0] } else { "" }
  echo $workspace = Join-Path $env:USERPROFILE "Documents"
  echo $port = "8765"
  echo $token = ""
  echo $fullAccess = "false"
  echo if ^($url -match "workspace=([^&]+)"^) { $workspace = [uri]::UnescapeDataString^($Matches[1]^).Replace^("/", "\"^) }
  echo if ^($url -match "port=([0-9]+)"^) { $port = $Matches[1] }
  echo if ^($url -match "token=([^&]+)"^) { $token = [uri]::UnescapeDataString^($Matches[1]^) }
  echo if ^($url -match "fullAccess=([^&]+)"^) { $fullAccess = [uri]::UnescapeDataString^($Matches[1]^) }
  echo if ^(-not ^(Test-Path -LiteralPath $workspace^)^) { New-Item -ItemType Directory -Force -Path $workspace ^| Out-Null }
  echo $agent = Join-Path $env:LOCALAPPDATA "VeloraAgent\velora-agent.mjs"
  echo $command = "node `"$agent`" --workspace `"$workspace`" --port $port"
  echo if ^($token^) { $command += " --token `"$token`"" }
  echo if ^($fullAccess -eq "true"^) { $command += " --fullAccess true" }
  echo Get-NetTCPConnection -LocalPort $port -State Listen -ErrorAction SilentlyContinue ^| ForEach-Object { $p = Get-CimInstance Win32_Process -Filter "ProcessId=$($_.OwningProcess)" -ErrorAction SilentlyContinue; if ^($p.CommandLine -like "*VeloraAgent*velora-agent.mjs*"^) { Stop-Process -Id $_.OwningProcess -Force -ErrorAction SilentlyContinue } }
  echo Start-Process powershell.exe -ArgumentList "-NoExit", "-NoProfile", "-Command", $command
)
> "%INSTALL_DIR%\start-velora-agent.cmd" (
  echo @echo off
  echo powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%%LOCALAPPDATA%%\VeloraAgent\launch.ps1" %%*
)
reg add "HKCU\Software\Classes\velora-agent" /ve /d "URL:Velora Desktop Agent" /f >nul
reg add "HKCU\Software\Classes\velora-agent" /v "URL Protocol" /d "" /f >nul
reg add "HKCU\Software\Classes\velora-agent\shell\open\command" /ve /d "\"%INSTALL_DIR%\start-velora-agent.cmd\" \"%%1\"" /f >nul
> "%USERPROFILE%\Desktop\Avvia Velora Desktop Agent.cmd" (
  echo @echo off
  echo powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%%LOCALAPPDATA%%\VeloraAgent\launch.ps1" "%LAUNCH_URL%"
)
echo Velora Desktop Agent installato.
echo Avvio agent locale...
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%LOCALAPPDATA%\VeloraAgent\launch.ps1" "%LAUNCH_URL%"
echo Ora torna su Velora. Il collegamento parte automaticamente; se serve premi Stato.
pause
