#!/bin/sh

# Script to setup environment correctly to run desktop test suites
# Can now run all required tests, all required plus an optional module,
# or just a single selected test

BUILD_VERSION=3.2.1

# these lists should have the same testnames; one is used for messages, 
# the other is the actual list of tests to run, in the order they should run
# optional modules appear here, and are handled by special-case code
MODLIST="FDO, GTKVTS, QT3, QT4, FONTCONFIG, XML, PNG, XRENDER, FREETYPE, XFT"
# we split the tests into interactive/automatic lists so one can choose
# fdo currently first because it may be interactive
INTERACTIVE="fdo"
AUTOMATIC="gtkvts qt3 qt4 fontconfig xml png xrender freetype xft"
TESTLIST="$INTERACTIVE $AUTOMATIC"

# optional-module support prototype:
# unset XXX_enabled

if [ -n "$1" ]; then
    if [ "$1" != "-m" -a "$1" != "-?" -a "$1" != "-h" -a "$1" != "-s" -a "$1" != "-a" -a "$1" != "-i" ]; then
        echo "arg '$1' is unknown, exiting"
        exit 1
    elif [ "$1" = "-?" -o "$1" = "-h" ];then
        echo "usage:"
        echo "-?, -h    this help"
        echo "-a        run only automatic tests"
        echo "-i        run only interactive tests"
#        echo "-m        add module to full test run (QT4)"
        echo "-s        run a single module ($MODLIST)"
        exit 0
    # optional-module support prototype:
    #elif [ "$1" = "-m" ];then
    #    if [ -z "$2" ];then
    #        echo "-m requires a valid module name";
    #        echo "valid choices: XXX"
    #        exit 1
    #    else
    #        XXX_enabled="yes"
    #    fi
    elif [ "$1" = "-s" ];then
        if [ -z "$2" ];then
            echo "-s requires a valid module name";
            echo "valid choices: $MODLIST"
            exit 1
        else
            dt_module=`echo "$2" | tr '[:lower:]' '[:upper:]'`
            if [ "$dt_module" = "FONTCONFIG" ];then
                one_module="fontconfig"
            elif [ "$dt_module" = "GTKVTS" ];then
                one_module="gtkvts"
            elif [ "$dt_module" = "PNG" ];then
                one_module="png"
            elif [ "$dt_module" = "QT3" ];then
                one_module="qt3"
            elif [ "$dt_module" = "XML" ];then
                one_module="xml"
            elif [ "$dt_module" = "QT4" ];then
                one_module="qt4"
            elif [ "$dt_module" = "FDO" ];then
                one_module="fdo"
            elif [ "$dt_module" = "XRENDER" ];then
                one_module="xrender"
            elif [ "$dt_module" = "FREETYPE" ];then
                one_module="freetype"
            elif [ "$dt_module" = "XFT" ];then
                one_module="xft"
            fi
            # optional-module support prototype:
            #elif [ "$dt_module" = "XXX" ];then
            #    one_module="XXX"
            #    XXX_enabled="yes"
        fi
    elif [ "$1" = "-a" ];then
	    TESTLIST=$AUTOMATIC
    elif [ "$1" = "-i" ];then
	    TESTLIST=$INTERACTIVE
    fi
fi

if [ ! -x /opt/lsb-tet3-lite/bin/tcc ]
then
    printf "tcc not found, aborting\n"
    exit 1
fi

TET_ROOT=`pwd`
TET_PATH=/opt/lsb-tet3-lite
MANPATH=$MANPATH:$TET_PATH/man
PATH=$TET_PATH/bin:$PATH
#fdo needs to find the lsb xdg-utils
PATH=$PATH:/opt/lsb/bin
#and we need to be sure to find pidof
PATH=$PATH:/sbin:/usr/sbin

PYTHONPATH=$TET_PATH/lib/python
export PATH MANPATH TET_ROOT PYTHONPATH

