#!/bin/bash
# shellcheck disable=SC2155
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
fi
include "${BASH_SOURCE[0]}" ../base/export.bash
false && source ../base/export.bash
patch_cross_qt() {
  local SYSROOT="${1}"
  local CMAKE_DIR="${2}"
  check_directory "${SYSROOT}"
  check_directory "${CMAKE_DIR}"
  local CMAKE_CURRENT_LIST_DIR="${CMAKE_DIR}/Qt5"
  check_directory "${CMAKE_CURRENT_LIST_DIR}"
  local exclude_list="${CMAKE_CURRENT_LIST_DIR}/Qt5Config.cmake"
  local QT5_DIR_LIST=""
  for d in "$CMAKE_DIR"/Qt5*; do
    [[ -d "$d" ]] || continue
    QT5_DIR_LIST+="${d},"
  done
  QT5_DIR_LIST="${QT5_DIR_LIST%,}"
  replace_in_tree \
    "${QT5_DIR_LIST}" \
    "\"/${SYSROOT}" \
    '"${_psc_sys_root}' \
    "${exclude_list}"
  local rel_path=$(realpath --relative-to="${CMAKE_CURRENT_LIST_DIR}" "${SYSROOT}")
  insert_after_match \
    "${CMAKE_DIR}/Qt5/Qt5Config.cmake" \
    'get_filename_component[(]_qt5_install_prefix.*[)]' \
    "
      if(CMAKE_SYSROOT)
          set(_psc_sys_root \"\${CMAKE_SYSROOT}\")
          message(STATUS \"CMAKE_SYSROOT:psc_sys_root = \${_psc_sys_root} at \${CMAKE_CURRENT_LIST_FILE}  \")
      else()
          set(_psc_sys_root \${CMAKE_CURRENT_LIST_DIR}/${rel_path})
          message(STATUS \"psc_sys_root = \${_psc_sys_root} at \${CMAKE_CURRENT_LIST_FILE} \")
      endif()
    "
}
export bq_ver="v5.15.2"
export bq_suffix_opts=""
export bq_install_prefix="ae/Qt/v5.15.2"
# 交叉编译的选项
export bq_cross="0"
#  这个选项时目录名称  在@/qtbase/mkspecs下

#host
export bq_platform="linux-g++"
#target
export bq_xplatform="linux-aarch64-gnu-g++"
export bq_sysroot=""
# 交叉编译使用的host工具qt版本  和目标qt版本 一致
export bq_host_tool_install_prefix=""
export bq_host_triplet=""
export bq_target_triplet=""
export bq_host_tool_suffix_opts=""
export bq_host_platform=""


build_qt_host_tools() {
  local src_dir=""
  local opts=""
  ensure_repo_version src_dir "Qt" "${bq_ver}"
  exec_record_fast_fail "cd \"${src_dir}\""
  exec_record_fast_fail "git submodule status --recursive"
  exec_record_fast_fail "git submodule update --init --recursive"
  check_platform "${src_dir}" "${bq_host_platform}"
  check_directory "$(dirname "${bq_host_tool_install_prefix}")"
    opts=" \
    -opensource -confirm-license \
    -release \
    -xplatform ${bq_host_platform} \
    -prefix \"${bq_host_tool_install_prefix}\" \
    -make tools -nomake examples -nomake tests \
    -c++std c++14 \
    -no-gui \
    -no-widgets \
    -no-dbus \
    -no-opengl \
    -no-egl \
    -no-openssl \
    -no-icu \
    -no-glib \
    -skip qtdoc \
    -device-option CROSS_COMPILE=${bq_host_triplet}- \
    ${bq_host_tool_suffix_opts} \
  "
  configure_gnu_ex "${src_dir}/qtbase" "${opts}"
  exec_record "make -j $(nproc)"
  exec_record "make install -j $(nproc)"
  if [ ! -d "${bq_host_tool_install_prefix}/bin" ]; then
    echo "host tool build failed: ${bq_host_tool_install_prefix}/bin not found" >&2
    return 1
  fi
  if [ ! -x "${bq_host_tool_install_prefix}/bin/moc" ]; then
    echo "host tool build failed: moc not found in ${bq_host_tool_install_prefix}/bin" >&2
    return 1
  fi
  if [ ! -x "${bq_host_tool_install_prefix}/bin/rcc" ]; then
    echo "host tool build failed: rcc not found in ${bq_host_tool_install_prefix}/bin" >&2
    return 1
  fi
  if [ ! -x "${bq_host_tool_install_prefix}/bin/uic" ]; then
    echo "host tool build failed: uic not found in ${bq_host_tool_install_prefix}/bin" >&2
    return 1
  fi
  echo "host tools installed at: ${bq_host_tool_install_prefix}/bin"
  return 0
}


get_qt_platform_from_triplet() {
  local triplet="${1}"
  local cpu vendor os abi
  parse_triplet "$triplet" cpu vendor os abi
}
get_platform_list() {
  local -n list="$1"
  local src_dir="$2"
  local base_dir="$src_dir/qtbase/mkspecs"
  local dir
  list=()
  [ -d "$base_dir" ] || return 1
  for dir in "$base_dir"/*; do
    [ -d "$dir" ] || continue
    list+=("$(basename "$dir")")
  done
  return 0
}
print_platform_list() {
  local src_dir="$1"
  local platforms=()
  local item
  get_platform_list platforms "$src_dir" || return 1
  for item in "${platforms[@]}"; do
    printf '%s\n' "$item"
  done
  return 0
}
check_platform() {
  local src_dir="$1"
  local platform="$2"
  local base_dir="$src_dir/qtbase/mkspecs"
  if [ ! -d "$base_dir" ]; then
    printf 'mkspecs 目录不存在: %s\n' "$base_dir" >&2
    return 1
  fi
  if [ -z "$platform" ]; then
    printf 'platform 不能为空\n' >&2
    return 1
  fi
  if [ -d "$platform" ]; then
    if [ "$(dirname "$platform")" = "$base_dir" ]; then
      return 0
    fi
    printf 'platform 不属于当前源码树: %s\n' "$platform" >&2
    return 1
  fi
  if [ -d "$base_dir/$platform" ]; then
    return 0
  fi
  printf 'platform 不存在: %s\n' "$platform" >&2
  printf '可用 platform 列表:\n' >&2
  print_platform_list "$src_dir" >&2
  return 1
}
# TODO: cross qt 还需要改路径
build_qt() {
  src_dir=""
  ensure_repo_version src_dir "Qt" "${bq_ver}"
  exec_record_fast_fail "cd \"${src_dir}\""
  exec_record_fast_fail "git submodule status --recursive"
  exec_record_fast_fail "git submodule update --init --recursive"
  local opts
  opts=" \
    -opensource -confirm-license \
    -nomake tests \
    -nomake examples \
    -skip qtwebengine \
    -skip qtdocgallery \
    ${bq_suffix_opts} \
  "
  if [[ "$bq_cross" == "1" ]]; then
    check_directory_var bq_sysroot
    check_platform "${src_dir}" "${bq_platform}"
    check_platform "${src_dir}" "${bq_xplatform}"
    if [ -z "$bq_host_tool_install_prefix" ]; then
      bq_host_tool_install_prefix="${bq_install_prefix}/host"
      log_info "build_qt_host_tools  ${bq_host_tool_install_prefix}"
      mkdir -p "${bq_host_tool_install_prefix}"
      build_qt_host_tools
    else
      log_info "build_qt_host_tools at [${bq_host_tool_install_prefix}] exist skip "
    fi

    opts=" \
      -sysroot \"${bq_sysroot}\"     \
      -external-hostbindir \"${bq_host_tool_install_prefix}/bin\"   \
      -platform ${bq_platform} \
      -xplatform ${bq_xplatform} \
      -prefix \"${bq_sysroot}\" \
      -extprefix \"${bq_install_prefix}\" \
      -hostprefix  \"${bq_install_prefix}/host\" \
      -c++std c++14 \
      -device-option CROSS_COMPILE=${bq_host_triplet}- \
    ${opts}"
    #    sudo update-alternatives --set x86_64-w64-mingw32-g++ /usr/bin/x86_64-w64-mingw32-g++-posix
    #    sudo update-alternatives --set x86_64-w64-mingw32-gcc /usr/bin/x86_64-w64-mingw32-gcc-posix
    # TODO: platform 是win 才传入
  else
    opts=" \
          -prefix \"${bq_install_prefix}\" \
    ${opts}"
  fi
  #  exec_record "which ${bq_host_triplet}-g++"
  #  exec_record "${bq_host_triplet}-g++ -v"
  #  exec_record "which ${bq_host_triplet}-gcc"
  #  exec_record "${bq_host_triplet}-gcc -v"
  #
  #
  #
  #
  #  export CC="${bq_host_triplet}-gcc"
  #  export CXX="${bq_host_triplet}-g++"
  configure_gnu_ex "$src_dir" "${opts}"
  exec_record "make -j $(nproc)"
  exec_record "make install -j $(nproc)"
  if [[ "$bq_cross" == "1" ]]; then
    patch_cross_qt "${bq_sysroot}" "${bq_install_prefix}/lib/cmake"
  fi
}


