#!/usr/bin/env bash
set -euo pipefail

TARGET="${1:?usage: ensure_qscintilla6.sh <target> <goos> <goarch>}"
GOOS_VALUE="${2:?usage: ensure_qscintilla6.sh <target> <goos> <goarch>}"
GOARCH_VALUE="${3:?usage: ensure_qscintilla6.sh <target> <goos> <goarch>}"

VERSION="${QSCINTILLA_VERSION:-2.14.1}"
ROOT_DIR="$(pwd)"
PC_DIR="${ROOT_DIR}/pkgconfig/${TARGET}"
CACHE_DIR="${QSCINTILLA_CACHE_DIR:-${ROOT_DIR}/.cache/qscintilla6}"
SRC_URL="${QSCINTILLA_SOURCE_URL:-https://www.riverbankcomputing.com/static/Downloads/QScintilla/${VERSION}/QScintilla_src-${VERSION}.tar.gz}"

pkg_config_cmd() {
  if [[ -n "${PKG_CONFIG:-}" ]]; then
    printf '%s\n' "${PKG_CONFIG}"
    return
  fi
  case "${GOOS_VALUE}/${GOARCH_VALUE}" in
    windows/386) printf '%s\n' "i686-w64-mingw32.static-pkg-config" ;;
    windows/amd64) printf '%s\n' "x86_64-w64-mingw32.static-pkg-config" ;;
    *) printf '%s\n' "pkg-config" ;;
  esac
}

pkg_config_search_path() {
  local path="$1"
  printf '%s:%s:%s\n' "${path}" "${ROOT_DIR}/pkgconfig" "${PKG_CONFIG_PATH:-}"
}

pkg_config_libdir() {
  local path="$1"
  local prefix=""
  case "${GOOS_VALUE}/${GOARCH_VALUE}" in
    windows/386) prefix="/usr/lib/mxe/usr/i686-w64-mingw32.static" ;;
    windows/amd64) prefix="/usr/lib/mxe/usr/x86_64-w64-mingw32.static" ;;
    *) return 0 ;;
  esac
  printf '%s:%s:%s:%s:%s\n' \
    "${path}" \
    "${ROOT_DIR}/pkgconfig" \
    "${prefix}/lib/pkgconfig" \
    "${prefix}/share/pkgconfig" \
    "${prefix}/qt6/lib/pkgconfig"
}

pkg_config_target_path_var() {
  case "${GOOS_VALUE}/${GOARCH_VALUE}" in
    windows/386) printf '%s\n' "PKG_CONFIG_PATH_i686_w64_mingw32_static" ;;
    windows/amd64) printf '%s\n' "PKG_CONFIG_PATH_x86_64_w64_mingw32_static" ;;
  esac
}

qscintilla_private_libs() {
  case "${GOOS_VALUE}" in
    windows) return 0 ;;
  esac
}

qscintilla_requires() {
  case "${GOOS_VALUE}" in
    windows) printf '%s\n' "Qt6Widgets" ;;
    *) printf '%s\n' "Qt6Widgets Qt6PrintSupport" ;;
  esac
}

repair_pc_private_libs() {
  local pc_file="$1"
  local private_libs
  private_libs="$(qscintilla_private_libs)"
  [[ -f "${pc_file}" ]] || return 0
  if [[ "${GOOS_VALUE}" == "windows" ]]; then
    if grep -q '^[[:space:]]*Requires:' "${pc_file}"; then
      sed -i.bak 's/^[[:space:]]*Requires:.*/Requires: Qt6Widgets/' "${pc_file}"
      rm -f "${pc_file}.bak"
    else
      printf 'Requires: Qt6Widgets\n' >> "${pc_file}"
    fi
  fi
  [[ -n "${private_libs}" ]] || return 0
  if ! grep -q '^[[:space:]]*Libs\.private:.*-lwinspool' "${pc_file}"; then
    printf 'Libs.private: %s\n' "${private_libs}" >> "${pc_file}"
  fi
}

