#!/bin/bash
set -e

pkg=stringtie
CUR_DIR=`pwd`

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 ${CUR_DIR}/debian-tests-data/* -a "${AUTOPKGTEST_TMP}"

cd "${AUTOPKGTEST_TMP}"
gunzip -r *

cat long_reads.sam| samtools view -Sb - > long_reads.bam
cat short_reads_and_superreads.sam| samtools view -Sb - > short_reads_and_superreads.bam
cat short_reads.sam| samtools view -Sb - > short_reads.bam

# skipping the first line because it differs due to location of compiled binary
function test_output()
{
	# $1: Test Number
	# $2: output file
	# $3: input bam file
	# $4: expected output file
	echo "Test $1"
	stringtie -o $2 $3
	if [ "$1" = 3 ]
	then
		stringtie -L -o $2 $3
	elif [ "$1" = 4 ]
	then
		stringtie -L -G human-chr19_P.gff -o $2 $3
	fi
	if [ $(dpkg-architecture -qDEB_BUILD_ARCH) = "amd64" ]
	then
		diff -u <(tail -n +3 $2) <(tail -n +3 $4)
		echo "Expected output and generated output are same: PASS"
	else
		[ -s $2 ] || exit 1
		echo "PASS"
	fi
}


test_output 1 short_reads.out.gtf short_reads.bam short_reads.out_expected.gtf
test_output 2 short_reads_and_superreads.out.gtf short_reads_and_superreads.bam short_reads_and_superreads.out_expected.gtf
test_output 3 long_reads.out.gtf long_reads.bam long_reads.out_expected.gtf
test_output 4 long_reads_guided.out.gtf long_reads.bam long_reads_guided.out_expected.gtf
