#!/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 quicksettings -type f \( -path '*/metadata.json' -o -path '*/package/metadata.json' \) | sort)
[[ "${#metadata_files[@]}" -gt 0 ]] || fail "no quick setting metadata files found"

for metadata_file in "${metadata_files[@]}"; do
    require_line "$metadata_file" '"Id": "org\.kde\.plasma\.quicksetting\.' \
        "$metadata_file must keep its existing quick setting package id until a namespace migration is planned"
    require_line "$metadata_file" '"Website": "https://invent\.kde\.org/marcoa/shift-shell"' \
        "$metadata_file must point at the Shift repository"

    reject_line "$metadata_file" '"Description\[[^]]+\]"' \
        "$metadata_file must not keep translated descriptions that can override the base description"
    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

reject_line quicksettings/caffeine/contents/ui/main.qml 'Plasma Mobile has enabled system-wide inhibition' \
    "Caffeine inhibition reason must use SHIFT branding"

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