# madarX OS installer (Windows, PowerShell) — best-effort; Windows is not fully tested.
#
#   iwr -useb https://raw.githubusercontent.com/kareemodev777/madarx-os/main/scripts/install.ps1 | iex
#
# Installs the `madarx` command globally via npm. Data lives in
# %USERPROFILE%\.madarx, so upgrades never touch your data.

$ErrorActionPreference = 'Stop'
$Pkg = 'madarx'

Write-Host 'madarX OS installer' -ForegroundColor Blue

if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
  Write-Host 'Node.js is not installed.' -ForegroundColor Red
  Write-Host 'madarX OS needs Node.js >= 22.5. Install from https://nodejs.org/ (22 LTS or newer), then re-run.'
  exit 1
}

$nodeVer = (node -v).TrimStart('v')
$major = [int]($nodeVer.Split('.')[0])
$minor = [int]($nodeVer.Split('.')[1])
if ($major -lt 22 -or ($major -eq 22 -and $minor -lt 5)) {
  Write-Host "Node.js $nodeVer is too old — madarX OS needs >= 22.5." -ForegroundColor Red
  exit 1
}
Write-Host "Node.js $nodeVer OK" -ForegroundColor Green

Write-Host "Installing $Pkg globally..." -ForegroundColor Blue
npm install -g "$Pkg@latest" --no-audit --no-fund
if ($LASTEXITCODE -ne 0) { Write-Host 'Global install failed.' -ForegroundColor Red; exit 1 }

Write-Host 'Installed. Next steps:' -ForegroundColor Green
Write-Host '  madarx setup     # configure the master key, folder permissions, keys'
Write-Host '  madarx           # start everything'
