#!/bin/bash
# This script is used to generate llt-cpp coverage.
# Copyright Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.

set -e
CUR_DIR=$(dirname $(readlink -f $0))
TOP_DIR=${CUR_DIR}/..
COV_DIR=${TOP_DIR}/test/build_llt/output/cpp_coverage
BUILD_DIR=${TOP_DIR}/test/build_llt

LCOV_RC="--rc lcov_branch_coverage=1 --rc geninfo_no_exception_branch=1"

if [ ! -d ${COV_DIR} ] ; then
    mkdir -p ${COV_DIR}
fi
#----------------------------------------------------------
# coverage function
generate_coverage(){
    echo "********************** Generate $2 Coverage Start.************************"
    lcov -c -d $1 -o ${COV_DIR}/lcov_$2.info $LCOV_RC
    lcov -r ${COV_DIR}/lcov_$2.info '*c++*' -o ${COV_DIR}/lcov_$2.info $LCOV_RC
    lcov -r ${COV_DIR}/lcov_$2.info '*msprof_llt*' -o ${COV_DIR}/lcov_$2.info $LCOV_RC
    lcov -r ${COV_DIR}/lcov_$2.info '*msprof_cpp*' -o ${COV_DIR}/lcov_$2.info $LCOV_RC
    lcov -r ${COV_DIR}/lcov_$2.info '*ascend_protobuf*' -o ${COV_DIR}/lcov_$2.info $LCOV_RC
    lcov -r ${COV_DIR}/lcov_$2.info '*gtest*' -o ${COV_DIR}/lcov_$2.info $LCOV_RC
    lcov -r ${COV_DIR}/lcov_$2.info '*opensource*' -o ${COV_DIR}/lcov_$2.info $LCOV_RC
    lcov -r ${COV_DIR}/lcov_$2.info '*proto*' -o ${COV_DIR}/lcov_$2.info $LCOV_RC
    lcov -r ${COV_DIR}/lcov_$2.info '*prof_params.h*' -o ${COV_DIR}/lcov_$2.info $LCOV_RC
    lcov -r ${COV_DIR}/lcov_$2.info '*message.h*' -o ${COV_DIR}/lcov_$2.info $LCOV_RC
    echo "********************** Generate $2 Coverage Stop.*************************"
}
#----------------------------------------------------------
test_obj=(
    entities_utest
    utils_utest
    device_init_utest
    dfx_utest
    association_utest
    viewer_utest
    host_worker_utest
    host_parser_utest
    device_parser_utest
    device_modeling_utest
    device_persistence_utest
    data_process_utest
    export_assemble_utest
)

str_test=""
for test in ${test_obj[@]} ; do
    str_test=${str_test}"-a ${COV_DIR}/lcov_${test}.info "
    test_dir=`find ${BUILD_DIR} -name "${test}.dir"`
    target_dir=`ls -F ${test_dir} | grep "/$" | grep -v "test" | grep -v "__"`
    echo "${target_dir} ${test_dir} ${str_test}"
    generate_coverage ${test_dir}/${target_dir} ${test}
done

echo "${str_test}"
lcov ${str_test} -o ${COV_DIR}/ut_report.info $LCOV_RC
genhtml ${COV_DIR}/ut_report.info -o ${COV_DIR}/result --branch-coverage
echo "report: ${COV_DIR}"

if [[ -n "$1" && "$1" == "diff" ]];then
  targetBranch=${targetBranch:-master}
  lcov_cobertura ${COV_DIR}/ut_report.info -o ${COV_DIR}/coverage.xml
  diff-cover ${COV_DIR}/coverage.xml --html-report ${COV_DIR}/result/ut_incremental_coverage_report.html --compare-branch="origin/${targetBranch}"  --fail-under=80
fi