<?php
// index.php — list subfolders that contain a file named "list.me"
// Place this file in the parent directory that contains the subfolders.

$baseDir = __DIR__;                 // parent folder to scan
$requiredFile = 'list.me';          // file required to list a folder
$ignore = ['.', '..', '.git', '.svn', 'node_modules']; // ignored names

$items = [];

$dh = opendir($baseDir);
if ($dh === false) {
  http_response_code(500);
  die("Unable to read directory.");
}

while (($entry = readdir($dh)) !== false) {
  if (in_array($entry, $ignore, true)) continue;

  $fullPath = $baseDir . DIRECTORY_SEPARATOR . $entry;
  if (!is_dir($fullPath)) continue;

  // Only list if the required file exists in that subfolder
  $marker = $fullPath . DIRECTORY_SEPARATOR . $requiredFile;
  if (is_file($marker)) {
    $items[] = $entry;
  }
}
closedir($dh);

// Sort A→Z
natcasesort($items);
$items = array_values($items);

function h($s) { return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); }
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1" />
  <title>Folders with <?= h($requiredFile) ?></title>
  <style>
    body { font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif; margin: 24px; background:#0b1220; color:#e6eefc; }
    h1 { font-size: 20px; margin: 0 0 16px; }
    .note { opacity: .8; margin-bottom: 18px; }
    .grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
    a.card { display:block; padding:14px 14px; border-radius:16px; background: rgba(255,255,255,.06); text-decoration:none; color:inherit; border:1px solid rgba(255,255,255,.08); }
    a.card:hover { background: rgba(255,255,255,.10); }
    .name { font-weight: 700; }
    .sub { font-size: 12px; opacity: .8; margin-top: 6px; }
    .empty { padding: 14px; border-radius: 16px; background: rgba(255,255,255,.05); border:1px dashed rgba(255,255,255,.18); opacity:.9; }
  </style>
</head>
<body>
  <h1>Folders containing <code><?= h($requiredFile) ?></code></h1>
  <div class="note">
    Only subfolders that include <code><?= h($requiredFile) ?></code> are shown.
  </div>

  <?php if (count($items) === 0): ?>
    <div class="empty">No subfolders found containing <code><?= h($requiredFile) ?></code>.</div>
  <?php else: ?>
    <div class="grid">
      <?php foreach ($items as $folder): ?>
        <?php
          // Link to the folder itself (or you can link directly to list.me)
          $urlFolder = rawurlencode($folder) . '/';
          $urlFile   = rawurlencode($folder) . '/' . rawurlencode($requiredFile);
        ?>
        <a class="card" href="<?= h($urlFolder) ?>">
          <div class="name"><?= h($folder) ?></div>
          <div class="sub">Has <?= h($requiredFile) ?> → <span style="opacity:.85"><?= h($urlFile) ?></span></div>
        </a>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>
</body>
</html>