#!/bin/bash
set -e

pkg=ngmlr

export LC_ALL=C.UTF-8
if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  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 *

echo -e "Test 1"
ngmlr --skip-write -r data/test_2/ref_chr21_20kb.fa -q data/test_2/reads_100_2200bp.fa 2> /dev/null | samtools view -Sb - 2> /dev/null | bedtools bamtobed > data/test_2/found.bed
diff -u data/test_2/expected.bed data/test_2/found.bed
echo -e "Passed"
echo

echo -e "Test 2"
ngmlr --skip-write -x pacbio --no-progress -t 4 -R 0.01 -r data/test_3/reference.fasta -q data/test_3/read.fa 2> /dev/null | samtools view -Sb - 2> /dev/null | bedtools bamtobed | sort > data/test_3/expected.bed
for i in {1..5}
do
	ngmlr --skip-write -x pacbio --no-progress -t 4 -R 0.01 -r data/test_3/reference.fasta -q data/test_3/read.fa 2> /dev/null | samtools view -Sb - 2> /dev/null | bedtools bamtobed | sort > data/test_3/found.bed
	diff -u data/test_3/expected.bed data/test_3/found.bed || exit 1
done
echo -e "Passed"
echo

echo -e "Test 3"
ngmlr --skip-write -x pacbio --no-progress -t 4 -r data/test_4/reference.fasta -q data/test_4/read.fa 2> /dev/null | samtools view -S - 2> /dev/null | sort | cut -f 1,2,3,4,5 > data/test_4/found.txt
diff -u data/test_4/expected.txt data/test_4/found.txt || exit 1
echo -e "Passed"
echo

echo -e "Test 4"
LEN=`ngmlr --skip-write -x pacbio --no-progress -t 4 -r data/test_5/reference.fasta -q data/test_5/read.fa 2> /dev/null | grep -v "^@" | cut -f 1 | wc -c`
if [ "$LEN" -gt 254 ]
then
	exit 1		
fi


echo -e "Test 5"
ngmlr --version 1>version_info 2>>version_info
if grep -q "%s" version_info; then
       echo "E: the logging shows the format string"
       exit 1
fi
rm -f version_info

echo -e "Passed"
echo
