{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://abdess.github.io/retrobios/api/v1/schemas/romfolders.schema.json",
  "title": "RetroBIOS rom folders of one setup",
  "description": "What a frontend's own source says about its game folders, read at pinned revisions (platforms/romfolders/<setup>.yml, written by python -m scripts.scraper.romfolders). Names are the OS's own: scripts/romfolders.py resolves them to repository systems and profiles.",
  "type": "object",
  "additionalProperties": false,
  "required": ["setup", "name", "platform", "sources", "roms_root", "case_sensitive", "folders"],
  "$defs": {
    "ref": {
      "type": "string",
      "description": "'<source key> <path>:<line>[-<line>]', several joined by '; '. The key names an entry of sources.",
      "pattern": "^[a-z0-9][a-z0-9-]* \\S+(?:; [a-z0-9][a-z0-9-]* \\S+)*$"
    },
    "named_rule": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source_ref"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "pattern": { "type": "string", "minLength": 1 },
        "reason": { "type": "string", "minLength": 1 },
        "source_ref": { "$ref": "#/$defs/ref" }
      },
      "oneOf": [{ "required": ["name"] }, { "required": ["pattern"] }]
    },
    "board_default": {
      "type": "object",
      "additionalProperties": false,
      "required": ["core", "source_ref"],
      "properties": {
        "core": { "type": "string", "minLength": 1 },
        "emulator": { "type": "string", "minLength": 1 },
        "source_ref": { "$ref": "#/$defs/ref" }
      }
    }
  },
  "properties": {
    "setup": { "type": "string", "pattern": "^[a-z0-9][a-z0-9-]*$" },
    "name": { "type": "string", "minLength": 1 },
    "platform": {
      "type": "string",
      "description": "The install manifest (install/<platform>.json) the installer uses on this setup, as install.py's OS rules choose it. Empty for a setup the installer lays out with no manifest."
    },
    "sources": {
      "type": "object",
      "minProperties": 1,
      "additionalProperties": {
        "type": "object",
        "additionalProperties": false,
        "required": ["repo", "commit"],
        "properties": {
          "repo": { "type": "string", "pattern": "^https://" },
          "commit": { "type": "string", "pattern": "^[0-9a-f]{40}$" },
          "tag": { "type": "string" },
          "note": { "type": "string" }
        }
      }
    },
    "roms_root": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "from_bios", "source_ref"],
      "properties": {
        "path": { "type": ["string", "null"], "minLength": 1, "description": "Null for a setup with no rom folder of its own." },
        "from_bios": {
          "type": ["string", "null"],
          "description": "The roms root relative to the BIOS folder the installer already knows. Null when no fixed relation exists."
        },
        "source_ref": { "$ref": "#/$defs/ref" }
      }
    },
    "case_sensitive": {
      "type": ["boolean", "null"],
      "description": "Whether the OS opens a folder only under the spelling it lists. Null when the source does not settle it (filesystem dependent)."
    },
    "case_ref": { "$ref": "#/$defs/ref" },
    "skip": {
      "type": "array",
      "description": "Folders under the roms root the scan never enters (the ELEC family's bios folder, the OS's own data).",
      "items": { "$ref": "#/$defs/named_rule" }
    },
    "hidden": {
      "type": "array",
      "description": "File and folder name patterns (fnmatch, case-insensitive) the OS hides from its game list.",
      "items": { "$ref": "#/$defs/named_rule" }
    },
    "media": {
      "type": "array",
      "description": "Folders inside a system folder that hold artwork, never games.",
      "items": { "$ref": "#/$defs/named_rule" }
    },
    "notes": { "type": "array", "items": { "type": "string", "minLength": 1 } },
    "folders": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["folder", "source_ref"],
        "properties": {
          "folder": { "type": "string", "minLength": 1, "description": "Path under the roms root, as the OS writes it." },
          "name": { "type": "string", "minLength": 1, "description": "The OS's own system id when it differs from the folder." },
          "aliases": { "type": "array", "items": { "type": "string", "minLength": 1 }, "uniqueItems": true },
          "extensions": {
            "type": "array",
            "description": "Lowercased, with the dot. Absent when the OS lists every file.",
            "items": { "type": "string", "pattern": "^\\.[^A-Z\\s]+$" },
            "uniqueItems": true
          },
          "files": {
            "type": "array",
            "description": "Whole file names the OS lists as games, as it writes them, in place of or beside extensions.",
            "items": { "type": "string", "minLength": 1 },
            "uniqueItems": true
          },
          "core": { "type": "string", "minLength": 1, "description": "The core or emulator the OS starts by default, in its own spelling." },
          "emulator": { "type": "string", "minLength": 1 },
          "source_ref": { "$ref": "#/$defs/ref" },
          "board_defaults": {
            "type": "object",
            "additionalProperties": { "$ref": "#/$defs/board_default" }
          },
          "offers": {
            "type": "array",
            "description": "Every emulator and core the frontend can start for this folder, the default included, in the list's order. Absent when the default is the only one.",
            "items": {
              "type": "object",
              "additionalProperties": false,
              "required": ["core"],
              "properties": {
                "emulator": { "type": "string", "minLength": 1 },
                "core": { "type": "string", "minLength": 1 },
                "when": { "type": "string", "minLength": 1, "description": "The build condition the offer is written under (a device case)." },
                "source_ref": { "$ref": "#/$defs/ref" }
              }
            }
          },
          "arcade": { "type": "boolean" },
          "searched_by": {
            "type": "array",
            "description": "The emulators, or the libretro core with its runner, whose commands for this folder pass a ROM search path, and the folders below the roms root it names.",
            "items": {
              "type": "object",
              "additionalProperties": false,
              "required": ["emulator", "reads", "source_ref"],
              "properties": {
                "emulator": { "type": "string", "minLength": 1 },
                "core": { "type": "string", "minLength": 1, "description": "The libretro core RetroArch starts, handed the search path in its content command line." },
                "reads": { "type": "array", "minItems": 1, "items": { "type": "string", "minLength": 1 }, "uniqueItems": true },
                "machines": { "type": "array", "description": "The machines the commands start, as the emulator names them.", "items": { "type": "string", "minLength": 1 }, "uniqueItems": true },
                "any_machine": { "type": "boolean", "description": "A command starts the game itself as the machine (ES-DE's %BASENAME%)." },
                "source_ref": { "$ref": "#/$defs/ref" }
              }
            }
          },
          "note": { "type": "string", "minLength": 1 }
        }
      }
    }
  }
}
