#!/bin/sh

# This script helps testig OOo installation using the ooqatesttool,
# http://qa.openoffice.org/qatesttool/index.html

# IMPORTANT: This is an initial version. I plan to integrate it better with
# ooo-build, see ooo-build/doc/test-ooo.txt for more details

if test `uname -o` = "Cygwin" ; then
    export killall="/usr/bin/PsTools/pskill.exe"
    export testToolBin="/cygdrive/c/Program Files/OpenOffice.org 3/Basis/program/testtool.exe"
    # where the qatesttool is stored (testcases)
    export testToolRoot="c:/cygwin/home/Mick/OOo/qa/qatesttool/"
elif test `uname -i` = "i386" ; then
    export killall="killall -9"
    export testToolBin="/usr/lib/ooo3/basis3.1/program/testtool.bin"
    export testToolRoot="/home/mick/OOo/qa/qatesttool"
else
    export killall="killall -9"
    export testToolBin="/opt/ooo-dev/basis3.1/program/testtool.bin"
    export testToolRoot="/home/mick/OOo/qa/qatesttool"
fi

# all tests will be skipped until this script name is found
# define empty string to do not skip any test
SKIP_TO_TEST=
#SKIP_TO_TEST=writer/loadsave/w_imp_bin.bas

# helper scripts
export testToolExitOfficeBas="$testToolRoot/global/tools/closeoffice.bas"
export testToolResetOfficeBas="$testToolRoot/global/tools/resetoffice.bas"

usage()
{
    echo "This script start the qatesttool scripts from the given list"
    echo
    echo "Usage: ${0##*/} test.list [test.blacklist]"
    echo
    echo "	test.list      - list of test scripts to start"
    echo "	test.blacklist - list of test scripts to skip"
    echo 
    echo "	Both files include list of paths to the test scripts, one path"
    echo "	per line. They can also include comments prefixed by #"
    echo 
    echo "Example of the file.list:"
    echo
    echo "	# Tests for OOo-2.0.3"
    echo "	framework/first/first.bas"
    echo "	framework/first/topten.bas"
    echo "	framework/level1/f_lvl1_loadsave.bas"
}

if test -z "$1" -o "$1" = "--help" -o $# -gt 2 ; then
    usage && exit 1;
fi

if ! which dos2unix >/dev/null 2>&1 ; then
    echo "Error: dos2unix utility is not installed"
    exit 1;
fi

# list of test to be started
testList=
if  test ! -f "$1" ; then
    echo "Error: cannot read \"$1\", try --help" && exit 1;
else
    testList=`cat "$1" | dos2unix | sed "s|\#.*$||"`
fi

# list of tests to be skipped
testBlackList=
if test -n "$2" ; then
    if test ! -f "$2" ; then
	echo "Error: cannot read \"$2\", try --help" && exit 1;
    else
	testBlackList=`cat "$2" | dos2unix | sed "s|\#.*$||"`
    fi
fi
    
echo "Switching to en_US.UTF-8 locales!!!"
export LC_ALL=en_US.UTF-8

echo "Exporting OOO_FORCE_SYSALLOC=1"
export OOO_FORCE_SYSALLOC=1
echo "Exporting MALLOC_CHECK_=2"
export MALLOC_CHECK_=2

is_blacklisted()
{
    for t in $testBlackList ; do
	test "$1" = "$t" && return 0
    done
    return 1
}

run_test()
{
    test="$1"

    "$testToolBin" -run "$test"
    sleep 5
    $killall testool.exe
    $killall testool.bin
    $killall soffice.exe
    $killall soffice.bin
    sleep 2
    "$testToolBin" -run "$testToolResetOfficeBas"
    sleep 5
    $killall testool.exe
    $killall testool.bin
    $killall soffice.exe
    $killall soffice.bin
    sleep 2  
    rm ~/OOo/qa/logs/resetoffice.res
    "$testToolBin" -run "$testToolExitOfficeBas"
    sleep 5
    $killall testool.exe
    $killall testool.bin
    $killall soffice.exe
    $killall soffice.bin
    sleep 2
    rm ~/OOo/qa/logs/closeoffice.res
}

# will we skip any test?
test -n "$SKIP_TO_TEST" && skip_tests=true || skip_tests=false

# kill any runnign testool and OOo
$killall testool.bin
$killall testool.exe
$killall soffice.bin
$killall soffice.exe

# reset OOo configuration
run_test "$testToolResetOfficeBas"

for test in $testList ; do
    if is_blacklisted $test ; then
	echo "Skipping blacklisted test $test..."
	continue;
    fi

    test "$test" = "$SKIP_TO_TEST" && skip_tests=false

    if $skip_tests ; then
	echo "Skipping test $test..."
    else
	echo "Starting test $test..."
	run_test "$testToolRoot/$test"
    fi
done
