#!/bin/sh

t=`basename "$1"`

TESTLIST="${srcdir}/tests.list"
INPUT_DIR="${srcdir}/inputs"
GOLDEN_DIR="${srcdir}/outputs"

rundir="${t}.run"


# figure out what files we need to copy for this test and what
# arguments to feed refdes_renum

files=`grep "^[ \t]*${t}[ \t]*|" "${TESTLIST}" | awk 'BEGIN{FS="|"} {print $2}'`
args=`grep "^[ \t]*${t}[ \t]*|" "${TESTLIST}" | awk 'BEGIN{FS="|"} {print $3}'`
code=`grep "^[ \t]*${t}[ \t]*|" "${TESTLIST}" | awk 'BEGIN{FS="|"} {print $4}'`

if test "X${code}" = "X" ; then
    code=0
fi


# create temporary run directory with the needed files

rm -fr "${rundir}"
mkdir -p "${rundir}"

if test ! -z "${files}" ; then
    for f in ${files} ; do
	cp "${INPUT_DIR}/${f}" "${rundir}"
	chmod 644 "${rundir}/${f}"
    done
fi


# run refdes_renum

here=`pwd`
cd "${rundir}"

echo "${REFDES_RENUM} ${args} ${files}"
${REFDES_RENUM} ${args} ${files}

rc=$?
if test ${rc} -ne "${code}" ; then
    echo "refdes_renum returned ${rc} which did not match the expected ${code}"
    exit 1
fi

cd "${here}"


# check output and clean up

status=0

for f in ${files} ; do
    ref="${GOLDEN_DIR}/${t}/${f}"
    out="${rundir}/${f}"

    if test "X${REGEN}" = "X1" ; then
	cp "${out}" "${ref}"
    elif test -f "${ref}" ; then
	diff -wu "${ref}" "${out}" || status=1
    else
	echo "No reference file.  Skipping"
	test ${status} -ne 0 || status=77
    fi
done

test ${status} -ne 0 || rm -fr "${rundir}"
exit ${status}
