#!/bin/bash
PATCH_FILE=$PWD/qt5-euro.patch
WORK=$HOME/coco
QT5=$WORK/qt5
QT5BASE=$QT5/qtbase
QT5QUICK=$QT5/qtquick3d
QT5DECLARATIVE=$QT5/qtdeclarative
QT5REF=$QT5/../qt5_lib_ref/
PROCESSORS=$(cat /proc/cpuinfo | grep processor | wc -l)

if [ ! -e "$PATCH_FILE" ]
then
    echo "Error: patch file $PATCH_FILE does not exists"
    exit
fi

function cleanup()
{
    cd $QT5 && git clean -xfd
    cd $QT5 && git submodule foreach --recursive 'git clean -dfx'
}

function checkout()
{
     if [ ! -e "$QT5" ]
     then
         cd $WORK
         git clone git://gitorious.org/qt/qt5.git
         cd $QT5 || exit
         ./init-repository -f
     fi
     cd $QT5 || exit
     git pull
     git submodule update
}

function configure_build_and_run_tests()
{
    cd $QT5 || exit

    ./configure -no-qpa-platform-guard -platform  linux-g++-64 -developer-build \
                -no-gtkstyle -no-pch -opensource -testcocoon \
                -confirm-license -prefix $QT5BASE || exit
    cd $QT5BASE
    make -j$PROCESSORS

    # executing test suite: -k is necessary to not stop at the first test
    make check -k
}

function process_csexe()
{
    CSEXE="$1"
    CSMES=$(dirname "$CSEXE")/$(basename "$CSEXE" .csexe).csmes
    CSEXE="$CSEXE"
    CSMES="$CSMES"
    if [ -e "$CSMES" ]
    then
        echo -n "Generating Unit Test Database $CSMES ... "
        cmcsexeimport -m "$CSMES" -e "$CSEXE" -t "unittest" -p merge
        echo "done"
    fi
    rm "$CSEXE"
}

function merge_unit_tests()
{
    QTLIB="$1"
    QTLIBTMP="$1".tmp
    UNITTESTS=$(find . -name "tst_*.csmes")
    QTLIB="$QTLIB"
    echo -n "Importing Unit Tests Result in $QTLIB ... "
    cmmerge -o "$QTLIBTMP"  -i "$QTLIB" $UNITTESTS
    mv -f "$QTLIBTMP" "$QTLIB"
    echo "done"
}

function collect_coverage_data_for_each_qt_library()
{
    cd $QT5BASE || exit

    # Process all execution reports of all unit tests
    find . -name "tst_*.csexe" | while read LINE
    do
        process_csexe "$LINE"
    done

    # Merge the result of the unit tests into the Qt libraries
    for QTLIB in lib/lib*.csmes
    do
        merge_unit_tests "$QTLIB"
    done
}

mkdir -p "$WORK"
rm -rf $QT5REF

checkout
cd $QT5BASE && git checkout -f

# compile
cleanup
configure_build_and_run_tests
collect_coverage_data_for_each_qt_library

# copy the instrumentation of the reference version of Qt
cd $QT5BASE
mkdir -p $QT5REF
mv $QT5BASE/lib/*.csmes $QT5REF/


# contribution
if [ ! -e "$PATCH_FILE" ]
then
    echo "$PATCH_FILE : file not existing"
    exit
fi
cd $QT5BASE || exit
cat "$PATCH_FILE" | patch -p1

# compiling, executing the suite after applying the changes
cleanup
configure_build_and_run_tests
collect_coverage_data_for_each_qt_library

# Code coverage report of the patch
cmreport -m "$QT5BASE/lib/libQtCore.so.5.0.0.csmes" -s '.*' --html="$WORK/euro_sym.html" \
         --toc --title="Euro symbol support" \
         --source=all --method=all --global=all \
         -bargraph --source-sort=coverage --method-sort=coverage \
         -mr "$QT5REF/libQtCore.so.5.0.0.csmes" --execution=all

# generate statistics for the whole Qt code
cd $QT5BASE
for QTLIB in $QT5REF/lib*.csmes
do
    HTML=$WORK/$(basename "$QTLIB" .csmes).html
    TTTLE=$(basename "$QTLIB" .csmes)
    cmreport -m "$QTLIB" -s '.*' --html="$HTML" --toc --title="$TITLE" \
             --source=all --method=all --global=all --dead-code \
             -bargraph --source-sort=coverage --method-sort=coverage
done