#! /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}
# ...but not here
: ${Y2BASE:=${prefix}/lib/YaST2/bin/y2base}
# load modules from the proper directory
# and plugins
export Y2DIR=.
TDIR=tests
MODDIR=$TDIR
# for building (DESTDIR is set to RPM_BUILD_ROOT from outside)
PLUGDIR=${DESTDIR}${libdir}/YaST2/plugin
rm -f $Y2DIR/modules
ln -s $MODDIR $Y2DIR/modules
rm -f $Y2DIR/plugin
ln -s $PLUGDIR $Y2DIR/plugin
trap "rm -f $Y2DIR/modules $Y2DIR/plugin" EXIT TERM

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

    echo ----- $BASE
    CASEOK=true
    # y2log to stderr, take specified namespace from Perl
    Y2SILENTSEARCH=1 $Y2BASE -l - -n $BASE=perl $SCRIPT testsuite > tmp.out.$BASE 2> tmp.err.$BASE
    # filter out variable information
    # - timestamp
    sed --in-place -e 's/^....-..-.. ..:..:.. \(<.> \)[^ ]* /\1/' tmp.err.$BASE
    # - line numbers: [component] whatever_without_space_or_colon:999
    sed --in-place -e 's/\(\[[^]]*\] [^: ]*\):[0-9]\+/\1:XXX/' tmp.err.$BASE
    # filter out uninteresting components
    sed --in-place -e '/^<[0-9]> \[\(liby2\|wfm\|ui-component\)\] /d' tmp.err.$BASE
    # client call contains absolute path, ignore the line
    sed --in-place '/^<1> \[Ruby\] yast\/wfm.rb:XXX Call client /d' 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
