#!/usr/bin/env bash

# No settings page may hand off to a GNOME panel that Panama itself owns.
#
# Panama absorbed Users, Sharing, Printers, Online Accounts and others one page
# at a time. Each time, the rows pointing at GNOME's equivalent stayed where
# they were -- so a settings app whose stated purpose is to make GNOME Settings
# unnecessary shipped four separate doors back to it, two of them inside a card
# headed "these areas remain owned by Fedora".
#
# Nothing failed. Every row worked exactly as written. The rows were simply no
# longer true, and no test could notice because none of them knew what Panama
# had come to own in the meantime.
#
# This reads SettingsRoutes for the pages that exist and the pages for the
# panels they hand off, and fails on any overlap. It is deliberately derived
# from both sides rather than from a hand-kept list, so absorbing the next page
# cannot leave a stale door behind.
#
# Read-only.

set -uo pipefail

repo_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
settings_dir="$repo_dir/config/dot/quickshell/modules/settings"
routes="$repo_dir/config/dot/quickshell/services/SettingsRoutes.qml"

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

[[ -r "$routes" ]] || fail "missing $routes"

# GNOME panel names that correspond to a Panama page. Only entries whose panel
# genuinely duplicates a Panama page belong here: "network" stays off it because
# Panama has no VPN or per-connection routing, and "online-accounts" is listed
# because Panama has an Online Accounts page -- but adding an account still has
# to go through GOA's own dialog, so that one exception is named explicitly.
declare -A OWNED=(
    [printers]=printers
    [online-accounts]=accounts
    [sharing]=sharing
    [users]=users
    [system\ users]=users
)

# Handoffs that are correct despite naming an owned panel, with the reason.
# Anything here must be justified, not merely tolerated.
declare -A ALLOWED=(
    ["OnlineAccountsPage.qml:online-accounts"]="adding an account requires GOA's own dialog"
    ["UsersPage.qml:system-users"]="fingerprint enrollment requires fprintd's guided capture flow, and GNOME's Users panel carries the only good dialog for it"
)

# The leaves: a tabless category is a page in its own right, and every tab is
# a page. A category that only groups tabs owns no controls itself, so it is
# not something GNOME could be handing a duplicate of.
pages="$( {
    grep -oE '\{ page: "[a-z-]+", label: "[^"]*", icon: "[^"]*", tabs: \[\] \}' "$routes"
    grep -oE '\{ page: "[a-z-]+", label: "[^"]*" \}' "$routes"
} | sed -E 's/\{ page: "([a-z-]+)".*/\1/' | sort -u)"
[[ -n "$pages" ]] || fail 'no pages could be read from SettingsRoutes, so this proves nothing'

has_page() {
    grep -qx "$1" <<<"$pages"
}

violations=0
checked=0

while IFS= read -r hit; do
    file="${hit%%:*}"
    base="$(basename "$file")"
    # openGnomePanel("a") or openGnomePanel("a", "b") -> "a" / "a b"
    panel="$(sed -E 's/.*openGnomePanel\("([^"]+)"(, *"([^"]+)")?\).*/\1 \3/' <<<"$hit" \
             | sed 's/ *$//')"
    [[ -n "$panel" ]] || continue
    checked=$((checked + 1))

    owner="${OWNED[$panel]:-}"
    [[ -n "$owner" ]] || continue
    has_page "$owner" || continue

    key="$base:${panel// /-}"
    if [[ -n "${ALLOWED[$key]:-}" ]]; then
        continue
    fi

    printf 'gnome handoff contract: %s hands "%s" to GNOME, but Panama owns the "%s" page\n' \
        "$base" "$panel" "$owner" >&2
    violations=$((violations + 1))
done < <(grep -rno --include='*.qml' -E 'openGnomePanel\("[^"]*"(, *"[^"]*")?\)' "$settings_dir")

(( checked > 0 )) || fail 'no handoffs were examined, so this proves nothing'

if (( violations > 0 )); then
    printf 'Each of these sends someone to GNOME for a page this app already has.\n' >&2
    exit 1
fi

printf 'gnome handoff contract: ok (%d handoffs checked against %d pages)\n' \
    "$checked" "$(wc -l <<<"$pages")"
