#!/bin/bash
set -e

pkg=mapsembler2

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}"

gunzip -r *
#do_stuff_to_test_package#
echo -e "\e[93m\e[1mTest 1\e[0m"
run_mapsembler2_pipeline -s starter.fa -r "reads1.fa reads2.fa" -t 3 -p res
if [ "$?" -ne "0" ]; then
  echo -e "\e[31m\e[1mTest could not be run.\e[0m"
  exit 1
fi
diff res_k_31_c_5_t_3_modified_and_covered.json ref-res_k_31_c_5_t_3_modified_and_covered.json --suppress-common-lines
if [ "$?" -ne "0" ]; then
  echo -e "\e[31m\e[1mOutput did not match the reference.\e[0m"
  exit 1
fi
echo -e "\e[92m\e[1mPassed\e[0m"

echo -e "\e[93m\e[1mTest 2\e[0m"
run_mapsembler2_pipeline -s starter.fa -r "reads1.fa reads2.fa" -t 1 -p reads1
if [ "$?" -ne "0" ]; then
  echo -e "\e[31m\e[1mTest could not be run.\e[0m"
  exit 1
fi
diff reads1_coherent_k_31_c_5_t_1.fasta ref-reads1_coherent_k_31_c_5_t_1.fasta --suppress-common-lines
diff reads1_uncoherent_k_31_c_5_t_1.fasta ref-reads1_uncoherent_k_31_c_5_t_1.fasta --suppress-common-lines
if [ "$?" -ne "0" ]; then
  echo -e "\e[31m\e[1mOutput did not match the reference.\e[0m"
  exit 1
fi
echo -e "\e[92m\e[1mPassed\e[0m"

echo -e "\e[93m\e[1mTest 3\e[0m"
mapsembler_extend starter.fa reads1.fa reads2.fa
if [ "$?" -ne "0" ]; then
  echo -e "\e[31m\e[1mTest could not be run.\e[0m"
  exit 1
fi
diff res_mapsembler_k_31_c_2_t_1.fasta ref-res_mapsembler_k_31_c_2_t_1.fasta --suppress-common-lines
if [ "$?" -ne "0" ]; then
  echo -e "\e[31m\e[1mOutput did not match the reference.\e[0m"
  exit 1
fi
echo -e "\e[92m\e[1mPassed\e[0m"

echo -e "\e[93m\e[1mTest 4\e[0m"
mapsembler2_extremities --k 20 --starters reads1_coherent_k_31_c_5_t_1.fasta --reads "reads1.fa reads2.fa" --output mapsembler2_extremities.out.fa
if [ "$?" -ne "0" ]; then
  echo -e "\e[31m\e[1mTest could not be run.\e[0m"
  exit 1
fi
diff mapsembler2_extremities.out.fa ref-mapsembler2_extremities.out.fa --suppress-common-lines
if [ "$?" -ne "0" ]; then
  echo -e "\e[31m\e[1mOutput did not match the reference.\e[0m"
  exit 1
fi
echo -e "\e[92m\e[1mPassed\e[0m"
