#!/bin/bash

options=()  # the buffer array for the parameters
eoo=0       # end of options reached
output=0    # do we try to write to file
target=()   # where do we try to write to

while [[ $1 ]]; do
    if ! ((eoo)); then
        case "$1" in
            --import)
                # ignore all the options and only collect file name(s) - if any
                shift
                exec qubes-gpg-import-key "$@"
                ;;
            # Keyserver options makes no sense for offline GPG VM, so it is
            # rejected by qubes-gpg-client and qubes-gpg-server. But since
            # it is forced by Torbirdy extension, simply ignore the option.
            --keyserver-options)
                shift 2
                ;;
            --yes)
                shift
                ;;
            -o)
                output=1
                target="$2"
                shift 2
                ;;
            --output)
                output=1
                target="$2"
                shift 2
                ;;
            --)
                eoo=1
                options+=("$1")
                shift
                ;;
            *)
                options+=("$1")
                shift
                ;;
        esac
    else
        options+=("$1")
        shift
    fi
done

. /etc/profile.d/qubes-gpg.sh
if ! ((output)); then
    exec qubes-gpg-client "${options[@]}"
else
    exec qubes-gpg-client "${options[@]}" > "$target"
fi
