#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2026 Marco Allegretti <mightymarco4@gmail.com>
# SPDX-License-Identifier: EUPL-1.2

set -euo pipefail

repo_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$repo_dir"

fail() {
    printf '%s\n' "$1" >&2
    exit 1
}

require_line() {
    local file="$1"
    local pattern="$2"
    local message="$3"

    grep -Eq "$pattern" "$file" || fail "$message"
}

reject_line() {
    local file="$1"
    local pattern="$2"
    local message="$3"

    ! grep -Eq "$pattern" "$file" || fail "$message"
}

mapfile -t metadata_files < <(find containments -name metadata.json | sort)
[[ "${#metadata_files[@]}" -eq 4 ]] || fail "unexpected containment metadata file count"

for metadata_file in "${metadata_files[@]}"; do
    require_line "$metadata_file" '"Website": "https://invent\.kde\.org/marcoa/shift-shell"' \
        "$metadata_file must point at the Shift repository"
    reject_line "$metadata_file" 'Plasma Mobile|plasma-mobile\.org|bugs\.kde\.org|"Website": "https://kde\.org"' \
        "$metadata_file must not expose upstream Plasma Mobile product metadata"
done

require_line containments/panel/metadata.json '"Description": "Top panel for SHIFT"' \
    "panel containment description must be branded SHIFT"
require_line containments/taskpanel/metadata.json '"Description": "Navigation panel for SHIFT"' \
    "task panel containment description must be branded SHIFT"

reject_line containments/panel/metadata.json '"Description\[[^]]+\]"' \
    "panel containment must not keep translated descriptions that can override the base description"
reject_line containments/taskpanel/metadata.json '"Description\[[^]]+\]"' \
    "task panel containment must not keep translated descriptions that can override the base description"

require_line containments/panel/CMakeLists.txt 'plasma_add_applet\(org\.kde\.plasma\.mobile\.panel' \
    "panel containment package id must stay stable until a namespace migration is planned"
require_line containments/taskpanel/CMakeLists.txt 'plasma_add_applet\(org\.kde\.plasma\.mobile\.taskpanel' \
    "task panel containment package id must stay stable until a namespace migration is planned"
require_line containments/homescreens/folio/CMakeLists.txt 'plasma_add_applet\(org\.kde\.plasma\.mobile\.homescreen\.folio' \
    "Folio containment package id must stay stable until a namespace migration is planned"
require_line containments/homescreens/halcyon/CMakeLists.txt 'plasma_add_applet\(org\.kde\.plasma\.mobile\.homescreen\.halcyon' \
    "Halcyon containment package id must stay stable until a namespace migration is planned"

reject_line containments/homescreens/folio/README.md 'Plasma Mobile' \
    "Folio README must not describe the homescreen as Plasma Mobile"

printf 'shift-containment-metadata-ok\n'