#!/bin/bash
set -e

pkg=bowtie2

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 -ar /usr/share/doc/${pkg}/examples "${AUTOPKGTEST_TMP}"/
cp -ar scripts/test "${AUTOPKGTEST_TMP}"/
cp -ar scripts/sim "${AUTOPKGTEST_TMP}"/

cd "${AUTOPKGTEST_TMP}"/test
perl simple_tests.pl --bowtie2=/usr/bin/bowtie2 \
	--bowtie2-build=/usr/bin/bowtie2-build \
	--compressed

cd "${AUTOPKGTEST_TMP}"/sim
perl run.pl --bowtie2=/usr/bin/bowtie2 \
	--bowtie2-build=/usr/bin/bowtie2-build \
	--cpus=$(nproc)

cd "${AUTOPKGTEST_TMP}"/examples

# check-wo-arguments
TMP="$(mktemp)"
# Run bowtie2 without any arguments and save stderr in a temporary file:
set +e
STDOUT="$(bowtie2 2> ${TMP})"
# Exit status of the previous command should be 1
[ "$?" -eq 1 ]
set -e
# Save help message into a variable:
HELP_MESSAGE="$(bowtie2 --help)"
# Check if stderr contains help message:
grep -q "${HELP_MESSAGE}" "${TMP}"
echo "SUCCESS: help message was detected!"
rm "${TMP}"

# indexing-ref-genome
examplesdir=/usr/share/doc/$pkg/examples
for file in reference/lambda_virus.fa.gz reads/reads_1.fq.gz
do
	extracted=`echo $file | awk -F/ '{print $NF}'`
        zcat $examplesdir/$file >${extracted%.gz}
done
bowtie2-build lambda_virus.fa lambda_virus >/dev/null 2>&1
bowtie2 -x lambda_virus -U reads_1.fq -S eg1.sam >/dev/null 2>&1

# binary-run
for file in build align inspect
do
	${pkg}-${file}-l -h >/dev/null 2>&1
	${pkg}-${file}-s -h >/dev/null 2>&1
done
