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

for session_file in bin/plasma-mobile.desktop.cmake bin/plasma-mobile-dev.desktop.cmake; do
    reject_line "$session_file" '^(Name|Comment)(\[[^]]+\])?=.*Plasma Mobile' \
        "$session_file must not expose Plasma Mobile branding in session chooser labels"
    reject_line "$session_file" '^Comment(\[[^]]+\])?=.*by KDE' \
        "$session_file must not present Shift as a KDE-branded session"
done

require_line bin/plasma-mobile.desktop.cmake '^Name=SHIFT$' \
    "main session must be branded SHIFT"
require_line bin/plasma-mobile-dev.desktop.cmake '^Name=SHIFT \(Development\)$' \
    "development session must be branded SHIFT"

[[ -f org.shift.mobile.metainfo.xml ]] || fail "missing Shift AppStream metadata"
[[ ! -e org.kde.plasma.mobileshell.metainfo.xml ]] || fail "upstream AppStream metadata file must not be installed from the repo root"

require_line CMakeLists.txt '^install\(FILES org\.shift\.mobile\.metainfo\.xml DESTINATION \$\{KDE_INSTALL_METAINFODIR\}\)$' \
    "CMake must install Shift AppStream metadata"
require_line CMakeLists.txt 'file\(REMOVE .*org\.kde\.plasma\.mobileshell\.metainfo\.xml' \
    "CMake must remove stale upstream Plasma Mobile AppStream metadata from existing prefixes"
reject_line CMakeLists.txt 'install\(FILES org\.kde\.plasma\.mobileshell\.metainfo\.xml' \
    "CMake must not install upstream Plasma Mobile AppStream metadata"

require_line org.shift.mobile.metainfo.xml '<id>org\.shift\.mobile</id>' \
    "AppStream id must be Shift-owned"
require_line org.shift.mobile.metainfo.xml '<name>SHIFT</name>' \
    "AppStream name must be SHIFT"
require_line org.shift.mobile.metainfo.xml 'https://invent\.kde\.org/marcoa/shift-shell' \
    "AppStream metadata must point at the Shift repository"
require_line org.shift.mobile.metainfo.xml '<binary>startplasmamobile</binary>' \
    "AppStream metadata must provide the installed startplasmamobile binary"
reject_line org.shift.mobile.metainfo.xml 'plasma-mobile\.org|bugs\.kde\.org|invent\.kde\.org/plasma/plasma-mobile|<project_group>KDE</project_group>' \
    "AppStream metadata must not point at upstream Plasma Mobile project resources"
reject_line org.shift.mobile.metainfo.xml '<binary>plasma\.mobileshell</binary>' \
    "AppStream metadata must not provide an unverifiable plasma.mobileshell binary"

require_line shell/metadata.json '"Id": "org\.kde\.plasma\.mobileshell"' \
    "runtime shell package id must stay stable until a namespace migration is planned"
require_line shell/metadata.json '"Name": "SHIFT"' \
    "shell metadata visible name must be SHIFT"
reject_line shell/metadata.json '"Name\[[^]]+\]":|"Description\[[^]]+\]":|plasma-mobile\.org' \
    "shell metadata must not retain translated upstream Plasma Mobile labels"

require_line plasma-mobile.service '^Description=SHIFT shell session for tty1$' \
    "systemd service description must be branded SHIFT"
require_line plasma-mobile.service '^Documentation=https://invent\.kde\.org/marcoa/shift-shell$' \
    "systemd service documentation must point at the Shift repository"
reject_line plasma-mobile.service '^Documentation=https://invent\.kde\.org/plasma-mobile$' \
    "systemd service documentation must not point at the upstream Plasma Mobile repository"

require_line initialstart/main.cpp 'QStringLiteral\("SHIFT Initial Setup"\)' \
    "initial setup KAbout display name must be branded SHIFT"
require_line initialstart/main.cpp 'setBugAddress\("https://invent\.kde\.org/marcoa/shift-shell/-/issues"\)' \
    "initial setup KAbout bug address must point at the Shift issue tracker"
reject_line initialstart/main.cpp 'QStringLiteral\("Initial Start"\)|© 2026 KDE Community' \
    "initial setup KAbout metadata must not retain the upstream-only display name or copyright string"

require_line initialstart/qml/LandingComponent.qml "wallpapers/SHIFT/contents/images/" \
    "initial setup landing page must use the Shift wallpaper"
require_line initialstart/modules/prepare/prepareutil.cpp 'QStringLiteral\("ShiftDark"\)' \
    "initial setup dark theme toggle must apply ShiftDark"
require_line initialstart/modules/prepare/prepareutil.cpp 'QStringLiteral\("ShiftLight"\)' \
    "initial setup dark theme toggle must apply ShiftLight"

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