#!/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"
}

kcm_files=(
    kcms/mobileshell/kcm_mobileshell.json
    kcms/navigation/kcm_navigation.json
    kcms/waydroidintegration/kcm_waydroidintegration.json
)

for metadata_file in "${kcm_files[@]}"; do
    require_line "$metadata_file" '"Website": "https://invent\.kde\.org/marcoa/shift-shell"' \
        "$metadata_file must point at the Shift repository"
done

mapfile -t all_kcm_metadata < <(find kcms -name 'kcm_*.json' | sort)
[[ "${#all_kcm_metadata[@]}" -eq 5 ]] || fail "unexpected KCM metadata file count"

for metadata_file in "${all_kcm_metadata[@]}"; do
    reject_line "$metadata_file" 'plasma-mobile\.org|bugs\.kde\.org|"Website": "https://kde\.org"' \
        "$metadata_file must not expose upstream Plasma Mobile project URLs"
done

require_line kded/autodetectapn/kded_plasma_mobile_autodetectapn.json '"Description": "Autodetect cellular APNs in SHIFT"' \
    "APN autodetect KDED description must be branded SHIFT"
require_line kded/autodetectapn/kded_plasma_mobile_autodetectapn.json '"Name": "SHIFT Autodetect APNs"' \
    "APN autodetect KDED name must be branded SHIFT"
require_line kded/start/kded_plasma_mobile_start.json '"Description": "Run initial tasks for SHIFT during session startup"' \
    "start KDED description must be branded SHIFT"
require_line kded/start/kded_plasma_mobile_start.json '"Name": "SHIFT Start"' \
    "start KDED name must be branded SHIFT"

for metadata_file in kded/autodetectapn/kded_plasma_mobile_autodetectapn.json kded/start/kded_plasma_mobile_start.json; do
    require_line "$metadata_file" '"X-KDE-Library": "plasma-mobile"' \
        "$metadata_file must keep the existing KDED library name until a namespace migration is planned"
    reject_line "$metadata_file" '"(Name|Description)\[[^]]+\]"' \
        "$metadata_file must not keep translated names or descriptions that can override the base metadata"
    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 kded/autodetectapn/CMakeLists.txt 'DESCRIPTION "SHIFT Autodetect APNs"' \
    "APN autodetect logging category description must be branded SHIFT"
require_line CMakeLists.txt 'PURPOSE "Allows SHIFT to configure Waydroid"' \
    "KF6 Auth feature summary must be branded SHIFT"

require_line kded/autodetectapn/CMakeLists.txt 'kcoreaddons_add_plugin\(kded_plasma_mobile_autodetect_apn' \
    "APN autodetect KDED plugin target must stay stable until a namespace migration is planned"
require_line kded/start/CMakeLists.txt 'kcoreaddons_add_plugin\(kded_plasma_mobile_start' \
    "start KDED plugin target must stay stable until a namespace migration is planned"

printf 'shift-settings-daemon-metadata-ok\n'