config_param()
{
    DEFAULT=`grep "^$1=" gtkvts/tetexec.cfg | cut -d= -f2`

    printf "$2 ("
    if [ -n "$DEFAULT" ]; then
        printf "$DEFAULT"
    fi
    printf ")? "
    read cmd

    if [ -z "$cmd" ]; then
        cmd=$DEFAULT
    fi

    # Write to temporary file
    echo "$1=$cmd" >> /tmp/newparam.$$
}

configure()
{
    config_param VSX_OPER "Name of person running tests"
    config_param VSX_ORG  "Organisation"
    config_param VSX_SYS "Test System"
    config_param VSX_REPORT "Show Summary Report"
    config_param XT_DISPLAYHOST "Hostname of client for X tests"
    config_param XT_DISPLAY "DISPLAY of the Xvfb (ie., :1)"
    config_param TARGET_DESKTOP "desktop environment to test (gnome/kde)"
    echo "VSX_NAME=LSB Certification Version $BUILD_VERSION" >> /tmp/newparam.$$

    mv $TET_ROOT/gtkvts/tetexec.cfg $TET_ROOT/gtkvts/tetexec.cfg.old
    mv $TET_ROOT/fontconfig/tetexec.cfg $TET_ROOT/fontconfig/tetexec.cfg.old
    mv $TET_ROOT/xrender/tetexec.cfg $TET_ROOT/xrender/tetexec.cfg.old
    mv $TET_ROOT/freetype/tetexec.cfg $TET_ROOT/freetype/tetexec.cfg.old
    cat $TET_ROOT/gtkvts/tetexec.cfg.old | $TET_ROOT/bin/editcfg.sh /tmp/newparam.$$ > $TET_ROOT/gtkvts/tetexec.cfg
    cat $TET_ROOT/fontconfig/tetexec.cfg.old | $TET_ROOT/bin/editcfg.sh /tmp/newparam.$$ > $TET_ROOT/fontconfig/tetexec.cfg
    cat $TET_ROOT/xrender/tetexec.cfg.old | $TET_ROOT/bin/editcfg.sh /tmp/newparam.$$ > $TET_ROOT/xrender/tetexec.cfg
    cat $TET_ROOT/freetype/tetexec.cfg.old | $TET_ROOT/bin/editcfg.sh /tmp/newparam.$$ > $TET_ROOT/freetype/tetexec.cfg
    rm -f /tmp/newparam.$$
}

# start xvfb if it isn't already running
startXvfb()
{
    running=`pidof Xvfb`
    if [ -z "$running" ];then
        /opt/lsb/bin/Xvfb -render -screen 0 1024x768x16 $TEST_DISPLAY -ac +bs -kb -fp $XT_FONTPATH &
        echo "Starting X Virtual Frame Buffer"; sleep 20
        export DISPLAY=$TEST_DISPLAY
        export G_SLICE=always-malloc
    fi
}

stopXvfb()
{
    running=`pidof Xvfb`
    if [ -n "$running" ];then
        # kill xvfb
        kill $running
    fi
}

# these functions kick off the various component tests

# optional-module support prototype:
#XXX()
#{
##   Example 1: use of Xvfb, tcc:
#    if [ -n "$XXX_enabled" ]; then
#        startXvfb
#        tcc -p -e -v DISPLAY=$TEST_DISPLAY XXX
#    fi
#
##   Example 2: non-tcc test without Xvfb:
#    if [ -n "$XXX_enabled" ]; then
#        ( cd XXX; ./lsb-rt-XXX )
#    fi
#}

gtkvts()
{
    startXvfb
    tcc -p -e -t 600 -v DISPLAY=$TEST_DISPLAY gtkvts
}

qt3()
{
    startXvfb
    tcc -p -e qt3
}

qt4()
{
    startXvfb
    ( cd Qt; ./lsb-rt-qt > Qt-run.log 2>&1 )
}

