#!/bin/sh

set -e
exec 2>&1
set -u

: "${AUTOPKGTEST_TMP:="${ADTTMP}"}"

if [ -z "${HOME-}" ] || ! [ -w "${HOME}" ]; then
    export HOME="${AUTOPKGTEST_TMP}"
fi

cd "$AUTOPKGTEST_TMP"

echo "1..2"

cat > connect.c <<'EOF'
#include <stdio.h>

#include <dbus/dbus-glib.h>

int main (void)
{
    GError *error = NULL;
    DBusGConnection *connection;

    connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);

    if (connection == NULL) {
        g_printerr("%s #%d: %s",
                   g_quark_to_string(error->domain),
                   error->code,
                   error->message);
        g_clear_error(&error);
        return 1;
    }

    dbus_g_connection_unref(connection);
    return 0;
}
EOF

# Deliberately word-splitting, that's how pkg-config works:
# shellcheck disable=SC2046
gcc -o connect connect.c $(pkg-config --cflags --libs dbus-glib-1)
test -x connect
dbus-run-session -- ./connect
echo "ok 1"

echo "# everything seems OK"
