#!/bin/bash

set -e

ARCH=$1

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

# get wine initialized, which normally spews logs to stderr
# (autopkgtest doesn't want stderr)
"$WINE" hostname 2>&1

export PKG_CONFIG_PATH=/usr/x86_64-w64-mingw32/lib/pkgconfig

# see https://dev.gnupg.org/T4624 for why this is needed
extralibs=(-lws2_32)

"$GCC" -pedantic -Wall -Werror -o test-run.exe \
       -static \
       -Wl,--no-as-needed \
       debian/tests/simple-build.c \
       $(pkg-config --static --cflags --libs libassuan) "${extralibs[@]}"

"$WINE" ./test-run.exe
rm -f test-run.exe