fontconfig()
{
    tcc -p -e fontconfig
}

xml()
{
    ( cd xml; ./lsb-rt-libxml2 )
}

png()
{
    ( cd libpng; ./lsb-rt-libpng )
}

fdo()
{
    if [ -z "$SKIP_FDO" ];then
        echo
        echo "executing fdo tests now; warning, partially interactive"
        TET_ROOT=$TET_PATH TET_SUITE_ROOT=`pwd` tcc -p -e desktop-test
        echo
        echo "desktop test finished; executing menu next"
        echo
        TET_ROOT=$TET_PATH TET_SUITE_ROOT=`pwd` tcc -p -e menu-spec
        echo
        echo "done; remaining tests should be automatic"
    fi
}

xrender()
{
    startXvfb
    tcc -p -e -v DISPLAY=$TEST_DISPLAY xrender
}

freetype()
{
    tcc -p -e freetype
}

xft()
{
    startXvfb
    tcc -p -e -v DISPLAY=$TEST_DISPLAY xft
}

# Configure the test suites
if [ ! -f .configured ]; then
    configure
    touch .configured
fi

XT_DISPLAY=`grep '^XT_DISPLAY=' gtkvts/tetexec.cfg | cut -f 2 -d=`
TEST_DISPLAY=`echo $XT_DISPLAY | grep -Eo ':([[:digit:]]+\.?)+'`
XT_FONTPATH=`grep '^XT_FONTPATH=' gtkvts/tetexec.cfg | cut -f 2 -d=`
VSX_REPORT=`grep '^VSX_REPORT=' gtkvts/tetexec.cfg | cut -f 2 -d=`
TARGET_DESKTOP=`grep '^TARGET_DESKTOP=' gtkvts/tetexec.cfg | cut -f 2 -d= | tr A-Z a-z`
# wipe any extra whitespace
TARGET_DESKTOP=`echo $TARGET_DESKTOP`

# XXX this code need not run if the FDO test is not selected
if [ "$TARGET_DESKTOP" = "gnome" ]; then
    export MENUTEST=gnome-menu-spec-test
elif [ "$TARGET_DESKTOP" = "kde" ]; then
    export MENUTEST='kbuildsycoca --menutest'
else
    echo "unknown desktop '$TARGET_DESKTOP'"
    echo "fdo tests will be skipped..."
    export MENUTEST='/bin/false'
    SKIP_FDO=1
fi
echo "TARGET is $TARGET_DESKTOP"

# XXX this code need not run if no Xvfb-using tests are selected
if [ -z "$TEST_DISPLAY" ]; then
    echo "TEST_DISPLAY is unset, XT_DISPLAY is $XT_DISPLAY"
    echo "won't be able to spawn Xvfb properly, exiting"
    exit 1
fi

# ready to run tests, set a marker so we can find journal
# files changed since the test run began
touch .start_test

if [ -n "$one_module" ];then
    # some tests run too quickly to find the journal newer than .start_test
    if [ "$one_module" = "png" -o "$one_module" = "fontconfig" ];then    
        echo "Delaying test start so we can find the journal..."   
        sleep 5
    fi
    $one_module
else
    for lsbtest in $TESTLIST ; do
        $lsbtest
    done
fi

stopXvfb

if [ -n "$VSX_REPORT" -a "$VSX_REPORT" = "y" -o "$VSX_REPORT" = "Y" ];then
    echo "Done, generating test reports:"
    echo "================================"
else
    echo "LSB desktop test is complete."
    echo "The test journals for this run can be found at:"
fi

for JOURNAL in `find $PWD -cnewer $TET_ROOT/.start_test -name 'journal*'`;do
    echo $JOURNAL
    if [ -n "$VSX_REPORT" -a "$VSX_REPORT" = "y" -o "$VSX_REPORT" = "Y" ];then
        tjreport $JOURNAL
        echo "================================"
    fi
done
