<?php
// --- Compute current build step for OG tags / initial display ---
$logs = glob('/var/www/nix.roulaise.net/status/qt-temp/qtw*/temp/build.log');
$logs = array_unique($logs);

$step = "Unknown";
foreach ($logs as $file) {
    $line = trim(@shell_exec('tail -n 1 ' . escapeshellarg($file)));
    if (preg_match('/\[\d+\/\d+\]/', $line, $m)) {
        $step = $m[0];
        break; // stop at first match
    }
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">

<!-- OpenGraph tags -->
<meta property="og:title" content="QT Build Status <?= htmlspecialchars($step) ?>" />
<meta property="og:description" content="Current build progress: <?= htmlspecialchars($step) ?>" />
<meta property="og:image" content="https://nix.roulaise.net/data/Guweiz1.jpeg" />
<meta property="og:url" content="https://nix.roulaise.net/status/QT.php" />
<meta property="og:type" content="website" />

<title>QT Build Status <?= htmlspecialchars($step) ?></title>
<link rel="stylesheet" href="/data/dracula-log.css">
</head>
<body>

<h1>Étape de construction : <span id="step"><?= htmlspecialchars($step) ?></span></h1>

<div class="log-box" id="log"></div>

<footer>
Tempo pas QT ! Je fait des tests !!
</footer>

<script>
async function update() {
    try {
        const res = await fetch("build-data.php");
        const data = await res.json();

        // Update DOM
        document.getElementById("step").textContent = data.step;
        document.getElementById("log").innerHTML = data.output.replace(/\n/g, "<br>");

        // Update page title (tab)
        document.title = `QT Build Status ${data.step}`;

        // Auto-scroll log
        const logBox = document.getElementById("log");
        logBox.scrollTop = logBox.scrollHeight;
    } catch(e) {
        console.error(e);
    }
}

// Refresh every 2 seconds
setInterval(update, 2000);
update();
</script>

</body>
</html>
