#!/bin/sh

set -e

export GNUPGHOME=$(mktemp -d)

arch=$(dpkg --print-architecture)

case "$arch" in
     amd64)
          echo "I: setting up multiarch"
          dpkg --add-architecture i386
          apt-get update # FIXME you might want to try this up to some N times to avoid failures on temporary network issues
          ;;
     i386)
          : nothing, tests should just work
          ;;
     *)
          echo "I: skipping tests, only works on amd64 or i386"
          exit
          ;;
esac

DEBIAN_FRONTEND=noninteractive apt-get install -qy wine32 # FIXME ditto

echo 'allow-loopback-pinentry:0:1' | gpgconf --change-options gpg-agent

# Generate a minimal signing key:
gpg2 --batch --debug-quick-random --pinentry-mode loopback --passphrase '' --quick-gen-key   'Test key for gpgv-win32 <test-key@example.com>'

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

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

# Verify using gpgv
gpgv2 --keyring "$GNUPGHOME/key.gpg" "${0}.gpg" "${0}"

# Verify using gpgv.exe
wine32 /usr/share/win32/gpgv.exe --keyring "Z:\\\\${GNUPGHOME}/key.gpg" "Z:\\\\$(pwd)/${0}.gpg" "Z:\\\\$(pwd)/${0}"