run_pkg_config() {
  local pc="$1"
  local path="$2"
  shift 2
  local search_path libdir target_path_var
  search_path="$(pkg_config_search_path "${path}")"
  libdir="$(pkg_config_libdir "${path}")"
  target_path_var="$(pkg_config_target_path_var)"
  if [[ -n "${libdir}" ]]; then
    if [[ -n "${target_path_var}" ]]; then
      env \
        "PKG_CONFIG_PATH=${search_path}" \
        "PKG_CONFIG_LIBDIR=${libdir}" \
        "${target_path_var}=${search_path}" \
        "${pc}" "$@"
    else
      PKG_CONFIG_PATH="${search_path}" PKG_CONFIG_LIBDIR="${libdir}" "${pc}" "$@"
    fi
  else
    PKG_CONFIG_PATH="${search_path}" "${pc}" "$@"
  fi
}

qmake_cmd() {
  if [[ -n "${QSCINTILLA_QMAKE:-}" ]]; then
    printf '%s\n' "${QSCINTILLA_QMAKE}"
    return
  fi

  local candidates=()
  local find_roots=()
  local find_patterns=()
  case "${GOOS_VALUE}/${GOARCH_VALUE}" in
    windows/386)
      candidates=(
        i686-w64-mingw32.static-qmake-qt6
        i686-w64-mingw32.static-qt6-qmake
        i686-w64-mingw32.static-qmake6
        i686-w64-mingw32.static-qmake
        /usr/lib/mxe/usr/bin/i686-w64-mingw32.static-qmake-qt6
        /usr/lib/mxe/usr/bin/i686-w64-mingw32.static-qt6-qmake
        /usr/lib/mxe/usr/bin/i686-w64-mingw32.static-qmake6
        /usr/lib/mxe/usr/bin/i686-w64-mingw32.static-qmake
        /usr/lib/mxe/usr/i686-w64-mingw32.static/qt6/bin/qmake
        /usr/lib/mxe/usr/i686-w64-mingw32.static/qt6/bin/qmake6
      )
      find_roots=(/usr/lib/mxe/usr /usr/local /opt)
      find_patterns=('*i686*w64*mingw32*static*qmake*' 'qmake' 'qmake6')
      ;;
    windows/amd64)
      candidates=(
        x86_64-w64-mingw32.static-qmake-qt6
        x86_64-w64-mingw32.static-qt6-qmake
        x86_64-w64-mingw32.static-qmake6
        x86_64-w64-mingw32.static-qmake
        /usr/lib/mxe/usr/bin/x86_64-w64-mingw32.static-qmake-qt6
        /usr/lib/mxe/usr/bin/x86_64-w64-mingw32.static-qt6-qmake
        /usr/lib/mxe/usr/bin/x86_64-w64-mingw32.static-qmake6
        /usr/lib/mxe/usr/bin/x86_64-w64-mingw32.static-qmake
        /usr/lib/mxe/usr/x86_64-w64-mingw32.static/qt6/bin/qmake
        /usr/lib/mxe/usr/x86_64-w64-mingw32.static/qt6/bin/qmake6
      )
      find_roots=(/usr/lib/mxe/usr /usr/local /opt)
      find_patterns=('*x86_64*w64*mingw32*static*qmake*' 'qmake' 'qmake6')
      ;;
    darwin/*)
      candidates=(
        qmake6
        qmake-qt6
        qt6-qmake
        qmake
        /osxcross/bin/qmake6
        /osxcross/bin/qmake
        /opt/osxcross/bin/qmake6
        /opt/osxcross/bin/qmake
        /opt/Qt/6.7.*/macos/bin/qmake
        /opt/Qt/6.7.*/clang_64/bin/qmake
        /opt/qt6/bin/qmake
        /usr/local/qt6/bin/qmake
      )
      find_roots=(/osxcross /opt/osxcross /opt /usr/local)
      find_patterns=('qmake' 'qmake6' 'qmake-qt6' 'qt6-qmake' '*qmake*')
      ;;
    *)
      candidates=(qmake6 qmake-qt6 qmake)
      find_roots=(/usr /usr/local /opt)
      find_patterns=('qmake6' 'qmake-qt6' 'qmake')
      ;;
  esac

  local qt_var qt_dir
  for qt_var in host_bins bindir libexecdir prefix; do
    qt_dir="$("$(pkg_config_cmd)" --variable="${qt_var}" Qt6Core 2>/dev/null || true)"
    [[ -n "${qt_dir}" ]] || continue
    candidates+=(
      "${qt_dir}/qmake6"
      "${qt_dir}/qmake"
      "${qt_dir}/bin/qmake6"
      "${qt_dir}/bin/qmake"
      "${qt_dir}/libexec/qmake6"
      "${qt_dir}/libexec/qmake"
    )
  done

  for candidate in "${candidates[@]}"; do
    if [[ "${candidate}" == *"*"* ]]; then
      for expanded in ${candidate}; do
        if [[ -x "${expanded}" ]]; then
          printf '%s\n' "${expanded}"
          return
        fi
      done
      continue
    fi
    if command -v "${candidate}" >/dev/null 2>&1; then
      command -v "${candidate}"
      return
    fi
    if [[ -x "${candidate}" ]]; then
      printf '%s\n' "${candidate}"
      return
    fi
  done

  local existing_roots=()
  local root
  for root in "${find_roots[@]}"; do
    [[ -d "${root}" ]] && existing_roots+=("${root}")
  done
  if [[ "${#existing_roots[@]}" -gt 0 ]]; then
    local found
    while IFS= read -r found; do
      [[ -x "${found}" ]] || continue
      printf '%s\n' "${found}"
      return
    done < <(
      find "${existing_roots[@]}" -maxdepth 7 -type f \( $(for pattern in "${find_patterns[@]}"; do printf -- '-name %q -o ' "${pattern}"; done) -false \) -print 2>/dev/null | sort
    )
  fi

  return 1
}

