#!/bin/sh

#use test.tar.xz from /usr/share/doc/subread/examples/ if executed with
#autopkgtest
#use binaries and test dir from the source package if executed
#by override_dh_auto_test:

set -e
pkg=subread
examplesdir=/usr/share/doc/$pkg/examples
if [ $# -eq 0 ]
then
    TMPDIR=$(mktemp -d)
	trap "rm -rf $TMPDIR" 0 INT QUIT ABRT PIPE TERM
    tar -xf ${examplesdir}/test.tar.xz -C $TMPDIR
    WORKDIR=${TMPDIR}/test
    #copy binaries to the location expected by the tests
    mkdir ${TMPDIR}/bin
    for bin in exactSNP featureCounts subjunc subindel subread-align subread-buildindex
    do
        cp /usr/bin/${bin} ${TMPDIR}/bin/
    done
else
    #run tests in the source package if executed during building process
    TMPDIR=$1
    cp -r ${TMPDIR}/test ${TMPDIR}/adt_test
    export PATH=${PATH}:${TMPDIR}/bin:${TMPDIR}:/bin/utilities
    WORKDIR=${TMPDIR}/adt_test
fi

cd $WORKDIR

bash test_all.sh

if [ $# -eq 0 ]
then
    rm -rf $TMPDIR
else
    rm -rf $WORKDIR
fi
