#!/bin/bash

set -e
shopt -s extglob

test=$1

case $test in
download/*) DISTDIR= ;;
*) DISTDIR=${top_srcdir}/testsuite/distdir ;;
esac

test -d ${top_builddir}/testsuite/$test || mkdir -p ${top_builddir}/testsuite/$test
cd ${top_builddir}/testsuite/$test

cp -f ${top_srcdir}/testsuite/$test/!(hints) .

DISTDIR=${DISTDIR} ${top_builddir}/bin/cygport-inplace *.cygport clean get all

# compare hints
for hint in $(find -name \*.hint)
do
    if [ -a ${top_srcdir}/testsuite/$test/hints/$hint ];
    then
        diff -u ${top_srcdir}/testsuite/$test/hints/$hint $hint
    else
        # if CREATE_HINTS is set, create the expected hint file for use in future testing
        if [ -z ${CREATE_HINTS+x} ]
        then
            echo "can't verify hint $hint, as expected hint ${top_srcdir}/testsuite/$test/hints/$hint not found." >&2
            missing_hints=1
        else
            mkdir -p $(dirname ${top_srcdir}/testsuite/$test/hints/$hint)
            cp $hint ${top_srcdir}/testsuite/$test/hints/$hint
        fi
    fi
done

if [ -n "$missing_hints" ]
then
    exit 1
fi

# compare filelist
diff -u ${CASE_INSENSITIVE:+-i} ${top_srcdir}/testsuite/$test/*.list <(${top_builddir}/bin/cygport-inplace *.cygport list)
