<?php
// Returns a JSON array of every .mp3 in /music/, naturally sorted.
// Drop a new file into music/ and it appears on the site automatically.

header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Access-Control-Allow-Origin: *');

$dir = __DIR__ . DIRECTORY_SEPARATOR . 'music';
$files = [];

if (is_dir($dir)) {
    $entries = @scandir($dir);
    if ($entries !== false) {
        foreach ($entries as $entry) {
            if ($entry === '.' || $entry === '..') continue;
            if (preg_match('/\.mp3$/i', $entry)) {
                $files[] = $entry;
            }
        }
        sort($files, SORT_NATURAL | SORT_FLAG_CASE);
    }
}

echo json_encode($files, JSON_UNESCAPED_UNICODE);
