#!/bin/bash
set -ex

pkg=canu

export LC_ALL=C.UTF-8
if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  # shellcheck disable=SC2064
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi
export TMPDIR="${AUTOPKGTEST_TMP}"

if [ "${AUTOPKGTEST_ARTIFACTS}" = "" ] ; then
  AUTOPKGTEST_ARTIFACTS=${PWD}
fi
cd "${AUTOPKGTEST_ARTIFACTS}"

cores=$(nproc)
mem=$(free --giga | grep Mem | awk '{ print $7}')
if ((mem < 6)) ; then
	echo "At least 6G of memory is required to run this test."
	exit 77
elif ((mem < 8)) ; then
	oeaMemory=oeaMemory=${mem}g
	redMemory=redMemory=${mem}g
else
	oeaMemory=
	redMemory=
fi
if ((cores < 4)) ; then
	corThreads=corThreads=${cores}
else
	corThreads=
fi

curl -L -o "${AUTOPKGTEST_TMP}"/pacbio.fastq \
	http://gembox.cbcb.umd.edu/mhap/raw/ecoli_p6_25x.filtered.fastq

# shellcheck disable=SC2086
canu -p ecoli -d ecoli-pacbio \
	genomeSize=4.8m \
	maxThreads="${cores}" ${corThreads} ${oeaMemory} ${redMemory} \
	-pacbio "${AUTOPKGTEST_TMP}"/pacbio.fastq