qmake_diagnostics() {
  echo "qmake lookup diagnostics for ${TARGET}:" >&2
  echo "  PKG_CONFIG=$(pkg_config_cmd)" >&2
  local qt_var qt_dir
  for qt_var in host_bins bindir libexecdir prefix; do
    qt_dir="$("$(pkg_config_cmd)" --variable="${qt_var}" Qt6Core 2>/dev/null || true)"
    [[ -n "${qt_dir}" ]] || continue
    echo "  Qt6Core ${qt_var}: ${qt_dir}" >&2
  done
  echo "  qmake candidates on PATH:" >&2
  command -v qmake6 >&2 2>/dev/null || true
  command -v qmake-qt6 >&2 2>/dev/null || true
  command -v qt6-qmake >&2 2>/dev/null || true
  command -v qmake >&2 2>/dev/null || true
}

darwin_qscintilla_diagnostics() {
  echo "macOS QScintilla lookup diagnostics for ${TARGET}:" >&2
  echo "  uid=$(id -u 2>/dev/null || echo unknown)" >&2
  echo "  port candidates:" >&2
  local candidate
  for candidate in \
    osxcross-mp \
    osxcross-macports \
    /osxcross/bin/osxcross-mp \
    /osxcross/bin/osxcross-macports \
    /opt/osxcross/bin/osxcross-mp \
    /opt/osxcross/bin/osxcross-macports \
    port \
    /opt/local/bin/port \
    /osxcross/macports/bin/port \
    /opt/osxcross/macports/bin/port \
    /osxcross/macports/pkgs/opt/local/bin/port \
    /opt/osxcross/macports/pkgs/opt/local/bin/port; do
    if command -v "${candidate}" >/dev/null 2>&1; then
      echo "    $(command -v "${candidate}")" >&2
    elif [[ -x "${candidate}" ]]; then
      echo "    ${candidate}" >&2
    fi
  done
  echo "  QScintilla headers:" >&2
  find /opt/local /osxcross /opt/osxcross -path '*qsciabstractapis.h' -type f -print 2>/dev/null | sort | head -n 40 >&2 || true
  echo "  QScintilla libraries:" >&2
  find /opt/local /osxcross /opt/osxcross -type f \( \
    -name 'libqscintilla2_qt6*.dylib' -o \
    -name 'libqscintilla2_qt6*.a' -o \
    -name 'qscintilla2_qt6*.dylib' -o \
    -name 'qscintilla2_qt6*.a' \
  \) -print 2>/dev/null | sort | head -n 40 >&2 || true
}

