#!/bin/bash

set -e

ARCH="$1"

if [ "$ARCH" = x86_64 ]; then
    WINE=/usr/lib/wine/wine64
elif [ "$ARCH" = i686 ]; then
    WINE=/usr/lib/wine/wine
else
    printf >&2 'Unknown Architecture: %s\n' "$ARCH"
    exit 1
fi
GCC="${ARCH}-w64-mingw32-gcc"

# just make sure that gpg-error.exe works

"$WINE" /usr/"${ARCH}"-w64-mingw32/bin/gpg-error.exe --version

# just compare the first 200 error messages (not all of them, because
# some errors have no available strings on windows):
for x in $(seq 0 200); do
    diff -u <(gpg-error $x) \
         <("$WINE" /usr/"${ARCH}"-w64-mingw32/bin/gpg-error.exe $x | sed 's/\r$//' )
done

export PKG_CONFIG_PATH="/usr/${ARCH}-w64-mingw32/lib/pkgconfig"

# see https://dev.gnupg.org/T4623 for why extra_libs is necessary as
# of gpg-error 1.36:
extra_libs=(-lws2_32)

# build a static windows binary around libgpg-error and run it:
"$GCC" -pedantic -Wall -Werror -static -o test-run.exe debian/tests/simple-build.c $(pkg-config --cflags --static --libs gpg-error) "${extra_libs[@]}"
"$WINE" ./test-run.exe
rm -f ./test-run.exe
