#!/bin/sh -e

if [ -z "$AUTOPKGTEST_TMP" ]; then
  AUTOPKGTEST_TMP=$(mktemp -d)  
  trap "rm -rf $AUTOPKGTEST_TMP" 0
fi
cd $AUTOPKGTEST_TMP

CC="${DEB_HOST_GNU_TYPE:+$DEB_HOST_GNU_TYPE-}${CC:-gcc}"
PKG_CONFIG="${DEB_HOST_GNU_TYPE:+$DEB_HOST_GNU_TYPE-}pkg-config"

cat > test.c << 'END'
#include <stdlib.h>
#include <stdio.h>
#include <tls.h>

int main (int argc, char *argv[]) {
	int rc;

	struct tls_config *config = tls_config_new();
	if (!config) {
		perror("tls_config_new()");
		exit(1);
	}

	rc = tls_config_set_protocols(config, TLS_PROTOCOLS_ALL);
	if (rc != 0) {
		perror(tls_config_error(config));
		exit(1);
	}

	struct tls *tls = tls_client();
	if (!tls) {
		perror("tls_client()");
		exit(1);
	}

	rc = tls_configure(tls, config);
	if (rc != 0) {
		perror(tls_error(tls));
		exit(1);
	}

	exit(0);
}
END

$CC -o test test.c $("$PKG_CONFIG" --cflags --libs libtls)
echo "build: OK"

./test
echo "run: OK"

