#! /bin/sh

# be locale independent
export LC_ALL=C

# run all test cases or stop at first failure?
if [ "x$1" = "x-a" -o "x$ALL" != "x" ]; then
    RUNALL=1
fi

# set unless overriden
: ${prefix:=/usr}
# will be lib64 in biarch ...
: ${libdir:=${prefix}/lib}
: ${PERL:=perl}
# load modules from the proper directory
# and plugins
export Y2DIR=.
TDIR=tests.pl
MODDIR=$TDIR
rm -f $Y2DIR/modules
ln -s $MODDIR $Y2DIR/modules
trap "rm -f $Y2DIR/modules" EXIT TERM

$prefix/bin/ycpc -c $TDIR/*.ycp

RET=0
for SCRIPT in $TDIR/*.pl; do
    BASE=${SCRIPT%.pl}
    OUT=$BASE.out
    ERR=$BASE.err
    BASE=${BASE#$TDIR/}

    echo ----- $BASE
    CASEOK=true
    # y2log to stderr, take specified namespace from Perl
    Y2SILENTSEARCH=1 $PERL $SCRIPT > tmp.out.$BASE 2> tmp.err.$BASE || CASEOK=false
#strace -eopen    $PERL $SCRIPT 2>&1 |grep -v ENOENT > t.out
    # filter out variable information
    sed --in-place -e 's/^....-..-.. ..:..:.. \(<.> \)[^ ]* /\1/' tmp.err.$BASE
    sed --in-place -e 's/\(([^)]*)\):[0-9]\+/\1:XXX/' tmp.err.$BASE
    diff -Nu $ERR tmp.err.$BASE || CASEOK=false
    diff -Nu $OUT tmp.out.$BASE || CASEOK=false

    if ! $CASEOK; then
	RET=1
	[ -n "$RUNALL" ] || break
    fi
done

exit $RET
