#!/bin/sh
set -e

# Verify that a consumer (as vg does) can #include <sonLib/sonLib.h> and
# link against both the shared and the static library.  The
# neighbour-joining call exercises the quicktree symbols, which are
# resolved at run time from the executable when linking the shared
# library.
cat > smoke.c <<'EOF'
#include <sonLib/sonLib.h>
#include <assert.h>
#include <stdio.h>

int main(void) {
    stList *list = stList_construct();
    stList_append(list, (void *)1);
    stList_append(list, (void *)2);
    assert(stList_length(list) == 2);
    stList_destruct(list);

    double distances[4][4] = {{0.0, 9.0, 3.0, 0.1},
                              {9.0, 0.0, 6.0, 8.9},
                              {3.0, 6.0, 0.0, 3.0},
                              {0.1, 8.9, 3.0, 0.0}};
    stMatrix *dm = stMatrix_construct(4, 4);
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            *stMatrix_getCell(dm, i, j) = distances[i][j];
        }
    }
    stTree *tree = stPhylogeny_neighborJoin(dm, NULL);
    assert(tree != NULL);
    stMatrix_destruct(dm);
    stPhylogenyInfo_destructOnTree(tree);
    stTree_destruct(tree);

    printf("sonlib smoke test OK\n");
    return 0;
}
EOF

MULTIARCH=$(dpkg-architecture -qDEB_HOST_MULTIARCH)

# Link against the shared library (the default for -lsonlib) and run.
cc -o smoke_dyn smoke.c -lsonlib -lquicktree -ltokyocabinet -lz -lbz2 -lpthread -lm -lstdc++
./smoke_dyn

# Link against the static library and run.
cc -o smoke_static smoke.c /usr/lib/$MULTIARCH/libsonlib.a -lquicktree -ltokyocabinet -lz -lbz2 -lpthread -lm -lstdc++
./smoke_static

echo "shared and static linking both OK"
