#!/bin/bash

# You need a working Hadoop cluster to run this.
# Copyright (C) 2011-2012 CRS4.
#
# This file is part of Seal.
#
# Seal is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Seal is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Seal.  If not, see <http://www.gnu.org/licenses/>.

#set -x
set -o nounset
set -o errexit

TestDir="$(dirname $0)"
source ${TestDir}/../blocks.sh

prep $@

${HADOOP} dfsadmin -safemode wait
${HADOOP} dfs -mkdir "${WD}/bin"
${HADOOP} dfs -put "${TestDir}/input" "${WD}/input"
output=$(${SealDir}/bin/prq -D seal.prq.min-bases-per-read=0 -D seal.prq.drop-failed-filter=true -D seal.qseq-input.base-quality-encoding=sanger --num-reducers 1 "${WD}/input" "${WD}/output" 2>&1)
nfailed=$(echo "${output}" | grep "FailedFilter=" | sed 's/.*FailedFilter=\([0-9]\+\)/\1/')
ndropped=$(echo "${output}" | grep "Dropped=" | sed 's/.*Dropped=\([0-9]\+\)/\1/')
${HADOOP} dfs -get "${WD}/output" "${OutputDir}"
${HADOOP} dfs -rmr "${WD}"

compare_sorted_output "${TestDir}/expected"  # sets exit_code
if [ $exit_code != 0 ]; then
	echo "Unexpected test output" >&2
elif [ "${nfailed}" != "4" ]; then
	echo "Unexpected counted reads without enough known bases (expected " 4 " but got ${neb})" >&2
	exit_code=2
elif [ "${ndropped}" != "2" ]; then
	echo "Unexpected counted reads dropped (expected " 2 " but got ${ndropped})" >&2
	exit_code=3
fi

show_test_msg $exit_code "${TestDir}"
rm_output_dir
exit $exit_code
