#!/bin/sh

set -exu

libpath="/usr/lib/$(dpkg-architecture -q DEB_HOST_MULTIARCH)/wayfire/libfiredecor.so"
if [ ! -e "$libpath" ]; then
	echo "$libpath does not exist" >&2
	exit 1
fi

cd "$AUTOPKGTEST_TMP"

cat << END > test.c
#include <wayfire/plugin.hpp>
#include <errno.h>
#include <dlfcn.h>
#include <stdio.h>
#include <inttypes.h>
int main(int argc, char* argv[]) {
	void *handle = dlopen(argv[1], RTLD_NOW | RTLD_GLOBAL);
	if (handle == NULL) {
		perror("dlopen failed");
		return 1;
	}
	uint32_t (*version_func)() = (uint32_t (*)())dlsym(handle, "getWayfireVersion");
	if (version_func == NULL) {
		perror("dlsym failed");
		dlclose(handle);
		return 1;
	}
	int32_t plugin_abi_version = version_func();
	if (WAYFIRE_API_ABI_VERSION != plugin_abi_version) {
		fprintf(stderr, "API/ABI version mismatch:\n");
		fprintf(stderr, "wayfire is: %" PRIu32 "\n", WAYFIRE_API_ABI_VERSION);
		fprintf(stderr, "plugin is: %" PRIu32 "\n", plugin_abi_version);
		dlclose(handle);
		return 1;
	}
	fprintf(stderr, "wayfire is: %" PRIu32 "\n", WAYFIRE_API_ABI_VERSION);
	fprintf(stderr, "plugin is: %" PRIu32 "\n", plugin_abi_version);
	dlclose(handle);
	return 0;
}
END

g++ -Wall -ldl $(pkg-config --cflags pixman-1) -DWLR_USE_UNSTABLE test.c -o test
./test "$libpath"
