#!/bin/sh

if test x"$srcdir" != x""; then
    builddir="."	# running from make check, but it does not define that
else
    srcdir=`echo "$0" | sed s,[^/]*$,,`
    test "$srcdir" = "$0" && srcdir=.
    test -z "$srcdir" && srcdir=.
    builddir="$srcdir"	# running manually, have to assume
fi

srcdir=`cd $srcdir;pwd`
builddir=`cd $builddir;pwd`

testfile="$srcdir/tests"
if [ $# -ge 1 ]; then testfile="$1"; fi

dirs=`awk -F '#' '{print $1}' $testfile`

basedir=`head -n 1 $testfile | awk '{print $2}'`
cd $srcdir; if [ ! -d $basedir ]; then
    cd ..; if [ ! -d $basedir ]; then exit 77; fi	# for make distcheck
fi
basedir=`cd $basedir;pwd`

cd $builddir
error=0

a52dec="../src/a52dec"; if [ ! -x $a52dec ]; then
    if [ -x ../vc++/Release/a52dec.exe ]; then
	a52dec="../vc++/Release/a52dec.exe"
	basedir=`echo "$basedir" | sed "s,/cygdrive/\(.\)/,\1:/,"`
    elif [ -x ../vc++/Debug/a52dec.exe ]; then
	a52dec="../vc++/Debug/a52dec.exe"
	basedir=`echo "$basedir" | sed "s,/cygdrive/\(.\)/,\1:/,"`
    else
	echo "Can not find a52dec executable"; exit 1
    fi
fi

for dir in $dirs; do
    echo $dir
    $a52dec -co float $basedir/$dir/stream >/dev/null 2>&1 >output.float
    ./compare output.float $basedir/$dir/output.float || error=1
    rm -f output.float
done

exit $error
