#!/bin/bash
set -e

pkg=ivar

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 *
ls -R
#do_stuff_to_test_package#
echo -e "\e[93m\e[1mTest 1\e[0m"
ivar trim -i data/test.sorted.bam -b data/test.bed -p out-trim
samtools view out-trim.bam > out-trim.sam
diff out-trim.sam ref/out-trim.sam
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 2\e[0m"
ivar trim -i data/test.sorted.bam -b data/test.bed -p out-trim2 -q 15 -m 50 -s 4
samtools view out-trim2.bam > out-trim2.sam
diff out-trim2.sam ref/out-trim2.sam
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 3\e[0m"
samtools mpileup -A -d 600000 -B -Q 0 data/test.trimmed.sorted.bam | ivar variants -p out-variants -q 20 -t 0.03
diff out-variants.tsv ref/out-variants.tsv
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 4\e[0m"
ivar filtervariants -t 0.5 -p out-filtered out-variants.tsv
diff out-variants.tsv ref/out-variants.tsv
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 5\e[0m"
samtools mpileup -d 1000 -A -Q 0 data/test.sorted.bam | ivar consensus -p out-consensus -q 20 -t 0
diff out-consensus.qual.txt ref/out-consensus.qual.txt
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 6\e[0m"
ivar getmasked -i data/test.filtered.tsv -b data/test.bed  -f data/out-filtered.tsv -p out-getmasked
diff out-getmasked.txt ref/out-getmasked.txt
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 7\e[0m"
ivar removereads -i out-trim.bam -p out-removereads -t data.out-getmasked.txt -b data/test.bed
samtools view out-removereads.bam > out-removereads.sam
diff out-removereads.sam ref/out-removereads.sam
echo -e "\e[92m\e[1mPassed\e[0m"
echo

