#!/bin/bash
set -e

pkg="primer3"
TESTOPTS="/usr/bin/valgrind"

if [ "$AUTOPKGTEST_TMP" = "" ] ; then
  AUTOPKGTEST_TMP=`mktemp -d /tmp/${pkg}-test.XXXXXX`
fi

BUILDARCH=$(dpkg-architecture -q DEB_BUILD_ARCH_ENDIAN)

P3CORE_FAILED_TESTS="primer_masker primer_masker_formatted"

FAILED_TESTS="testmasker"

cp -a /usr/share/doc/${pkg}/examples/* $AUTOPKGTEST_TMP

cd $AUTOPKGTEST_TMP

find . -name "*.gz" -exec gunzip \{\} \;
ls
ln -s /usr/bin/primer3_core ./src/primer3_core
ln -s /usr/bin/ntdpal ./src/ntdpal
ln -s /usr/bin/ntthal ./src/ntthal
ln -s /usr/bin/oligotm ./src/oligotm

if [ $BUILDARCH = big ]; then
  cp -a test/p3test.pl test/p3test.pl~
  cp -a test/Makefile test/Makefile~
  # exclude tests known to fail on big endian
  # See README.source for further explanation.
  for tst in $P3CORE_FAILED_TESTS ; do sed -i "/${tst}.,/d" test/p3test.pl ; done
  for tst in $FAILED_TESTS; do sed -i "/^test:/s/$tst//" test/Makefile; done
fi

cd test/;

echo "testcmdline:"
perl cmdline_test.pl ${TESTOPTS};

echo "testcore:"
echo; echo; echo 'TESTING primer3_core'; echo; perl p3test.pl ${TESTOPTS};

echo "testdpal:"
echo; echo; echo 'TESTING ALIGNMENT CALCS'; echo; perl dpal_test.pl ${TESTOPTS}; 

echo "testthal:"
echo; echo; echo 'TESTING THERMODYNAMIC ALIGNMENT CALCS'; echo; perl thal_test.pl ${TESTOPTS};

echo "testtm:"
perl oligotm_test.pl ${TESTOPTS}

cd ..

if [ $BUILDARCH = big ]; then
  # restore original test file
  mv test/p3test.pl~ test/p3test.pl
  mv test/Makefile~ test/Makefile
fi

echo "PASS"
