#!/bin/sh -ex
CURDIR=$(pwd)
if [ -d /usr/share/doc/iqtree3/examples ] ; then
  EXAMPLEDIR=/usr/share/doc/iqtree3/examples
else
  EXAMPLEFILE=$(find "$CURDIR" -name example.phy | head -n 1)
  EXAMPLEDIR=$(dirname "$EXAMPLEFILE")
fi

pkg=iqtree3
if [ "$AUTOPKGTEST_TMP" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  trap "rm -rf $AUTOPKGTEST_TMP" 0 INT QUIT ABRT PIPE TERM
fi
cd "$AUTOPKGTEST_TMP"

cp -a "$EXAMPLEDIR"/example.phy* "$EXAMPLEDIR"/example.nex* .
find . -name "*.gz" -exec gunzip \{\} \;

PATHTOEXE=$(which iqtree3)
if [ "$PATHTOEXE" = "" ] ; then
  PATHTOEXE=$(find "$CURDIR" -name iqtree3 -type f -executable)
fi
PATHTOEXEOMP=$(which iqtree3-omp)
if [ "$PATHTOEXEOMP" = "" ] ; then
  PATHTOEXE=$(find "$CURDIR" -name iqtree3-omp -type f -executable)
fi
#if [ "$PATHTOEXE" = "" ] ; then
#  PATHTOEXE=$(find "$CURDIR" -name iqtree3 -type l)
#fi
echo "Executing tests using $PATHTOEXE ..."
set -x
/bin/time "$PATHTOEXE" -s example.phy
/bin/time "$PATHTOEXE" -redo -s example.phy -pre myprefix
/bin/time "$PATHTOEXE" -redo -s example.phy -nni1
/bin/time "$PATHTOEXE" -redo -s example.phy -m TEST
/bin/time "$PATHTOEXE" -redo -s example.phy -m TIM+I+G
/bin/time "$PATHTOEXE" -redo -s example.phy -m TESTONLY
/bin/time "$PATHTOEXE" -redo -s example.phy -m TIM+I+G -bb 1000
/bin/time "$PATHTOEXE" -redo -s example.phy -m TIM+I+G -b 100
/bin/time "$PATHTOEXE" -redo -s example.phy -m TIM+I+G -alrt 1000
/bin/time "$PATHTOEXE" -redo -s example.phy -m TIM+I+G -lbp 1000
/bin/time "$PATHTOEXE" -redo -s example.phy -m TIM+I+G -alrt 1000 -lbp 1000
/bin/time "$PATHTOEXE" -redo -s example.phy -m TIM+I+G -bb 1000 -alrt 1000 -lbp 1000
/bin/time "$PATHTOEXE" -redo -s example.phy -sp example.nex
/bin/time "$PATHTOEXEOMP" -redo -s example.phy
if [ -e example.treels ] ; then
  /bin/time "$PATHTOEXE" -redo -s example.phy -z example.treels
  /bin/time "$PATHTOEXE" -redo -s example.phy -z example.treels -n 1
  /bin/time "$PATHTOEXE" -redo -s example.phy -z example.treels -n 1 -zb 1000
  /bin/time "$PATHTOEXE" -redo -s example.phy -z example.treels -n 1 -zb 1000 -zw
fi
/bin/time "$PATHTOEXE" -redo -s example.phy -m 010010+G
if [ -e mymodel ] ; then
  /bin/time "$PATHTOEXE" -redo -s example.phy -m mymodel+G
fi
/bin/time "$PATHTOEXE" -redo -s example.phy -m 'TN{2.0,3.0}+G8{0.5}+I{0.15}'
/bin/time "$PATHTOEXE" -redo -s example.phy -m GTR+G+Fo
