#!/bin/bash
set -e

pkg=prinseq-lite-examples

export LC_ALL=C.UTF-8
if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  # Double quote below to expand the temporary directory variable now versus
  # later is on purpose.
  # shellcheck disable=SC2064
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

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

cd "${AUTOPKGTEST_TMP}"

# test_filetype tests that the file extension matches its expected datatype.
# It is still not ideal compared to real results consistency, but it is better
# than simply testing file exists and is non-void.
test_filetype () {
	local file="$1"
	local type="${file##*.}"
	case "$type" in
	gd   ) echo "$(file "$file")" | grep -q 'ASCII text'     ;;
	html ) echo "$(file "$file")" | grep -q 'HTML document'  ;;
	png  ) echo "$(file "$file")" | grep -q 'PNG image data' ;;
	esac
}

set -v

# prinseq-lite
prinseq-lite --version
prinseq-lite.pl -help || test "$?" = 1

prinseq-lite -verbose -fastq example1.fastq -graph_data pkgtest1.gd \
	-out_good null -out_bad null
test_filetype pkgtest1.gd

prinseq-lite -verbose -fastq example1.fastq -graph_data pkgtest2.gd \
	-out_good null -out_bad null -exact_only
test_filetype pkgtest2.gd

# prinseq-graph
prinseq-graphs --version
prinseq-graphs.pl -help || test "$?" = 1

prinseq-graphs -verbose -i example1.gd -png_all -o pkgtest1
test_filetype pkgtest1_cd.png
test_filetype pkgtest1_ce.png
test_filetype pkgtest1_df.png
test_filetype pkgtest1_dl.png
test_filetype pkgtest1_dm.png
test_filetype pkgtest1_gc.png
test_filetype pkgtest1_ld.png
test_filetype pkgtest1_ns.png
test_filetype pkgtest1_or.png
test_filetype pkgtest1_qd.png
test_filetype pkgtest1_qd2.png
test_filetype pkgtest1_qd3.png
test_filetype pkgtest1_td3.png
test_filetype pkgtest1_td5.png
test_filetype pkgtest1_pm.png
test_filetype pkgtest1_pv.png

prinseq-graphs.pl -verbose -i example1.gd -html_all -o pkgtest1
test_filetype pkgtest1.html

# prinseq-graphs-noPCA
prinseq-graphs-noPCA --version
prinseq-graphs-noPCA.pl -help || test "$?" = 1

prinseq-graphs-noPCA -verbose -i example1.gd -png_all -o pkgtest2
test_filetype pkgtest2_cd.png
test_filetype pkgtest2_ce.png
test_filetype pkgtest2_df.png
test_filetype pkgtest2_dl.png
test_filetype pkgtest2_dm.png
test_filetype pkgtest2_gc.png
test_filetype pkgtest2_ld.png
test_filetype pkgtest2_ns.png
test_filetype pkgtest2_or.png
test_filetype pkgtest2_qd.png
test_filetype pkgtest2_qd2.png
test_filetype pkgtest2_qd3.png
test_filetype pkgtest2_td3.png
test_filetype pkgtest2_td5.png

prinseq-graphs-noPCA.pl -verbose -i example1.gd -html_all -o pkgtest2
test_filetype pkgtest2.html
