#!/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

PROJECT_HOME=${PROJECT_HOME:-$(dirname "$0")/../../}
PROJECT_HOME=$(cd $PROJECT_HOME || return; pwd)
DOWNLOAD_PATH=${PROJECT_HOME}/deps
DEP_LIB_DIR=./lib
DEP_TMP_DIR=./tmp

function extract_deps_so()
{
    echo "begin to extract .run file ........."
    chmod +x ./${DRIVER_RUN_NAME}
    tar -xvf ${DEV_TOOLS_PACKAGE}
    chmod -R +x ${DEV_TOOLS_PACKAGE_NAME}
    [ -n "${DEP_TMP_DIR}" ] && rm -rf "${DEP_TMP_DIR}"
    ./${DRIVER_RUN_NAME} --noexec --extract=${DEP_TMP_DIR}/driver
    ./${DEV_TOOLS_PACKAGE_NAME}/${COMPILER_RUN_NAME} --noexec --extract=${DEP_TMP_DIR}/compiler
    ./${DEV_TOOLS_PACKAGE_NAME}/${RUNTIME_RUN_NAME} --noexec --extract=${DEP_TMP_DIR}/runtime
    ./${DEV_TOOLS_PACKAGE_NAME}/${OPP_RUN_NAME} --noexec --extract=${DEP_TMP_DIR}/opp
}

function extract_deps_so_community()
{
    echo "begin to extract .run file ........."
    chmod +x ./${DRIVER_RUN_NAME_C}
    chmod +x ./${PACKAGE_NAME_C}
    [ -n "${DEP_TMP_DIR}" ] && rm -rf "${DEP_TMP_DIR}"
    ./${DRIVER_RUN_NAME_C} --noexec --extract=${DEP_TMP_DIR}/driver
    ./${PACKAGE_NAME_C} --noexec --extract=${DEP_TMP_DIR}/Packages_tmp
    ${DEP_TMP_DIR}/Packages_tmp/run_package/${COMPILER_RUN_NAME_C} --noexec --extract=${DEP_TMP_DIR}/compiler
    ${DEP_TMP_DIR}/Packages_tmp/run_package/${RUNTIME_RUN_NAME_C} --noexec --extract=${DEP_TMP_DIR}/runtime
    ${DEP_TMP_DIR}/Packages_tmp/run_package/${OPP_RUN_NAME_C} --noexec --extract=${DEP_TMP_DIR}/opp
}

function copy_so_to_target_dir()
{
    mkdir -p $DEP_LIB_DIR
    mv ${DEP_TMP_DIR}/driver/driver $DEP_LIB_DIR/driver
    mv ${DEP_TMP_DIR}/compiler/compiler $DEP_LIB_DIR/compiler
    mv ${DEP_TMP_DIR}/runtime/runtime $DEP_LIB_DIR/runtime
    mv ${DEP_TMP_DIR}/opp/opp $DEP_LIB_DIR/opp
}

function clear_libs()
{
    [ -n "${DOWNLOAD_PATH}" ] && rm -rf "${DOWNLOAD_PATH}"
}

function download_runs()
{
    source scripts/update/deps_config.sh
    echo "begin to download .run file ........."
    clear_libs
    mkdir -p ./ ${DOWNLOAD_PATH}
    pushd "${DOWNLOAD_PATH}" >/dev/null
        cd ${DOWNLOAD_PATH} 
        wget --user=${DEP_USER} --password=${DEP_PASSWORD}  ${DRIVER_URL}
        wget --user=${DEP_USER} --password=${DEP_PASSWORD}  ${DEV_TOOLS_URL}
    popd >/dev/null
}

function download_runs_from_community()
{
    source scripts/update/deps_config_community.sh
    echo "begin to download .run file from community........."
    clear_libs
    mkdir -p ./ ${DOWNLOAD_PATH}
    pushd "${DOWNLOAD_PATH}" >/dev/null
        cd ${DOWNLOAD_PATH} 
        wget ${DRIVER_URL_C}
        wget ${PACKAGE_URL_C}
    popd >/dev/null
}

function install_deps()
{
    source scripts/update/deps_config.sh
    mkdir -p ./ ${DOWNLOAD_PATH}
    pushd "${DOWNLOAD_PATH}" >/dev/null
        cd ${DOWNLOAD_PATH}
        extract_deps_so
        copy_so_to_target_dir
    popd >/dev/null
}

function install_deps_community()
{
    source scripts/update/deps_config_community.sh
    mkdir -p ./ ${DOWNLOAD_PATH}
    pushd "${DOWNLOAD_PATH}" >/dev/null
        cd ${DOWNLOAD_PATH}
        extract_deps_so_community
        copy_so_to_target_dir
    popd >/dev/null
}


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

update dependencies of build and test

Options:
    -p, --public       Download dependencies from community
    -d, --download     Download dependencies
    -i, --install      Install dependencies
    -c, --clear        Clear dependencies 
    -h, --help
EOF

}

function parse_args(){
    parsed_args=$(getopt -a -o pdich --long public,download,install,clear,help -- "$@") || {
        help
        exit 1
    }

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

    eval set -- "$parsed_args"
    while true; do
        case "$1" in
            -p | --public)
                download_runs_from_community
                install_deps_community
                ;;
            -d | --download)
                download_runs
                ;;
            -i | --install)
                install_deps
                ;; 
            -c | --clear)
                clear_libs
                ;;
            -h | --help)
                help; exit 1;
                ;;
            --)
                shift; break;
                ;;
            *)
                help; exit 1
                ;;
        esac
        shift
    done
}

function main(){
    parse_args "$@"
}

main "$@"

set +e