#!/bin/bash

working_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 
# coverage applies only on unit tests
unit_test_dir='build/tests/unit'
coverage_dir='build/coverage'
coverage_file='coverage.info'
 
# execute unit tests to generate coverage files
pushd ${unit_test_dir}
ctest -R unit
popd

# generate coverage info files for each unit test binary
#   + exclude stl, tests and libs
cd ${working_dir}
binary_path="${unit_test_dir}/CMakeFiles"
all_coverage_info=""
for dir in $binary_path/*.dir; do
    pushd ${dir}
    lcov --gcov-tool ${working_dir}/llvm-gcov -c -d . -o ${coverage_file}
    lcov -r ${coverage_file} '/usr/*' '*/libs/*' '*/tests/*' -o ${coverage_file}
    test_coverage_file=${working_dir}/${dir}/${coverage_file}
    if [ -f ${coverage_file} ]; then
        if [ -s ${coverage_file} ]; then
            char_no=`wc -c ${coverage_file} | awk '{print $1}'`
            if [ "${char_no}" -eq "0" ]; then
                echo "PASS: ${test_coverage_file} contains 0 chars."
                popd
                continue
            fi
        else
            echo "PASS: ${test_coverage_file} is empty."
            popd
            continue
        fi
    else
        echo "PASS: ${test_coverage_file} doesn't exist."
        popd
        continue
    fi
    all_coverage_info+=" -a ${working_dir}/${dir}/${coverage_file}"
    popd
done

# merge all info files into total coverage info and generate html
echo ${all_coverage_info}
cd ${working_dir}
mkdir -p ${coverage_dir}
pushd ${coverage_dir}
lcov ${all_coverage_info} -o ${coverage_file}
genhtml ${coverage_file}
popd

# generage coberatura xml file (compatible with Jenkins)
cd ${working_dir}
python libs/lcov-to-cobertura-xml/lcov_cobertura/lcov_cobertura.py \
    ${coverage_dir}/${coverage_file} -o ${coverage_dir}/coverage.xml