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

metadata_files=(
    initialstart/modules/cellular/package/metadata.json
    initialstart/modules/deviceprofile/metadata.json
    initialstart/modules/experienceprofile/metadata.json
    initialstart/modules/finished/metadata.json
    initialstart/modules/prepare/package/metadata.json
    initialstart/modules/systemnavigation/metadata.json
    initialstart/modules/time/package/metadata.json
    initialstart/modules/wifi/package/metadata.json
)

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"
}

for metadata_file in "${metadata_files[@]}"; do
    [[ -f "$metadata_file" ]] || fail "missing initial setup metadata file: $metadata_file"

    require_line "$metadata_file" '"Description": ".*initial setup module for SHIFT"' \
        "$metadata_file must use a SHIFT-facing base description"
    require_line "$metadata_file" '"Website": "https://invent\.kde\.org/marcoa/shift-shell"' \
        "$metadata_file must point at the Shift repository"
    require_line "$metadata_file" '"Id": "org\.kde\.plasma\.mobileinitialstart\.' \
        "$metadata_file must keep its existing mobileinitialstart package id until a namespace migration is planned"

    reject_line "$metadata_file" '"Description\[[^]]+\]"' \
        "$metadata_file must not keep translated descriptions that can override the SHIFT base description"
    reject_line "$metadata_file" 'Plasma Mobile|plasma-mobile\.org|bugs\.kde\.org' \
        "$metadata_file must not expose upstream Plasma Mobile product metadata"
done

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