#!/bin/bash

#!/bin/bash
# shellcheck disable=SC2155
if ! type "include" > /dev/null 2>&1; then
  include() {
   # echo "${1} ${2}"
    . "$(cd "$(dirname "$1")" && pwd)/${2}";
  };
fi
include "${BASH_SOURCE[0]}" ./pragma_once.bash
if ! pragma_once ${BASH_SOURCE[0]}; then return 0; fi


include "${BASH_SOURCE[0]}" ./attach/export.bash
include "${BASH_SOURCE[0]}" ./patch/export.bash
include "${BASH_SOURCE[0]}" ./libc/export.bash
include "${BASH_SOURCE[0]}" ./base/export.bash
# shellcheck source=./attach/export.bash
# shellcheck source=./patch/export.bash
# shellcheck source=./libc/export.bash
# shellcheck source=./base/export.bash




install_win_mingw_run_time() {
  local install_prefix="$1"
  local target="x86_64-w64-mingw32"

  check_directory "${install_prefix}"

  log_info "Copying MinGW runtime DLLs"
  log_info "Source: /usr/${target}/lib"
  log_info "Dest  : ${install_prefix}"

  mkdir -p "${install_prefix}"

  find "/usr/${target}" -name "*.dll" | while read -r f; do
    cp -a "$f" "${install_prefix}/"
    log_info "file: ${f} "
  done

  find "/usr/lib/gcc/${target}" -path "*-posix/*.dll" | while read -r f; do
    cp -a "$f" "${install_prefix}/"
    log_info "file: ${f}"
  done


  log_info "Done!"
}


function safe_llvm_strip {
  # Check if a directory parameter is provided
  if [[ -z "$1" ]]; then
    echo "No directory specified. Exiting."
    return 1
  fi

  # Directory parameter
  local dir="$1"

  # Find all regular files in the specified directory and subdirectories
  find "$dir" -type f | while read -r file; do
    # Attempt to strip the file
    llvm-strip --strip-unneeded "$file" 2>/dev/null
  done
}

echo "工作目录： $(pwd)"

build_kylin_ver(){
  local ver=${1}
  local ver2=${2}
  local toolchain_name
  local host="x86_64-w64-mingw32"
  cacl_toolchain_name_canadian toolchain_name "${host}" "${ver}"
  local toolchain_root="${gev_canadian_dir}/${toolchain_name}"
  # 编译交叉编译工具
  attach_env_local_cross "${ver}"
  # 解压python库
  local pkg_root="/ae/pkg_mingw-w64-x86_64"
  mkdir -p "${toolchain_root}"



  # --debug-trycompile --debug-output  -DCMAKE_TRY_COMPILE_NO_CLEANUP
  # 工具链运行的时候居然也需要 zlib1.dll  但是没重装系统前没发现
  local python_pkg="${pkg_root}/mingw-w64-x86_64-python-3.12.12-1-any.pkg.tar.zst"
  tar --zstd -xf "${python_pkg}" -C "${toolchain_root}"  --strip-components=1 mingw64
  local zlib_pkg="${pkg_root}/mingw-w64-x86_64-zlib-1.3.2-2-any.pkg.tar.zst"
  tar --zstd -xf "${zlib_pkg}" -C "${toolchain_root}"  --strip-components=1 mingw64

  local script_text
  local script_path="${toolchain_root}/configure_python.bash"
  # 生成configure 脚本



  gen_python_pth "${toolchain_root}/lib/python3.12/site-packages/myenv.pth" \
    ".." \
    "../lib-dynload"


  generate_case_dispatch_script_string script_text \
    '--includes|--cflags' "-I ${toolchain_root}/include/python3.12" \
    '--ldflags|--libs' "${toolchain_root}/lib/libpython3.12.dll.a" \
    '--exec-prefix' "${toolchain_root}"
  printf '%s' "$script_text" > "${script_path}"
  chmod 777 "${script_path}"


  # 后就
  bge_binutils_gdb_suffix_opts=" --with-python=${script_path}  --enable-targets=all --disable-sim"
  attach_env_canadian_stage3 "${host}" "${ver}"
  bge_binutils_gdb_suffix_opts=""



  # 安装mingw 运行时库
  install_win_mingw_run_time "${toolchain_root}/bin"
  #install_win_mingw_run_time "${toolchain_root}/libexec/gcc/${target}/${ver2}"

  # 非递归复制
  local libexec_runtime_dir="${toolchain_root}/libexec/gcc/${target}/${ver2}"
  check_directory_var libexec_runtime_dir
  cp "${toolchain_root}/bin/"*.dll "${libexec_runtime_dir}"


  local target_bin="${toolchain_root}/${target}/bin"
  check_directory_var target_bin
  cp "${toolchain_root}/bin/"*.dll "${target_bin}"
}



