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

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