#!/bin/bash
set -e

pkg=lsd2

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/${pkg}/examples/unrooted_tree_without_outgroups/* "${AUTOPKGTEST_TMP}"

cd "${AUTOPKGTEST_TMP}"

echo "== running lsd2 on unrooted-tree example, estimating the root =="
lsd2 -i Dengue.phy_phyml_tree.txt -d Dengue.date -r a -s 1000

for suffix in result result.date.nexus result.nwk; do
    test -s "Dengue.phy_phyml_tree.txt.$suffix"
done

echo "-- result file --"
cat Dengue.phy_phyml_tree.txt.result

grep -q 'rate' Dengue.phy_phyml_tree.txt.result
grep -q 'tMRCA' Dengue.phy_phyml_tree.txt.result

echo "OK: lsd2 produced dating results"


cp -a /usr/share/doc/${pkg}/examples/test.cpp .

echo "== compiling and linking against the shared library =="
g++ test.cpp -o test_shared -llsd2
./test_shared -h | grep -m1 -oE 'v\.[0-9]+(\.[0-9]+)+'

echo "== compiling and linking against the static library =="
g++ test.cpp -o test_static -Wl,-Bstatic -llsd2 -Wl,-Bdynamic
./test_static -h | grep -m1 -oE 'v\.[0-9]+(\.[0-9]+)+'

echo "OK: liblsd2-dev headers/shared/static libs all usable"
