#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
PHP_BIN="${PHP_BIN:-php}"
NODE_BIN="${NODE_BIN:-node}"
"$PHP_BIN" -r '
if (PHP_MAJOR_VERSION !== 8 || PHP_MINOR_VERSION !== 5) {
    fwrite(STDERR, "Se requiere PHP 8.5 CLI.\n"); exit(1);
}
foreach (["ctype", "curl", "dom", "fileinfo", "filter", "mbstring", "openssl", "pdo_mysql", "session", "tokenizer", "xml"] as $extension) {
    if (!extension_loaded($extension)) {
        fwrite(STDERR, "Falta la extensión $extension.\n"); exit(1);
    }
}
require "vendor/autoload.php";
if (!is_writable("storage") || !is_writable("bootstrap/cache")) {
    fwrite(STDERR, "storage y bootstrap/cache deben permitir escritura.\n"); exit(1);
}
'
command -v flock >/dev/null
"$NODE_BIN" -e 'if (![22,24,26].includes(Number(process.versions.node.split(".")[0]))) process.exit(1)'
"$NODE_BIN" browser/point.mjs --check
printf '\nComprobación local del servidor completada.\n'
