#!/bin/sh

rm -f print.pdf >/dev/null 2>/dev/null

QTVERSION=$(${QMAKE:-qmake} --version 2>&1 | tail -n1| cut -d' ' -f4)
TESTNAME=${0##/}
PASSED=0
FAILED=0
SKIPPED=0

pass() # test_name
{
    echo "PASS   : ${TESTNAME}::$1()"    
    PASSED=$((PASSED+1))
}

fail() # test_name
{
    echo "FAIL!  : ${TESTNAME}::$1()"    
    FAILED=$((FAILED+1))
}

output() # test_name text
{
    echo -n "QDEBUG : ${TESTNAME}::$1() "
    shift
    echo $@
}

run_command() # test_name command
{
    test=$1
    shift
    output=$("$@" 2>&1)
    returned=$?
    ORIGIFS=$IFS
    IFS=`printf "\n\b"`
    for line in $output
    do
        output $test $line
    done
    IFS=$ORIGIFS
    [ "$returned" -eq 0 ] && pass "$test" || fail "$test"
}


echo "********* Start testing of ${TESTNAME} *********"
echo "Config: Using QTest library ${QTVERSION}, Qt ${QTVERSION}"
pass "initTestCase"

# -------- actual testing goes here --------

run_command "runPrinting" ../../gui/fwbuilder -f test.fwb -P test
run_command "fileExists" ls print.pdf

# --------- end of actual testing ---------

rm -f print.pdf >/dev/null 2>&1

pass "cleanupTestCase"
echo "Totals: ${PASSED} passed, ${FAILED} failed, ${SKIPPED} skipped"
echo "********* Finished testing of ${TESTNAME} *********"
[ "${FAILED}" -eq 0 ] && exit 0 || exit 1
