#!/bin/bash
set -euo pipefail

# Prevent starting real gpg-agent locally if it's redirected via split-gpg2,
# which only uses the default GnuPG home directory.
if [ -e /run/qubes-service/split-gpg2-client ]; then
    case "$@" in
        *"--homedir $HOME/.gnupg "*)
            exit 0
            ;;
    esac
fi

# Otherwise, launch gpg-agent.
gpgagent="$(gpgconf --list-components | awk -F: '/^gpg-agent:/{print $3}')"
exec "$gpgagent" "$@"
