#!/bin/bash
set -e

pkg=indelible

export LC_ALL=C.UTF-8
if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  # Double quote below to expand the temporary directory variable now versus
  # later is on purpose.
  # shellcheck disable=SC2064
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

cp -a /usr/share/doc/${pkg}/examples/* "${AUTOPKGTEST_TMP}"

cd "${AUTOPKGTEST_TMP}"

run_test() {
  # $1: test number
  # $2: folder name
  # $3: array of files
  # $4: test description
  echo -e "Test $1: $4"
  cd $2 && indelible > /dev/null 2>&1
  if [ "$?" -ne "0" ]; then
      echo -e "\e[31m\e[1mTest $1 could not be run.\e[0m"
      exit 1
  fi
  list=$3[@]
  files=("${!list}")
  for file in "${files[@]}" ; do
    diff $file ref/$file --suppress-common-lines
    if [ "$?" -ne "0" ]; then
      echo -e "\e[31m\e[1m$file did not match the reference.\e[0m"
      exit 1
    fi
  done
  echo -e "\e[92m\e[1mPassed\e[0m"
  echo
}

gunzip -r *
#do_stuff_to_test_package#
files=("J1out_TRUE.phy" "J2out_TRUE.phy" "trees.txt" "Uout_TRUE.phy" "WFout.fas" "Wout.fas" \
        "J1out.fas" "J2out.fas" "Uout.fas" "WFout_TRUE.phy" "Wout_TRUE.phy")
run_test 1 aminoacid files aminoacid

files=("outputname.fas" "outputname_TRUE.phy" "trees.txt")
run_test 2 ../basicaminoacid files basicaminoacid

run_test 3 ../basiccodon files basiccodon

run_test 4 ../basicnucleotide files basicnucleotide

run_test 5 ../branch files branch

run_test 6 ../branch2 files branch2

files=("out1.fas" "out1_TRUE.phy" "out2.fas" "out2_TRUE.phy" "out3.fas" \
       "out3_TRUE.phy" "out4.fas" "out4_TRUE.phy" "trees.txt")
run_test 7 ../codon files codon

files=("out1_TRUE.phy" "out2_TRUE.phy" "out3_TRUE.phy" "out4_TRUE.phy" "out5_TRUE.phy" \
       "out1.fas" "out2.fas" "out3.fas" "out4.fas" "out5.fas" "trees.txt")
run_test 8 ../codonsites files codonsites

files=("outputname1.fas" "outputname2.fas" "outputname3.fas" "outputname4.fas" "trees.txt" \
       "outputname1_TRUE.phy" "outputname2_TRUE.phy" "outputname3_TRUE.phy" "outputname4_TRUE.phy")
run_test 9 ../indels files indels

files=("GTRout.fas" "HKYout.fas" "JCout.fas" "K81out.fas" \
       "GTRout_TRUE.phy" "HKYout_TRUE.phy" "JCout_TRUE.phy" "K81out_TRUE.phy" "trees.txt")
run_test 10 ../nucleotide files nucleotide

files=("outputname1.fas" "outputname1_TRUE.phy" "outputname2.fas" "outputname2_TRUE.phy" "trees.txt")
run_test 11 ../nucleotide-branch files nucleotide-branch 

files=("outputname1_TRUE.phy" "outputname2_TRUE.phy" "outputname3_TRUE.phy" \
       "outputname4_TRUE.phy" "outputname5_TRUE.phy" "outputname1.fas" "outputname2.fas" \
       "outputname3.fas" "outputname4.fas" "outputname5.fas" "trees.txt")
run_test 12 ../rates files rates

files=("filename.fas" "filename_TRUE.phy" "trees.txt")
run_test 13 ../settings files settings

files=("outputname.fas" "outputname_TRUE.phy" "trees.txt")
run_test 14 ../sitebranch files sitebranch