pkg_config_diagnostics() {
  local pc="$1"
  local path="$2"
  echo "pkg-config diagnostics for ${TARGET}:" >&2
  echo "  PKG_CONFIG=${pc}" >&2
  echo "  PKG_CONFIG_PATH=$(pkg_config_search_path "${path}")" >&2
  echo "  PKG_CONFIG_LIBDIR=$(pkg_config_libdir "${path}")" >&2
  local target_path_var
  target_path_var="$(pkg_config_target_path_var)"
  if [[ -n "${target_path_var}" ]]; then
    echo "  ${target_path_var}=$(pkg_config_search_path "${path}")" >&2
  fi
  if [[ -f "${path}/QScintilla6.pc" ]]; then
    sed 's/^/  QScintilla6.pc: /' "${path}/QScintilla6.pc" >&2
  fi
  run_pkg_config "${pc}" "${path}" --print-errors --exists QScintilla6 >&2 || true
  run_pkg_config "${pc}" "${path}" --cflags QScintilla6 >&2 || true
  run_pkg_config "${pc}" "${path}" --libs QScintilla6 >&2 || true
}

header_exists_for_pc() {
  local pc="$1"
  local path="$2"
  local cflags
  cflags="$(run_pkg_config "${pc}" "${path}" --cflags QScintilla6 2>/dev/null || true)"
  for token in ${cflags}; do
    case "${token}" in
      -I*)
        local include_dir="${token#-I}"
        if [[ -f "${include_dir}/qsciabstractapis.h" || -f "${include_dir}/Qsci/qsciabstractapis.h" ]]; then
          return 0
        fi
        ;;
    esac
  done
  return 1
}

private_libs_exist_for_pc() {
  local pc="$1"
  local path="$2"
  case "${GOOS_VALUE}" in
    windows)
      ! run_pkg_config "${pc}" "${path}" --libs QScintilla6 2>/dev/null | grep -q -- '-lQt6PrintSupport'
      ;;
    *)
      return 0
      ;;
  esac
}

pc_is_usable() {
  local pc="$1"
  local path="$2"
  run_pkg_config "${pc}" "${path}" --exists QScintilla6 >/dev/null 2>&1 &&
    header_exists_for_pc "${pc}" "${path}" &&
    private_libs_exist_for_pc "${pc}" "${path}"
}

write_pc() {
  local prefix="$1"
  local libdir="$2"
  local includedir="$3"
  local qsciincludedir="$4"
  local private_libs
  private_libs="$(qscintilla_private_libs)"
  local requires
  requires="$(qscintilla_requires)"
  mkdir -p "${PC_DIR}"
  cat > "${PC_DIR}/QScintilla6.pc" <<PC
prefix=${prefix}
exec_prefix=\${prefix}
libdir=${libdir}
includedir=${includedir}
qsciincludedir=${qsciincludedir}

Name: QScintilla6
Description: QScintilla editor component for Qt6
Version: ${VERSION}
Requires: ${requires}
Libs: -L\${libdir} -lqscintilla2_qt6
Cflags: -I\${includedir} -I\${qsciincludedir}
PC
  if [[ -n "${private_libs}" ]]; then
    printf 'Libs.private: %s\n' "${private_libs}" >> "${PC_DIR}/QScintilla6.pc"
  fi
}

find_first_dir_for_file() {
  local name="$1"
  shift
  find "$@" -name "${name}" -type f -print 2>/dev/null | head -n 1 | xargs dirname
}

