{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://schemas.idkyet.fml128.ch/v1/installer.schema.json",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "schema_version": {
      "type": "integer",
      "const": 1
    },
    "target_selection": {
      "$ref": "#/definitions/target_selection"
    },
    "partition_table": {
      "type": "string",
      "enum": ["gpt"],
      "default": "gpt"
    },
    "partitions": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/partition"
      },
      "minItems": 1
    },
    "encryption": {
      "$ref": "#/definitions/encryption"
    },
    "hooks": {
      "$ref": "#/definitions/hooks"
    },
    "user_enrollment": {
      "$ref": "#/definitions/user_enrollment"
    }
  },
  "required": ["schema_version", "target_selection", "partitions"],
  "definitions": {
    "string_list": {
      "type": "array",
      "items": {
        "type": "string",
        "minLength": 1
      },
      "uniqueItems": true
    },
    "target_selection": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "strategy": {
          "type": "string",
          "enum": [
            "largest_disk",
            "by_model",
            "by_serial",
            "by_size_min_gib",
            "prompt"
          ]
        },
        "model": { "type": "string", "minLength": 1 },
        "serial": { "type": "string", "minLength": 1 },
        "size_min_gib": { "type": "integer", "minimum": 1 }
      },
      "required": ["strategy"]
    },
    "partition": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "label": { "type": "string", "minLength": 1, "maxLength": 36 },
        "partlabel": { "type": "string", "minLength": 1, "maxLength": 36 },
        "fslabel": { "type": "string", "minLength": 1, "maxLength": 36 },
        "role": {
          "type": "string",
          "enum": ["esp", "boot", "root", "home", "swap", "data"]
        },
        "size": {
          "type": "string",
          "pattern": "^([1-9][0-9]*(MiB|GiB)|rest)$"
        },
        "filesystem": {
          "type": "string",
          "enum": ["vfat", "ext4", "btrfs", "xfs", "swap"]
        },
        "mount": { "type": "string", "pattern": "^/.*" },
        "options": { "$ref": "#/definitions/string_list" },
        "flags": { "$ref": "#/definitions/string_list" },
        "subvolumes": { "$ref": "#/definitions/string_list" }
      },
      "required": ["label", "role", "size", "filesystem"]
    },
    "encryption": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "root": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "method": { "type": "string", "enum": ["luks2"] },
            "prompt": { "type": "boolean" }
          },
          "required": ["method"]
        }
      }
    },
    "hooks": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "pre_install": { "$ref": "#/definitions/hook_list" },
        "post_install": { "$ref": "#/definitions/hook_list" },
        "pre_update": { "$ref": "#/definitions/hook_list" },
        "post_update": { "$ref": "#/definitions/hook_list" }
      }
    },
    "user_enrollment": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "enabled": { "type": "boolean", "default": false },
        "default_auth": { "type": "string", "enum": ["fido2", "password"], "default": "fido2" },
        "shell": { "type": "string", "pattern": "^/.*" },
        "groups": { "$ref": "#/definitions/string_list" },
        "recovery_key": { "type": "boolean", "default": true },
        "fido2_with_client_pin": { "type": "boolean", "default": true }
      }
    },
    "hook_list": {
      "type": "array",
      "items": { "$ref": "#/definitions/hook" }
    },
    "hook": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "run": { "type": "string", "minLength": 1 },
        "copy": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "from": { "type": "string", "pattern": "^/.*" },
            "to": { "type": "string", "pattern": "^/.*" }
          },
          "required": ["from", "to"]
        },
        "write_file": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "path": { "type": "string", "pattern": "^/.*" },
            "content": { "type": "string" },
            "mode": { "type": "string", "pattern": "^0[0-7]{3}$" }
          },
          "required": ["path", "content"]
        }
      },
      "oneOf": [
        { "required": ["run"] },
        { "required": ["copy"] },
        { "required": ["write_file"] }
      ]
    }
  }
}
