#!/usr/bin/env bash

# pkexec, with a stated reason on the password prompt.
#
#   panama-sudo --reason "Installing gamemode hooks" -- dnf install gamemode
#
# The reason travels to the shell over Quickshell IPC before pkexec runs, and
# Panama's prompt shows it clearly labeled beside polkitd's own action message
# -- beside, never instead of: anything can claim any reason, so the real
# action text stays the trust anchor. Meant for agents and scripts, so the
# person at the keyboard learns WHY before typing their password.
#
# Degrades to plain pkexec: no --reason, no running shell, or no qs on PATH
# all behave identically to calling pkexec yourself.

set -euo pipefail

reason=""
if [[ "${1:-}" == "--reason" ]]; then
  reason="${2:?panama-sudo: --reason needs a value}"
  shift 2
fi
[[ "${1:-}" == "--" ]] && shift

if (( $# == 0 )); then
  echo 'usage: panama-sudo [--reason "why"] -- command [args...]' >&2
  exit 2
fi

if [[ -n "$reason" ]] && command -v qs >/dev/null 2>&1; then
  qs ipc call polkit reason "$reason" >/dev/null 2>&1 || true
fi

exec pkexec "$@"
