#!/bin/sh -e

pkg=sshash

export LC_ALL=C.UTF-8
if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  # shellcheck disable=SC2064
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

cp -a /usr/share/doc/libsshash-dev/examples/* "${AUTOPKGTEST_TMP}"
cd "${AUTOPKGTEST_TMP}"

k=31
m=19
bases=20000

# SSHash expects one DNA sequence per line, each preceded by a header, and a
# set of k-mers without duplicates.  A pseudo-random sequence of 20 kb
# generated with a fixed seed satisfies this: the expected number of
# duplicated 31-mers is negligible (about 20000^2 / 2 / 4^31).
awk -v n="${bases}" 'BEGIN {
    srand(42);
    printf ">synthetic_sequence\n";
    for (i = 0; i < n; ++i) printf "%s", substr("ACGT", int(rand() * 4) + 1, 1);
    printf "\n";
}' > input.fa

num_kmers=$((bases - k + 1))

echo "=== sshash build ==="
sshash build -i input.fa -k "${k}" -m "${m}" -o index.sshash --check --verbose

echo "=== sshash check ==="
sshash check -i index.sshash 2>&1 | tee check.log
if grep -q -e 'not found' -e 'ERROR' -e 'expected id' check.log ; then
    echo "dictionary check reported errors" >&2
    exit 1
fi

echo "=== sshash query ==="
sshash query -i index.sshash -q input.fa 2>&1 | tee query.log

# All k-mers of the indexed sequence must be found, and none of them may be
# reported as missing or invalid.
grep -q "^num_kmers = ${num_kmers}$" query.log
grep -q "^num_positive_kmers = ${num_kmers} " query.log
grep -q "^num_negative_kmers = 0 " query.log
grep -q "^num_invalid_kmers = 0 " query.log

echo "=== sshash usage ==="
sshash > usage.log 2>&1
grep -q "Available tools" usage.log

# The tool must use the shared library rather than a private copy of it.
echo "=== sshash links against libsshash0 ==="
ldd "$(command -v sshash)" | grep -q 'libsshash\.so\.0 =>'

# Build a consumer of the development package with pkg-config and check that
# it links against the shared library, then exercise build, access and lookup.
echo "=== libsshash-dev consumer ==="
cat > consumer.cpp <<'EOF'
#include <sshash/dictionary_types.hpp>
#include <sshash/util.hpp>

#include <iostream>
#include <string>

int main(int argc, char** argv) {
    if (argc != 2) {
        std::cerr << "usage: " << argv[0] << " sequences.fa\n";
        return 1;
    }

    sshash::build_configuration config;
    config.k = 31;
    config.m = 19;
    config.verbose = false;

    sshash::dictionary_type dict;
    dict.build(argv[1], config);

    std::string kmer(dict.k(), 0);
    for (uint64_t id = 0; id != dict.num_kmers(); ++id) {
        dict.access(id, kmer.data());
        sshash::lookup_result result = dict.lookup(kmer.c_str());
        if (result.kmer_id != id) {
            std::cerr << "k-mer " << id << " ('" << kmer << "') not found\n";
            return 1;
        }
    }

    std::cout << "looked up " << dict.num_kmers() << " k-mers" << std::endl;
    return 0;
}
EOF

c++ -O2 -Wall -Wextra consumer.cpp -o consumer \
    $(pkg-config --cflags --libs sshash)
ldd ./consumer | grep -q 'libsshash\.so\.0 =>'
./consumer input.fa

# Build and run the unit test that ships in the examples directory of
# libsshash-dev.  The sources refer to the installed headers by name, so they
# have to be built from within the copied examples tree; the subshell keeps
# the working directory for the consumer below.
cd test
for test in test_*.cpp; do
    name="${test%.cpp}"
    echo "=== $name ==="
    c++ -O2 -Wall -Wextra -I.. $(pkg-config --cflags sshash) \
            "$test" -o "$name" $(pkg-config --libs sshash)
    "./$name" 31
done
