#!/bin/sh

host=localhost
port=5050
exit_on_failure="false"
clean_command="daruma-setup --init-only"

usage()
{
	echo "Usage: ${0} [option ...] [TEST_DIR|TEST_BASE_NAME]"
	echo "Possible options are:"
	echo "     --help"
	echo "  -h|--host HOST (default: ${host})"
	echo "  -p|--port PORT (default: ${port})"
	echo "  -e|--exit-on-failure"
	echo "     --clean-command (default: ${clean_command})"
}


#
# guess path of this program
#
program_name=run-test

if [ -z "${DARUMA_TEST_PATH}" ]; then
	search_path="${PATH}"

	argv0_path=`echo $0 | sed 's/\/*[^\/]*$//'`

	if [ ! -z "${argv0_path}" ]; then
		search_path="${argv0_path}:${search_path}"
	fi

	for dir in `echo ${search_path} | sed 's/:/ /g'`
	do
		if [ -x "${dir}/${program_name}" ]; then
			DARUMA_TEST_PATH="${dir}"
			break
		fi

		if [ -z "${DARUMA_TEST_PATH}" ]; then
			DARUMA_TEST_PATH='.'
		fi
	done
fi

DARUMA_TEST_PATH=`echo ${DARUMA_TEST_PATH} | sed 's/\/$//'`

if [ X${DARUMA_TEST_PATH} = X'.' ];then
	DARUMA_TEST_PATH=`pwd | sed 's/\/$//'`
fi



#
# analyze options
#
tests=''

while [ "${#}" -gt 0 ]
do
	case "${1}" in

	--help)
		usage 1>&2
		exit 0
		;;

	--host|-h)
		host="${2}"
		shift 1
		;;

	--port|-p)
		port="${2}"
		shift 1
		;;
	--exit-on-failure|-e)
		exit_on_failure="true"
		;;

	--clean-command)
		clean_command="${2}"
		shift 1
		;;

	-*)
		usage 1>&2
		echo 1>&2
		echo "unknown option: ${1}" 1>&2
		exit 1
		;;

	*)
		tests="${tests} ${1}"
		;;
	esac

	shift 1
done

if [ -z "${tests}" ]; then
	tests='.'
fi



#
# main process
#

#result_column=70

error_count=0
error_tests=""

check_file_base()
{
	input_file="${1}"

	file_base=`echo ${input_file} | sed 's/\.input\.xml$//'`

	print_name=`echo "${file_base}" | sed 's|^\([^/]*/\)*||'`

	basename=`basename "${file_base}"`
	dirname=`dirname "${file_base}"`

	message="checking ${print_name} ... "

	echo -n "${message}" 1>&2

#	column=`echo -n "${message}" | wc -c`
#
#	while [ ${column} -lt ${result_column} ]
#	do
#		echo -n ' '
#
#		column=`expr ${column} + 1`
#	done


	if "${DARUMA_TEST_PATH}"/check-io \
	      --host "${host}" --port "${port}" "${file_base}" \
				2> "${dirname}/${basename}".error; then
		echo "OK"

		if [ ! -s "${dirname}/${basename}".error ]; then
			rm "${dirname}/${basename}".error
		fi
	else
		echo "FAILED!!"

		(cd ${dirname} \
		 && rm -f ".failed.${basename}".xml \
		 && ln -s "${basename}".output.xml \
			  ".failed.${basename}".xml)

		error_count=`expr ${error_count} + 1`
		error_tests="${error_tests} ${dirname}/${basename}"

		if [ ${exit_on_failure} = "true" ]; then
			echo
			if [ -s "${dirname}/${basename}".output.xml ]; then
				cat "${dirname}/${basename}".output.xml
			else
				echo "empty output"
			fi
			echo
			echo "skipping rest of tests ..."
			exit 1
		else
			return 1
		fi
	fi
}


check_dir()
{
	dir="${1}"

	if [ ! -d "${dir}" ]; then
		echo "${dir} not found, ignored"
		return 1
	fi

	if [ -e "${dir}/.ignore" ]; then
		return
	fi


	if [ -e "${dir}/.clean" ]; then
		echo cleaning ...
		(${clean_command}) || exit 1
	fi


	list=`ls "${dir}" | sort -n \
		| awk "{printf(\"%s/%s\n\", \"${dir}\", \\$1)}"`
	for i in ${list}
	do
		if [ -d "${i}" ]; then
			check_dir "${i}"
		elif [ -f "${i}" ] \
		     && echo "${i}" | grep '\.input.xml$' 1>/dev/null; then
			check_file_base "${i}"
		fi
	done
}


checked=false
for t in ${tests}
do
	if [ -d "${t}" ]; then
		check_dir "${t}"
		checked=true
	elif [ -f "${t}.input.xml" ]; then
		check_file_base "${t}"
		checked=true
	fi
done


if [ $checked = 'false' ]; then
	echo "no tests found" 1>&2
	exit 1
fi


if [ ${error_count} -ne 0 ]; then
	echo 1>&2
	echo -n "${error_count}"
	if [ ${error_count} -eq 1 ]; then
		echo " error" 1>&2
	else
		echo " errors" 1>&2
	fi

	for t in ${error_tests}
	do
		echo "	${t}"
	done

	echo 1>&2
	echo "FAILED!!" 1>&2
	echo 1>&2

	exit 1
fi
