#!/bin/bash
if ! type "include" > /dev/null 2>&1; then
  include() {
  . "$(cd "$(dirname "$1")" && pwd)/${2}";
  };
fi

include "${BASH_SOURCE[0]}" ../pragma_once.bash


if ! pragma_once ${BASH_SOURCE[0]}; then return 0;
  source ./../pragma_once.bash
  source ./container.bash
fi

include "${BASH_SOURCE[0]}" container.bash





print_exports() {
    echo "All exported environment variables:"
    export -p  # 使用 `export -p` 显示所有已导出的变量
}



load_library() {
  local library_list_str=$1
  local previous_PATH=${PATH}
  local previous_LD_LIBRARY_PATH=${LD_LIBRARY_PATH}
  local library_path_list_str=$LD_LIBRARY_PATH
  local bin_path_list_str=$PATH
  local library_list library lib_name version
  IFS=':' read -r -a library_list <<<"$library_list_str"
  for library in "${library_list[@]}"; do
    IFS='-' read -r lib_name version <<<"$library" # 分割库名和版本
    local library_path="${_install_}/${lib_name}/${version}"
    if [ ! -d "$library_path" ]; then
      library_path="${_install_}/${library}" # 尝试原始库路径
    fi
    if [ ! -d "$library_path" ]; then
      echo "load_library  ${library_path} not exist!"
      exit 1
    fi
    local lib_dir bin_dir
    for lib_dir in "lib64" "lib"; do
      local cur_path="${library_path}/${lib_dir}"
      if [ -d "${cur_path}" ]; then
        list_remove_element_if_exist "${library_path_list_str}" "${cur_path}"
        list_prepend_element "${_ret_}" "${cur_path}"
        library_path_list_str="${_ret_}"
      fi
    done
    for bin_dir in "bin" "libexec" "include"; do
      local cur_path="${library_path}/${bin_dir}"
      if [ -d "${cur_path}" ]; then
        list_remove_element_if_exist "${bin_path_list_str}" "${cur_path}"
        list_prepend_element "${_ret_}" "${cur_path}"
        bin_path_list_str="${_ret_}"
      fi
    done
  done

  export LD_LIBRARY_PATH="${library_path_list_str}"
  export PATH="${bin_path_list_str}"

  echo -e "===============[start] load_library加载环境变量:==============="
  echo -e "[ param ]： ${library_list_str}"
  echo -e "[ old LD_LIBRARY_PATH ]："
  IFS=":" read -r -a paths <<<"$previous_LD_LIBRARY_PATH"
  for path in "${paths[@]}"; do
    echo "    $path"
  done
  echo -e "[ old PATH ]："
  IFS=":" read -r -a paths <<<"$previous_PATH"
  for path in "${paths[@]}"; do
    echo "    $path"
  done
  #    echo -e "[LD_LIBRARY_PATH]： ${LD_LIBRARY_PATH}"
  #    echo -e "[PATH]： ${PATH}"

  echo -e "[ LD_LIBRARY_PATH ]："
  IFS=":" read -r -a paths <<<"$LD_LIBRARY_PATH"
  for path in "${paths[@]}"; do
    echo "    $path"
  done
  echo -e "[ PATH ]："
  IFS=":" read -r -a paths <<<"$PATH"
  for path in "${paths[@]}"; do
    echo "    $path"
  done
  echo -e "===============[ended] load_library加载环境变量:==============="
}


return 0