#!/bin/bash
set -e

pkg=trf

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 *.gz

function check_compare()
{
	# $1: result of the command
	# $2: value to compare result with
	if (( $(echo "$1 < $2" |bc -l) ))
	then
			exit 1
	fi
}

function verify_generated_output()
{

# $1: the name of html file to be compared
# $2: Consensus size
# $3: lower bound for entropy
# $4: upper bound for entropy
echo "comparing test results of $1 ..."
html2text $1 > test.txt
result=`cat test.txt | grep -A 2 Entropy | grep -Ev 'Entropy|Size'`
result=${result//_}
echo "$result"


IFS='|'
read -ra ADDR <<< "$result"
cnt=0
for i in "${ADDR[@]}";
do
	if [ "$cnt" = 2 ]
	then
		echo "Compare consensus size"
		if [ "$i" -ne "$2" ]
		then
			exit 1
		fi
	fi

	if [ "$cnt" = 12 ]
	then
		echo "Comparing entropy"
		check_compare $i $3
		check_compare $4 $i
	fi
	let "cnt=cnt+1"
done
rm -f ./text.txt
echo "PASS"

}

if [ $(dpkg-architecture -qDEB_BUILD_ARCH) = "amd64" ]
then
	trf test_seqs.fasta 2 5 7 80 10 50 2000 -l 10
	verify_generated_output test_seqs.fasta.s4.2.5.7.80.10.50.2000.1.html 125 1.9 2
else
	sed -i $(grep -n \>3 test_seqs.fasta | cut -f1 -d:),$(wc -l test_seqs.fasta | awk '{print $1}')d test_seqs.fasta
	trf test_seqs.fasta 2 5 7 80 10 50 2000
fi

verify_generated_output test_seqs.fasta.s3.2.5.7.80.10.50.2000.1.html 35 1.9 2
verify_generated_output test_seqs.fasta.s2.2.5.7.80.10.50.2000.1.html 12 1.8 2
verify_generated_output test_seqs.fasta.s1.2.5.7.80.10.50.2000.1.html 7 1.9 2

