<?php
// --- Configuration des serveurs ---
$servers = [
    'Fabric 1.19.2' => '/srv/fabric-1.19.2',
    'Forge 1.19.2'  => '/srv/forge-1.19.2',
];
// ----------------------------------

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');

function mod_name(string $filename): string {
    $name = preg_replace('/\.jar$/i', '', $filename);
    $name = preg_replace('/[-_](?:v?\d+[\.\d]*[\+\-\w]*).*$/i', '', $name);
    $name = str_replace(['-', '_'], ' ', $name);
    return ucwords(strtolower(trim($name)));
}

$result = [];
foreach ($servers as $label => $root) {
    $mods_dir = $root . '/mods';
    $entry = ['label' => $label, 'mods' => [], 'error' => null];

    if (!is_dir($mods_dir)) {
        $entry['error'] = 'Dossier introuvable : ' . $mods_dir;
    } else {
        $files = glob($mods_dir . '/*.jar');
        sort($files);
        foreach ($files as $f) {
            $filename = basename($f);
            $entry['mods'][] = [
                'file' => $filename,
                'name' => mod_name($filename),
            ];
        }
    }
    $result[] = $entry;
}

echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
