#!/bin/bash
#
# Try to run the given command in a pipe to the test meter, but if that
# fails, just dump it to stdout instead.  Also make sure nobody *inside*
# us runs the testhelper script again.
#
export WVTESTRUN="env"

reportcode()
{
    CODE=$1
    if [ "$CODE" = "0" ]; then
        echo "! wvtesthelper   Test program exited ok    ok"
    else
        echo "! wvtesthelper   Test program aborted (code $CODE)   FAILED"
    fi
    return $CODE
}

exec 3>&1

if [ -z "$DISPLAY" ]; then
    exec "$@"
else
    WVTESTMETER=$WVSTREAMS_SRC/wvtestmeter
    if [ ! -e $WVSTREAMS_SRC/wvtestmeter ]; then
       WVTESTMETER=wvtestmeter
    fi

    ( "$@"; reportcode $?) \
    	| tee /dev/fd/3 | ($WVTESTMETER || cat)
    exit ${PIPESTATUS[0]}
fi
