#!/bin/sh
#
#   Copyright (C) 2014 Masatake YAMATO
#
#   This program 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 2 of the License, or
#   (at your option) any later version.
#
#   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

READTAGS=${1:-./readtags}
UNITS=${2:-./Units}
s=0

sink()
{
    cat > /dev/null
}

if ! [ -f "${READTAGS}" ]; then
    echo "No such file: ${READTAGS}" 1>&2
    exit 1
fi

if ! [ -x "${READTAGS}" ]; then
    echo "Not an executable: ${READTAGS}" 1>&2
    exit 1
fi

if ! [ -x "${UNITS}" ]; then
    echo "Not such directory: ${UNITS}" 1>&2
    exit 1
fi


for tags in $(find "$UNITS" -name expected.tags); do
    for t in $(sed -e 's/^\([^\t]*\)\t.*/\1/' "$tags"); do
	if r=$( "${READTAGS}" -t "$tags" - "$t" 2>/dev/null ); then
	    :
	else
	    printf "FAILED[%3d]: " $?
	    echo "${READTAGS}" -t "$tags" - "$t"
	    s=1
	fi
    done 
done

exit $s