qscintilla_lib_dir() {
  local root="$1"
  local lib
  lib="$(find "${root}" -type f \( \
    -name 'libqscintilla2_qt6*.a' -o \
    -name 'libqscintilla2_qt6*.dll.a' -o \
    -name 'qscintilla2_qt6*.a' -o \
    -name 'qscintilla2_qt6*.dll.a' -o \
    -name 'qscintilla2_qt6*.dll' -o \
    -name 'libqscintilla2_qt6*.so*' -o \
    -name 'libqscintilla2_qt6*.dylib' -o \
    -name 'qscintilla2_qt6*.lib' \
  \) -print 2>/dev/null | head -n 1)"
  [[ -n "${lib}" ]] || return 1
  dirname "${lib}"
}

qt_pkg_config_var() {
  local name="$1"
  "$(pkg_config_cmd)" --variable="${name}" Qt6Core 2>/dev/null || true
}

write_existing_darwin_pc() {
  if [[ "${GOOS_VALUE}" != "darwin" ]]; then
    return 1
  fi

  local prefixes=()
  local qt_prefix qt_bindir qt_libexec
  local extra_prefixes=()
  qt_prefix="$(qt_pkg_config_var prefix)"
  qt_bindir="$(qt_pkg_config_var bindir)"
  qt_libexec="$(qt_pkg_config_var libexecdir)"
  [[ -n "${qt_prefix}" ]] && prefixes+=("${qt_prefix}")
  [[ -n "${qt_bindir}" ]] && prefixes+=("$(dirname "${qt_bindir}")")
  [[ -n "${qt_libexec}" ]] && prefixes+=("$(dirname "${qt_libexec}")")
  if [[ -n "${QSCINTILLA_DARWIN_PREFIXES:-}" ]]; then
    IFS=: read -r -a extra_prefixes <<< "${QSCINTILLA_DARWIN_PREFIXES}"
    prefixes+=("${extra_prefixes[@]}")
  fi
  prefixes+=(
    /opt/local/libexec/qt6
    /osxcross/macports/pkgs/opt/local/libexec/qt6
    /opt/osxcross/macports/pkgs/opt/local/libexec/qt6
    /opt/local
    /osxcross/macports/pkgs/opt/local
    /opt/osxcross/macports/pkgs/opt/local
  )

  local root header_dir libdir
  for root in "${prefixes[@]}"; do
    [[ -d "${root}" ]] || continue
    header_dir="$(find_first_dir_for_file qsciabstractapis.h "${root}/include" "${root}/libexec" 2>/dev/null || true)"
    [[ -n "${header_dir}" ]] || continue
    libdir="$(qscintilla_lib_dir "${root}" 2>/dev/null || true)"
    [[ -n "${libdir}" && -d "${libdir}" ]] || continue
    write_pc "${root}" "${libdir}" "$(dirname "${header_dir}")" "${header_dir}"
    return 0
  done
  return 1
}

