#!/bin/sh

usage()
{
	echo "Usage: $0 ORIGINAL_TEST_DIR TARGET_TEST_DIR" 1>&2
}

if [ $# != 2 ]; then
	usage
	exit 1
fi

original_test_dir="${1}"
target_test_dir="${2}"

check_diff()
{
    if ! diff "${1}" "${2}" \
	  | grep -v TransactionID \
	  | grep -v 'gml:beginPosition' \
	  | grep -v 'gml:endPosition'  \
	  | egrep -v '^[0-9]*(,[0-9]*)?c[0-9]*(,[0-9]*)?$' \
	  | grep -v '^---$' \
	  | egrep -v '^[<>][ 	]+at daruma\.[^\(]+\([^)\.]+\.java:[0-9]+\)$' ; then
	    return 0
    else
	    return 1
    fi
}


file_list=`cd ${original_test_dir} && find . -name '*.output.xml' -print`

failed_files=''
failure_count=0

for f in `echo ${file_list}`
do
    basename=`basename "${f}"`

#    printf '%s' "checking ${basename} ... "

    if check_diff "${original_test_dir}/${f}" "${target_test_dir}/${f}" > /dev/null; then
#	echo "OK"
    else
	if [ $failure_count -ne 0 ]; then
		echo
		echo
		echo
	fi

	printf '%s' "${f}: "
	echo "FAILED!!"

	check_diff "${original_test_dir}/${f}" "${target_test_dir}/${f}"

	failed_files="${failed_files} ${f}"
	failure_count=`expr ${failure_count} + 1`
    fi
done

if [ $failure_count -ne 0 ]; then
    echo
    echo
    echo

    for f in `echo ${failed_files}`
	do
	echo "failed: ${f}"
    done
fi
