#!/bin/bash
# wallet-gate CMD...: run CMD only once the KDE wallet is unlocked. If it's locked,
# wallet-unlock asks for the password in a dialog on this display. With no wallet made yet,
# there's nothing to unlock: CMD runs (KWalletManager, protected too, is what makes one).
[ -e "$HOME/.local/share/kwalletd/kdewallet.kwl" ] || exec "$@"
if ! "$HOME/.local/bin/wallet-unlock" >/dev/null; then
    # Translations (lib/i18n.sh; the copy install_i18n puts here), else English. Only here: it
    # runs python3, and this wraps every launch.
    # shellcheck source=/dev/null
    source "$HOME/.local/share/frame-tweaks/i18n/i18n.sh" 2>/dev/null ||
        t() { local f=$1; shift; if [ $# -eq 0 ]; then printf '%s' "$f"; else printf -- "$f" "$@"; fi; }
    # Name the app, not a wrapper: past frame-outer-bus, and a Flatpak's app id after its options.
    app=$1; [ "${app##*/}" = frame-outer-bus ] && shift && app=${1:-$app}
    if [ "${app##*/}" = flatpak ] && [ "${2:-}" = run ]; then
        for a in "${@:3}"; do case $a in -*) ;; *) app=$a; break ;; esac; done
    fi
    notify-send -a Wallet "$(t '%s not started' "${app##*/}")" "$(t 'The wallet is still locked.')" 2>/dev/null
    exit 1
fi
exec "$@"
