#!/bin/sh
# (C) 2012 Canonical Ltd.
# (C) 2018-2019 Simon McVittie
# Authors: Martin Pitt, Simon McVittie
# SPDX-License-Identifier: LGPL-2.0-or-later

set -eux

if [ -n "${DEB_HOST_GNU_TYPE:-}" ]; then
    CROSS_COMPILE="$DEB_HOST_GNU_TYPE-"
else
    CROSS_COMPILE=
fi

mode=dynamic

getopt_temp="$(getopt -o '' --long 'static' -n debian/tests/build -- "$@")"
eval set -- "$getopt_temp"

while true; do
    case "$1" in
        (--static)
            mode=static
            shift
            continue
            ;;

        (--)
            shift
            break
            ;;

        (*)
            echo "getopt: Internal error" >&2
            exit 2
    esac
done

cd "$AUTOPKGTEST_TMP"
cat <<EOF > gnome-rr.c
#define GNOME_DESKTOP_USE_UNSTABLE_API
#include <gnome-rr/gnome-rr.h>

int main(void)
{
    g_assert_cmpuint (GNOME_RR_TYPE_SCREEN, !=, G_TYPE_INVALID);
    return 0;
}
EOF

cflags=
pcflags=

case "$mode" in
    (static)
        cflags=-static
        pcflags=--static
        ;;
esac

# shellcheck disable=SC2046
"${CROSS_COMPILE}gcc" $cflags -o "$mode" gnome-rr.c $("${CROSS_COMPILE}pkgconf" $pcflags --cflags --libs gnome-rr-4 glib-2.0)
echo "build ($mode): OK"
[ -x "$mode" ]
"./$mode"
echo "run ($mode): OK"
