#!/bin/bash
# get real path of parents' dir of this file
CUR_DIR=$(dirname $(readlink -f $0))
TOP_DIR=${CUR_DIR}/..

# store product
PREFIX_DIR=${TOP_DIR}/build/prefix
MSPROF_TEMP_DIR=${TOP_DIR}/../output/msprof

RUN_SCRIPT_DIR=${TOP_DIR}/scripts/run_script
FILTER_PARAM_SCRIPT=${RUN_SCRIPT_DIR}/help.conf
MAIN_SCRIPT=main.sh
INSTALL_SCRIPT=install.sh
UTILS_SCRIPT=utils.sh
CANN_UNINSTALL=cann_uninstall.sh
UNINSTALL=uninstall.sh

# script for spc
MAIN_SPC=main_spc.sh
FILTER_PARAM_SCRIPT_SPC=${RUN_SCRIPT_DIR}/help_spc.conf
BACKUP=backup.sh
ROLLBACK_PRECHECK=rollback_precheck.sh
ROLLBACK_SPC=rollback_spc.sh
UNINSTALL_SPC=uninstall_spc.sh
COMMON_ROLLBACK=rollback.sh
COMMON_UNINSTALL=uninstall.sh

version="unknwon"
package_type="unknwon"

PKG_LIMIT_SIZE=524288000 # 500M

function parse_script_args() {
    while true; do
        if [ "$1" = "" ]; then
            break
        fi
        case "$1" in
        version_dir=*)
            version=${1#version_dir=}
            shift
            continue
            ;;
        Patch)
            package_type="Patch"
            shift
            continue
            ;;
        *)
            echo "[ERROR]" "Input option is invalid"
            exit 1
            ;;
        esac
    done
}

# create temp dir for product
function create_temp_dir() {
    local temp_dir=${1}
    if [ -d ${temp_dir} ]; then
        rm -rf ${temp_dir}
    fi
    mkdir -p ${temp_dir}
    local collector_dir="${PREFIX_DIR}/collector"
    local mspti_dir="${PREFIX_DIR}/mspti"
    local profcommon_dir=${PREFIX_DIR}/profcommon

    # if we want to change product, we also need to change rollback_precheck
    cp ${collector_dir}/lib/libmsprofiler.so ${temp_dir}
    cp -r ${collector_dir}/stub ${temp_dir}
    cp ${collector_dir}/bin/msprof ${temp_dir}
    cp ${mspti_dir}/libmspti.so ${temp_dir}
    cp ${profcommon_dir}/libprof_common.so ${temp_dir}
    cp ${TOP_DIR}/build/build/dist/msprof-0.0.1-py3-none-any.whl ${temp_dir}
    cp ${TOP_DIR}/build/build/dist/mspti-0.0.1-py3-none-any.whl ${temp_dir}
    cp ${TOP_DIR}/collector/inc/external/acl/acl_prof.h ${temp_dir}
    cp ${TOP_DIR}/collector/inc/external/ge/ge_prof.h ${temp_dir}
    cp ${TOP_DIR}/mspti/external/mspti.h ${temp_dir}
    cp ${TOP_DIR}/mspti/external/mspti_activity.h ${temp_dir}
    cp ${TOP_DIR}/mspti/external/mspti_callback.h ${temp_dir}
    cp ${TOP_DIR}/mspti/external/mspti_cbid.h ${temp_dir}
    cp ${TOP_DIR}/mspti/external/mspti_result.h ${temp_dir}
    cp -r ${TOP_DIR}/mspti/samples ${temp_dir}
    mkdir -p ${temp_dir}/mstx_samples
    cp -r ${TOP_DIR}/samples ${temp_dir}/mstx_samples
    copy_script ${INSTALL_SCRIPT} ${temp_dir}
    copy_script ${UTILS_SCRIPT} ${temp_dir}
}

# copy script
function copy_script() {
    local script_name=${1}
    local temp_dir=${2}

    if [ -f "${temp_dir}/${script_name}" ]; then
        rm -f "${temp_dir}/${script_name}"
    fi

    cp ${RUN_SCRIPT_DIR}/${script_name} ${temp_dir}/${script_name}
    chmod 500 "${temp_dir}/${script_name}"
}

# build python whl
function build_python_whl() {
  cd ${TOP_DIR}/build/build
  python3  ${TOP_DIR}/build/setup.py bdist_wheel --python-tag=py3 --py-limited-api=cp37
  python3  ${TOP_DIR}/build/setup_mspti.py bdist_wheel --python-tag=py3 --py-limited-api=cp37
  rm -rf ${TOP_DIR}/msprof.egg-info
  cd -
}

function sed_param() {
    local main_script=${1}
    sed -i "2i VERSION=$version" "${RUN_SCRIPT_DIR}/${main_script}"
    sed -i "2i package_arch=$(arch)" "${RUN_SCRIPT_DIR}/${main_script}"

    if [ "${main_script}" = "${MAIN_SCRIPT}" ]; then
        sed -i "2i package_arch=$(arch)" "${RUN_SCRIPT_DIR}/${UNINSTALL}"
    fi
}

function delete_sed_param() {
    local main_script=${1}
    sed -i "2d" "${RUN_SCRIPT_DIR}/${main_script}"
    sed -i "2d" "${RUN_SCRIPT_DIR}/${main_script}"

    if [ "${main_script}" = "${MAIN_SCRIPT}" ]; then
        sed -i "2d" "${RUN_SCRIPT_DIR}/${UNINSTALL}"
    fi
}

function sed_main_param() {
	local main_script=${1}
	local filer=${2}
	sed_param ${main_script}
	build_python_whl
	create_temp_dir ${MSPROF_TEMP_DIR}
	check_file_exist ${MSPROF_TEMP_DIR}
	delete_sed_param ${main_script}
}

check_file_exist() {
  local temp_dir=${1}
  check_package ${temp_dir}/acl_prof.h ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/ge_prof.h ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/msprof-0.0.1-py3-none-any.whl ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/mspti-0.0.1-py3-none-any.whl ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/msprof ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/libmsprofiler.so ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/stub ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/${INSTALL_SCRIPT} ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/${UTILS_SCRIPT} ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/libmspti.so ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/mspti.h ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/mspti_activity.h ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/mspti_callback.h ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/mspti_cbid.h ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/mspti_result.h ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/samples ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/mstx_samples/samples ${PKG_LIMIT_SIZE}
  check_package ${temp_dir}/libprof_common.so ${PKG_LIMIT_SIZE}
}

function check_package() {
    local _path="$1"
    local _limit_size=$2
    echo "check ${_path} exists"
    # 检查路径是否存在
    if [ ! -e "${_path}" ]; then
        echo "${_path} does not exist."
        exit 1
    fi

    # 检查路径是否为文件
    if [ -f "${_path}" ]; then
        local _file_size=$(stat -c%s "${_path}")
        # 检查文件大小是否超过限制
        if [ "${_file_size}" -gt "${_limit_size}" ] || [ "${_file_size}" -eq 0 ]; then
            echo "package size exceeds limit:${_limit_size}"
            exit 1
        fi
    fi
}

parse_script_args $*

if [ "${package_type}" = "Patch" ];
    then
        sed_main_param ${MAIN_SPC} ${FILTER_PARAM_SCRIPT_SPC}
    else
        sed_main_param ${MAIN_SCRIPT} ${FILTER_PARAM_SCRIPT}
fi