#!/bin/bash
# -----------------------------------------------------------------------------------------------------------
# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# This program is free software, you can redistribute it and/or modify it under the terms and conditions of 
# CANN Open Software License Agreement Version 2.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.
# -----------------------------------------------------------------------------------------------------------

set -e


function help(){
    cat <<-EOF
Usage: ge cov [OPTIONS]

Options:
    -a, --all          Full coverage
    -i, --increment    Increment coverage
    -d, --directory    Coverage of directory
    -h, --help 
EOF

}

PROJECT_HOME=${PROJECT_HOME:-$(dirname "$0")/../../}
PROJECT_HOME=$(cd $PROJECT_HOME || return; pwd)

source ${PROJECT_HOME}/scripts/support_multiple_versions_of_lcov.sh

ALL_COV_GEN_PATH=${PROJECT_HOME}/cov/all
DIFF_FILE_PATH=${PROJECT_HOME}/cov/diff
DIFF_FILE_NAME=${DIFF_FILE_PATH}/inc_change_diff.txt

function process_diff_format(){
    sed -i "s/--- a/--- \/code\/Turing\/graphEngine/g" ${DIFF_FILE_NAME}
    sed -i "s/+++ b/+++ \/code\/Turing\/graphEngine/g" ${DIFF_FILE_NAME}
}


function add_cov_generate(){
    addlcov --diff ${ALL_COV_GEN_PATH}/coverage.info ${DIFF_FILE_NAME} -o ${PROJECT_HOME}/cov/diff/inc_coverage.info
}

function gen_add_cov_html(){
    GENHTML_IGNORE_ERRORS=$(get_genhtml_ignore_errors)
    genhtml --prefix ${PROJECT_HOME} -o ${PROJECT_HOME}/cov/diff/html ${PROJECT_HOME}/cov/diff/inc_coverage.info --legend -t CHG --no-branch-coverage --no-function-coverage ${GENHTML_IGNORE_ERRORS}
}

function increment_cov_for_directory(){
    [ -n "${DIFF_FILE_PATH}" ] && rm -rf "${DIFF_FILE_PATH}"
    mkdir -p ${DIFF_FILE_PATH}
    git diff HEAD -- $1 >>${DIFF_FILE_NAME}
    process_diff_format
    add_cov_generate
    gen_add_cov_html
}

function run_all_coverage(){
    [ -n "${ALL_COV_GEN_PATH}" ] && rm -rf ${ALL_COV_GEN_PATH}
    mkdir -p ${ALL_COV_GEN_PATH}
    pushd "${PWD}" >/dev/null
        cd ${PROJECT_HOME} 
        lcov -c -d build/tests/ge/ut/ge -d build/tests/ge/ut/common/graph/ -o ${ALL_COV_GEN_PATH}/tmp.info
        lcov -r ${ALL_COV_GEN_PATH}/tmp.info '*/output/*' '*/build/opensrc/*' '*/build/proto/*' '*/third_party/*' '*/tests/*' '/usr/local/*' '/usr/include/*' '*/metadef/*' '*/parser/*' -o ${ALL_COV_GEN_PATH}/coverage.info
        cd ${ALL_COV_GEN_PATH}
        GENHTML_IGNORE_ERRORS=$(get_genhtml_ignore_errors)
        genhtml coverage.info ${GENHTML_IGNORE_ERRORS}
    popd  >/dev/null
}

function do_coverage_run(){
    local cov_mode=$1
    local directory_dir=$2
    
    run_all_coverage

    if [ "$cov_mode" = "all" ]; then
        exit 1
    elif [ -n "$directory_dir" ]; then
        increment_cov_for_directory $directory_dir
    else 
        increment_cov_for_directory "ge"
    fi 
}

function parse_args(){
    parsed_args=$(getopt -a -o aid::h --long all,increment,directory::,help -- "$@") || {
        help
        exit 1
    }

    if [ $# -lt 1 ]; then
        run_all_coverage
        exit 1
    fi

    local cov_mode="increment"
    local directory_dir=""
    eval set -- "$parsed_args"
    while true; do
        case "$1" in
            -a | --all)
                cov_mode="all"
                ;;
            -i | --increment)
                ;;
            -d | --directory)
                directory_dir=$2
                shift
                ;;
            -h | --help)
                help; exit 1;
                ;;
            --)
                shift; break;
                ;;
            *)
                help; exit 1;
                ;;
        esac
        shift 
    done
    do_coverage_run $cov_mode $directory_dir
}

function main(){
    parse_args "$@"
}

main "$@"

set +e