#!/usr/bin/env pwsh
# AeThex OS - Unikernel Build Wrapper for Windows/WSL

Write-Host "═══════════════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host "  AeThex OS - Unikernel Builder (Windows -> WSL)" -ForegroundColor Cyan
Write-Host "═══════════════════════════════════════════════════════════════"
Write-Host ""

# 1. Check if OPS is installed in WSL
$checkOps = wsl bash -c "command -v ops"
if (-not $checkOps) {
    Write-Host "[!] 'ops' is not installed in WSL." -ForegroundColor Red
    Write-Host "    Installing it now..." -ForegroundColor Yellow
    wsl bash -c "curl https://ops.city/get.sh -sSfL | sh"
    Write-Host "[OK] OPS Installed." -ForegroundColor Green
    Write-Host ""
}

# 2. Convert line endings of the build script just in case
$scriptPath = "script/build-unikernel.sh"
if (Test-Path $scriptPath) {
    (Get-Content $scriptPath) -join "`n" | Set-Content $scriptPath -NoNewline
}

# 3. Run the build inside WSL
# We explicitly set the working directory to the current folder mapped in WSL
Write-Host "[*] Building Kernel..." -ForegroundColor Yellow
wsl bash -c "cd '$PWD' && bash script/build-unikernel.sh"

if ($LASTEXITCODE -eq 0) {
    Write-Host ""
    Write-Host "[OK] Kernel Image Created Successfully!" -ForegroundColor Green
    Write-Host "    To run it, type: wsl ops run aethex-kernel-v1" -ForegroundColor Gray
} else {
    Write-Host ""
    Write-Host "[!] Build Failed." -ForegroundColor Red
}
