#!/bin/bash
# -----------------------------------------------------------------------------------------------------------
# Copyright (c) 2026 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.
# -----------------------------------------------------------------------------------------------------------

SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd)
SAMPLE_NAME="tc_ge_irrun_open_api_0014"
# 默认使用 CANN 默认安装路径, 可通过环境变量 ASCEND_HOME_PATH 指定自己的 Ascend 安装目录
ASCEND_INSTALL_PATH="${ASCEND_HOME_PATH:-/usr/local/Ascend/cann}"

if [ ! -f "${ASCEND_INSTALL_PATH}/set_env.sh" ]; then
    echo "Error: '${ASCEND_INSTALL_PATH}/set_env.sh' not found."
    echo "Please install CANN Toolkit first, or set ASCEND_HOME_PATH to your Ascend install directory."
    exit 1
fi
source "${ASCEND_INSTALL_PATH}/set_env.sh"

cd "${SCRIPT_DIR}/${SAMPLE_NAME}" || exit 1
make clean
if ! make; then
    echo "Error: build sample '${SAMPLE_NAME}' failed, please check the compiler output above"
    echo "and the CANN install path '${ASCEND_INSTALL_PATH}'."
    exit 1
fi

cd "${SCRIPT_DIR}" || exit 1
if [ ! -f "./${SAMPLE_NAME}.bin" ]; then
    echo "Error: './${SAMPLE_NAME}.bin' was not generated by the build."
    exit 1
fi

./"${SAMPLE_NAME}.bin"
ret=$?
if [ ${ret} -ne 0 ]; then
    echo "Error: run sample '${SAMPLE_NAME}' failed with exit code ${ret}."
    echo "Please check the log above and the plog logs under ~/ascend/log/ for details."
fi
exit ${ret}