#!/bin/sh

COMMAND="/usr/bin/gpg --status-fd=2 --no-verbose --quiet  --batch  --output - --verify"

RES=`$COMMAND "$1" "$2" 2>&1`
GPGOK="$?"
# Did gpg work well?

RES=`echo -n "$RES" | grep "\[GNUPG:\] NO_PUBKEY"`
NO_PUB="$?"
# was the possible error because of a missing pubkey?

# if gpg worked fine or if the error was _not_ a missing pubkey error
# forward the error to mutt
if [ "$GPGOK" -eq "0" ] || [ "$NO_PUB" -ne "0" ]; then
	exec $COMMAND "$1" "$2"
fi

# otherwise try again, with the proper keyring included.

KEYID=`echo $RES | tr -s " " | cut -d " " -f 3`
KEYRING=~/.mutt/debian-keyring.splitted/keyring.$KEYID

if [ -e "$KEYRING" ]; then
	exec $COMMAND --keyring $KEYRING "$1" "$2"
else
	exec $COMMAND "$1" "$2"
fi