install_darwin_package() {
  if [[ "${GOOS_VALUE}" != "darwin" ]]; then
    return 1
  fi
  local osxcross_mp=""
  if command -v osxcross-mp >/dev/null 2>&1; then
    osxcross_mp="$(command -v osxcross-mp)"
  elif command -v osxcross-macports >/dev/null 2>&1; then
    osxcross_mp="$(command -v osxcross-macports)"
  elif [[ -x /osxcross/bin/osxcross-mp ]]; then
    osxcross_mp="/osxcross/bin/osxcross-mp"
  elif [[ -x /osxcross/bin/osxcross-macports ]]; then
    osxcross_mp="/osxcross/bin/osxcross-macports"
  elif [[ -x /opt/osxcross/bin/osxcross-mp ]]; then
    osxcross_mp="/opt/osxcross/bin/osxcross-mp"
  elif [[ -x /opt/osxcross/bin/osxcross-macports ]]; then
    osxcross_mp="/opt/osxcross/bin/osxcross-macports"
  fi
  if [[ -n "${osxcross_mp}" ]]; then
    if [[ "${GOARCH_VALUE}" == "arm64" ]]; then
      echo "Installing qscintilla-qt6 with ${osxcross_mp} -arm64 for ${TARGET}." >&2
      "${osxcross_mp}" -arm64 install qscintilla-qt6 || true
    else
      echo "Installing qscintilla-qt6 with ${osxcross_mp} for ${TARGET}." >&2
      "${osxcross_mp}" install qscintilla-qt6 || true
    fi
    write_existing_darwin_pc
    return
  fi

  local port_cmd=""
  if command -v port >/dev/null 2>&1; then
    port_cmd="$(command -v port)"
  elif [[ -x /opt/local/bin/port ]]; then
    port_cmd="/opt/local/bin/port"
  elif [[ -x /osxcross/macports/bin/port ]]; then
    port_cmd="/osxcross/macports/bin/port"
  elif [[ -x /opt/osxcross/macports/bin/port ]]; then
    port_cmd="/opt/osxcross/macports/bin/port"
  elif [[ -x /osxcross/macports/pkgs/opt/local/bin/port ]]; then
    port_cmd="/osxcross/macports/pkgs/opt/local/bin/port"
  elif [[ -x /opt/osxcross/macports/pkgs/opt/local/bin/port ]]; then
    port_cmd="/opt/osxcross/macports/pkgs/opt/local/bin/port"
  fi
  if [[ -n "${port_cmd}" ]]; then
    echo "Installing qscintilla-qt6 with ${port_cmd} for ${TARGET}." >&2
    "${port_cmd}" -N -b install qscintilla-qt6 || "${port_cmd}" -N install qscintilla-qt6 || true
  fi
  write_existing_darwin_pc
}

install_linux_package() {
  if [[ "${GOOS_VALUE}" != "linux" ]]; then
    return 1
  fi
  if command -v apt-get >/dev/null 2>&1 && [[ "$(id -u)" == "0" ]]; then
    apt-get update
    DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
      pkg-config qt6-base-dev libqscintilla2-qt6-dev zip ca-certificates
  fi

  local header_dir
  header_dir="$(find_first_dir_for_file qsciabstractapis.h /usr/include /usr/local/include)"
  if [[ -z "${header_dir}" ]]; then
    return 1
  fi
  local libdir
  libdir="$(qscintilla_lib_dir /usr 2>/dev/null || true)"
  if [[ -z "${libdir}" || ! -d "${libdir}" ]]; then
    libdir="/usr/lib/$(gcc -dumpmachine 2>/dev/null || true)"
  fi
  write_pc /usr "${libdir}" "$(dirname "${header_dir}")" "${header_dir}"
}

write_existing_cross_pc() {
  case "${GOOS_VALUE}/${GOARCH_VALUE}" in
    windows/386)
      local prefix="/usr/lib/mxe/usr/i686-w64-mingw32.static"
      ;;
    windows/amd64)
      local prefix="/usr/lib/mxe/usr/x86_64-w64-mingw32.static"
      ;;
    *)
      return 1
      ;;
  esac

  local header_dir
  header_dir="$(find_first_dir_for_file qsciabstractapis.h "${prefix}" 2>/dev/null || true)"
  [[ -n "${header_dir}" ]] || return 1
  local libdir
  libdir="$(qscintilla_lib_dir "${prefix}" 2>/dev/null || true)"
  [[ -n "${libdir}" && -d "${libdir}" ]] || return 1
  write_pc "${prefix}" "${libdir}" "$(dirname "${header_dir}")" "${header_dir}"
}

