#!/bin/sh
# compile a small D program against the dlang-openssl bindings and verify it
# actually links and runs against the system libssl/libcrypto, not just that
# the headers parse.

set -e

INCDIR=/usr/include/d/common
WORKDIR="${AUTOPKGTEST_TMP:-$(mktemp -d)}"

cat > "$WORKDIR/test.d" <<'EOF'
import deimos.openssl.ssl;
import deimos.openssl.err;
import core.stdc.stdio;

void main()
{
    SSL_library_init();
    SSL_load_error_strings();

    const(SSL_METHOD)* method = TLS_method();
    assert(method !is null, "TLS_method() returned null");

    SSL_CTX* ctx = SSL_CTX_new(cast(SSL_METHOD*) method);
    assert(ctx !is null, "SSL_CTX_new() failed");

    SSL_CTX_free(ctx);

    printf("OK: dlang-openssl bindings compile, link and run against libssl\n");
}
EOF

gdc -I"$INCDIR" -fversion=DeimosOpenSSL_3_0 -o "$WORKDIR/test" "$WORKDIR/test.d" -lssl -lcrypto
"$WORKDIR/test"
