#!/usr/bin/env bash
# h-qml-run.sh — VALIDA el hito H-QML (ADR 0011, Capa 1) contra el FHS hidratado desde el repo firmado.
# Compila scripts/kde/h-qml-test.cpp (QQmlApplicationEngine + QtQuick Window/Text) con zig-cc/musl
# contra work/kde-rootfs y lo corre con QT_QPA_PLATFORM=offscreen vía el loader musl del .dev-fs.
# Pensado para correr EN EL WORKER (host glibc + rootfs musl). Prerequisito: hydrate-fhs.sh ya hidrató
# el cierre runtime de qt6-qtdeclarative en work/kde-rootfs.
#
# Éxito = "H-QML: QML instanciado" + "event loop terminó rc=0".
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"; cd "$ROOT"
R="${RFS:-$ROOT/work/kde-rootfs}"
ZIG="${ZIG:-$ROOT/.dev-fs/tools/zig-x86_64-linux-0.13.0/zig}"
LOADER="${LOADER:-$ROOT/.dev-fs/alpine/lib/ld-musl-x86_64.so.1}"
A="$ROOT/.dev-fs/alpine"
BIN="${BIN:-/tmp/h-qml}"
I="$R/usr/include/qt6"

echo "==> compilando h-qml-test.cpp (zig-cc/musl) contra $R"
"$ZIG" c++ -std=c++17 -target x86_64-linux-musl -mcpu=baseline -fPIC \
  scripts/kde/h-qml-test.cpp -o "$BIN" \
  -I"$I" -I"$I/QtCore" -I"$I/QtGui" -I"$I/QtQml" \
  -L"$R/usr/lib" -lQt6Qml -lQt6Gui -lQt6Core

echo "==> corriendo H-QML (offscreen)"
LIBS="$R/usr/lib:$R/usr/lib/qt6:$A/usr/lib:$A/lib"
QT_QPA_PLATFORM=offscreen QT_QUICK_BACKEND=software \
QT_PLUGIN_PATH="$R/usr/lib/qt6/plugins" \
QML_IMPORT_PATH="$R/usr/lib/qt6/qml" QML2_IMPORT_PATH="$R/usr/lib/qt6/qml" \
  "$LOADER" --library-path "$LIBS" "$BIN"
