#!/bin/sh

set -e

GNUPGHOME=$(mktemp -d)
export GNUPGHOME
gpg_args() {
	gpg --batch --quiet --pinentry-mode=loopback --passphrase '' --with-colons "$@"
}

# Generate a minimal signing key:
gpg_args --quick-gen-key 'Test key for gpgv-win32 <test-key@example.com>'

gpg_args -o "$GNUPGHOME/key.gpg" --export test-key@example.com

# Sign this very script
rm -f "${0}.gpg"
gpg_args --output "${0}.gpg" --detach-sign "${0}"

# Verify using gpgv
gpgv --quiet --status-fd 3 3> native.status --keyring "$GNUPGHOME/key.gpg" "${0}.gpg" "${0}"

WINE=/usr/lib/wine/wine
export WINESERVER=/usr/lib/wine/wineserver32

# Verify using gpgv.exe (using --status-fd 1 because i don't know how
# to pass a non-standard file descriptor into wine)
"$WINE" /usr/share/win32/gpgv.exe --quiet --status-fd 1 > win32.status --keyring "Z://${GNUPGHOME}/key.gpg" "${0}.gpg" "${0}" 

# convert to unix newlines if necessary:
sed -i 's/\r$//' win32.status

diff -u native.status win32.status

head -v win32.status

rm -rf "$GNUPGHOME"
