#!/bin/bash
set -e

pkg="fastqc"
test_failures=0

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

find . -name "*.gz" -exec gunzip \{\} \;

for sample in example.fastq toy_sam.sam toy_bam.bam ; do
    /usr/bin/fastqc --extract "$sample"
    result="${sample%%.*}_fastqc"
    expected="${result}_expected_summary.txt"
    if ! diff -q "${result}/summary.txt" ${expected} 2>/dev/null ; then
	(( test_failures += 1 ))
        cat <<EOT
Summary results did not match contents of ${expected} for
    fastqc "$sample"
Please investigate!
EOT
    else
        echo "Summary results match for:  fastqc "$sample" "
    fi
    rm -rf "$result"*
    echo ""
done

if (( test_failures == 0 )); then
    echo "PASS"
    exit 0
fi

echo "FAIL mismatched fastqc summary count: $test_failures"
exit 1
