#!/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

trf test_seqs.fasta 2 5 7 80 10 50 2000 -l 10

echo "comparing test results ..."
html2text test_seqs.fasta.s1.2.5.7.80.10.50.2000.1.html > test.txt
result=`cat test.txt | grep -A 2 Entropy | grep -Ev 'Entropy|Size'`
result=${result//_}
echo "$result"

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

IFS='|'
read -ra ADDR <<< "$result"
cnt=0
for i in "${ADDR[@]}";
do
	echo $i

	if [ "$cnt" = 2 ]
	then
		echo "Compare consensus size"
		if [ "$i" != 7 ]
		then
			exit 1
		fi
	fi

	if [ "$cnt" = 12 ]
	then
		echo "Comparing extropy"
		check_compare $i 1.5
	fi
	let "cnt=cnt+1"
done

