#!/bin/sh

host=localhost
port=5050

usage()
{
	echo "Usage: ${0} [option ...] TEST_BASE_NAME"
	echo "Possible options are:"
	echo "     --help"
	echo "  -h|--host HOST (default: ${host})"
	echo "  -p|--port PORT (default: ${port})"
}


#
# guess path of this program
#
program_name=check-io

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
#
base=''

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
		;;

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

	*)
		if [ -z "${base}" ]; then
			base="${1}"
		else
			usage 1>&2
			echo 1>&2
			echo "TEST_BASE_NAME multiply defined" 1>&2
			exit 1
		fi
		;;
	esac

	shift 1
done

if [ -z "${base}" ]; then
	usage 1>&2
	echo 1>&2
	echo "TEST_BASE_NAME not defined" 1>&2
	exit 1
fi


#
# check exsistence of command
#
#   This function is replacement of `which' command for portability.
#
which_command()
{
	command="${1}"

	for p in `echo "${PATH}" | sed 's/:/ /g'`
	do
		if [ -x "${p}/${command}" ]; then
			echo "${p}/${command}"
			return 0
		fi
	done

	return 1
}

error=false
if ! which_command xmllint 1>/dev/null; then
	echo "\"xmllint\" command not found, install \"libxml\"" 1>&2
	echo "	refer to http://xmlsoft.org/" 1>&2
	error=true
fi

if ! which_command xmldiff 1>/dev/null; then
	if [ X"${error}" = X'true' ]; then
		echo 1>&2
	fi

	echo "\"xmldiff\" command not found, install it first" 1>&2
	echo "	refer to http://www.logilab.org/projects/xmldiff/" 1>&2
	error=true
fi

if ! which_command darumaClient 1>/dev/null; then
	if [ X"${error}" = X'true' ]; then
		echo 1>&2
	fi
	echo "\"darumaClient\" command not found" 1>&2
	error=true
fi

if [ X"${error}" = X'true' ]; then
	exit 1
fi



#
# main
#
input="${base}".input.xml
if [ ! -r "${input}" ]; then
	if [ -r "${base}" ] \
	   && echo -n "${base}" | grep '\.input.xml$' > /dev/null 2>&1; then
		input="${base}";
		base=`echo "${base}" | sed 's/\.input.xml$//'`
	else
		echo "file \"${input}\" not found" 1>&2
		exit 1
	fi
fi



output="${base}".output.xml
expected_xml="${base}".expected.xml
expected_xsd="${base}".expected.xsd

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


if [ ! -r "${expected_xml}" -a ! -r "${expected_xsd}" ]; then
	if [ "${dirname}/.default.expected.xsd" ]; then
		expected_xsd="${dirname}/.default.expected.xsd"
	elif [ "${dirname}/.default.expected.xml" ]; then
		expected_xml="${dirname}/.default.expected.xml"
	else
		echo -n "file \"${expected_xml}\" nor \"${expected_xml}\"" 1>&2
		echo " not found" 1>&2
		exit 1
	fi
fi

darumaClient --host "${host}" --port "${port}" < "${input}" > "${output}"

xmllint --noout --nonet "${output}" || exit 1

if [ -r "${expected_xsd}" ]; then
	if ! xmllint --noout --nonet --schema "${expected_xsd}" "${output}" \
	     2>/dev/null; then
		xmllint --noout --nonet --schema "${expected_xsd}" "${output}"
		exit 1
	fi
fi

if [ -r "${expected_xml}" ]; then
	if [ -s "${expected_xml}" -a ! -s "${output}" ]; then
		echo 'unexpected empty output' 1>&2
		exit 1
	fi

	xmldiff "${expected_xml}" "${output}" || exit 1
fi
