<?php
header('Content-Type: application/json');

// --- Get all build logs ---
$logs = glob('/var/www/nix.roulaise.net/status/qt-temp/qtw*/temp/build.log');
$logs = array_unique($logs);

$output = "";
$step = "Unknown";

// --- Loop through each log ---
foreach ($logs as $file) {
    // Get last line to reduce output
    $line = trim(@shell_exec('tail -n 1 ' . escapeshellarg($file)));
    $output .= "==> $file <==\n$line\n\n";

    // Extract current step [123/456]
    if (preg_match('/\[\d+\/\d+\]/', $line, $m)) {
        $step = $m[0];
    }
}

echo json_encode([
    'output' => $output,
    'step'   => $step
]);