build_arm_kylin(){
  name="win_arm_kylin_414_1224"
  local target="aarch64-linux-gnu"
  local sysroot="/ae/sysroots/arm_kylin"
  init_env "${name}" "${sysroot}" "${target}"
  gev_cur_toolchain_name="arm_kylin"
  local root="${gev_target_sysroot}"
  check_and_create_dir_var root
  patch_system_qt5  "${root}"  "${root}/usr/lib/aarch64-linux-gnu/cmake" "/ae/sysroots/win_qt_host_tool/5.14.2/msvc2017_64/qt5/bin"
  build_kylin_ver "releases/gcc-9.3.0" "9.3.0"
  #build_kylin_ver "releases/gcc-15.2.0" "15.2.0"
  compress_directory_tar_gz "/ae/env/${name}" "/ae/env"  "${name}.tar.gz"
}





#build_arm_kylin



build_local_gcc() {
  name="local_ubuntu2004_15_10_527_1412"
  local target="x86_64-linux-gnu"
  local sysroot="/ae/sysroots/ubuntu2004"
  local ver="releases/gcc-15.1.0"
  gev_cur_toolchain_name="local_gcc"
  init_env "${name}" "${sysroot}" "${target}"
  attach_env_local_cross  "${ver}"
  compress_directory_tar_gz "/ae/env/${name}" "/ae/env"  "${name}.tar.gz"
}

build_ubuntu_2004() {
  # 填写 15.1.0 9.3.0 这种
  local name=$1
  local inter_ver=$2
  local local_gcc_toolchain_dir=$3
  name="${name}_${inter_ver}"

  local target="x86_64-linux-gnu"
  local sysroot="/ae/sysroots/debootstrap_ubuntu_2004"
  local ver="releases/gcc-${inter_ver}"
  local ver2="${inter_ver}"
  init_env "${name}" "${sysroot}" "${target}"
  gev_cur_toolchain_name="ubuntu_2004_${target}_${inter_ver}"


  if [ -z "$local_gcc_toolchain_dir" ]; then
    attach_env_local_cross  "${ver}"
  fi

  local host="x86_64-w64-mingw32"
  local toolchain_name
  cacl_toolchain_name_canadian toolchain_name "${host}" "${ver}"
  local toolchain_root="${gev_canadian_dir}/${toolchain_name}"

  load_toolchain "${local_gcc_toolchain_dir}" "${target}"
  attach_env_canadian_stage3 "${host}" "${ver}"
  local pkg_root="/ae/pkg_mingw-w64-x86_64"
  local zlib_pkg
  zlib_pkg="$(download_msys2_pkg \
    "mingw64" \
    "mingw-w64-x86_64-zlib-1.3.2-2-any.pkg.tar.zst" \
    "${pkg_root}" \
    "9e75842a070ba648e986e12424e1c92c9d7d77200e85f6a34eeb600819f2e694"
  )"
  install_win_mingw_run_time "${toolchain_root}/bin"
  local libexec_runtime_dir="${toolchain_root}/libexec/gcc/${target}/${ver2}"
  check_directory_var libexec_runtime_dir
  cp "${toolchain_root}/bin/"*.dll "${libexec_runtime_dir}"
  local target_bin="${toolchain_root}/${target}/bin"
  check_directory_var target_bin
  cp "${toolchain_root}/bin/"*.dll "${target_bin}"
  compress_directory_tar_gz "/ae/env/${name}" "/ae/env"  "${name}.tar.gz"
}






#sudo chmod -R 777 /aenv/test_sysroot
#compress_directory_tar_gz "/aenv/test_sysroot" "/aenv"  "aaa.tar.gz"



build_ubuntu_2004  "win_ubuntu2004_528_1457" "15.1.0"  "/ae/env/local_ubuntu2004_15_10_527_1412/releases_gcc-15.1.0_x86_64-linux-gnu"














echo "执行完毕 !!!"

# linux 内核模块构建
# https://docs.kernel.org/kbuild/modules.html