download_source() {
  mkdir -p "${CACHE_DIR}"
  local tarball="${CACHE_DIR}/QScintilla_src-${VERSION}.tar.gz"
  if [[ ! -f "${tarball}" ]]; then
    if command -v curl >/dev/null 2>&1; then
      curl -fsSL "${SRC_URL}" -o "${tarball}" || return 1
    elif command -v wget >/dev/null 2>&1; then
      wget -q "${SRC_URL}" -O "${tarball}" || return 1
    else
      echo "curl or wget is required to download QScintilla ${VERSION}." >&2
      return 1
    fi
  fi
  if [[ ! -d "${CACHE_DIR}/QScintilla_src-${VERSION}" ]]; then
    tar -C "${CACHE_DIR}" -xf "${tarball}" || return 1
  fi
}

build_source() {
  local qmake
  qmake="$(qmake_cmd)" || {
    echo "No qmake command found for ${TARGET}. Set QSCINTILLA_QMAKE to build QScintilla." >&2
    qmake_diagnostics
    return 1
  }
  download_source || return 1

  local source_dir="${CACHE_DIR}/QScintilla_src-${VERSION}"
  local build_dir="${CACHE_DIR}/build-${TARGET}"
  rm -rf "${build_dir}"
  mkdir -p "${build_dir}"
  cp -R "${source_dir}/." "${build_dir}/"

  (
    cd "${build_dir}/src"
    "${qmake}" qscintilla.pro CONFIG+=release
    make -j"$(getconf _NPROCESSORS_ONLN 2>/dev/null || echo 2)"
  )

  local header_dir="${build_dir}/src/Qsci"
  if [[ ! -f "${header_dir}/qsciabstractapis.h" ]]; then
    header_dir="${build_dir}/src"
  fi
  local libdir
  libdir="$(qscintilla_lib_dir "${build_dir}" 2>/dev/null || true)"
  if [[ -z "${libdir}" || ! -d "${libdir}" ]]; then
    echo "QScintilla build finished but libqscintilla2_qt6 was not found." >&2
    find "${build_dir}" -type f -iname '*qscintilla*' -print 2>/dev/null | sort | head -n 80 >&2
    return 1
  fi
  write_pc "${build_dir}" "${libdir}" "${build_dir}/src" "${header_dir}"
}

pc="$(pkg_config_cmd)"
mkdir -p "${PC_DIR}"
repair_pc_private_libs "${PC_DIR}/QScintilla6.pc"

if pc_is_usable "${pc}" "${PC_DIR}"; then
  echo "QScintilla6 is available for ${TARGET} via ${PC_DIR}."
  exit 0
fi

if install_linux_package && pc_is_usable "${pc}" "${PC_DIR}"; then
  echo "Installed QScintilla6 system package for ${TARGET}."
  exit 0
fi

if write_existing_cross_pc && pc_is_usable "${pc}" "${PC_DIR}"; then
  echo "Found existing QScintilla6 cross files for ${TARGET}."
  exit 0
fi

if write_existing_darwin_pc && pc_is_usable "${pc}" "${PC_DIR}"; then
  echo "Found existing QScintilla6 macOS cross files for ${TARGET}."
  exit 0
fi

if install_darwin_package && pc_is_usable "${pc}" "${PC_DIR}"; then
  echo "Installed QScintilla6 MacPorts package for ${TARGET}."
  exit 0
fi

if [[ "${GOOS_VALUE}" == "darwin" ]]; then
  echo "Unable to find or install MacPorts qscintilla-qt6 for ${TARGET}." >&2
  darwin_qscintilla_diagnostics
  echo "The Skydas macOS Docker image has Qt6 pkg-config files but no qmake, so QScintilla must be available from MacPorts target files." >&2
  echo "Install qscintilla-qt6 in the macOS builder image or provide QScintilla headers/libs under /osxcross/macports/pkgs/opt/local." >&2
  exit 1
fi

if build_source; then
  if pc_is_usable "${pc}" "${PC_DIR}"; then
    echo "Built QScintilla6 ${VERSION} for ${TARGET}."
    exit 0
  fi
  echo "QScintilla6 was built for ${TARGET}, but its pkg-config file is not usable." >&2
  pkg_config_diagnostics "${pc}" "${PC_DIR}"
fi

echo "Unable to prepare QScintilla6 for ${TARGET}." >&2
exit 1
