# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=doxygen
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.14.0
pkgrel=1
pkgdesc="A documentation system for C++, C, Java, IDL and PHP (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.doxygen.nl/"
msys2_repository_url="https://github.com/doxygen/doxygen"
msys2_references=(
  'archlinux: doxygen'
  "cpe: cpe:/a:doxygen:doxygen"
)
license=('spdx:GPL-2.0-only')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         $([[ ${CARCH} == i686 ]] || echo "${MINGW_PACKAGE_PREFIX}-clang-libs")
         "${MINGW_PACKAGE_PREFIX}-fmt"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-spdlog"
         "${MINGW_PACKAGE_PREFIX}-xapian-core")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             $([[ ${CARCH} == i686 ]] || echo \
               "${MINGW_PACKAGE_PREFIX}-clang-tools-extra" \
               "${MINGW_PACKAGE_PREFIX}-llvm" \
               "${MINGW_PACKAGE_PREFIX}-qt6-base"))
if [[ ${CARCH} != i686 ]]; then
  optdepends=("${MINGW_PACKAGE_PREFIX}-qt6-base: for doxywizard")
fi
source=(https://www.doxygen.nl/files/${_realname}-${pkgver}.src.tar.gz
        cmake-mingw.patch
        0002-clang-19-fix.patch
        0003-fix-building-with-gcc.patch)
sha256sums=('d4536d11ab13037327d8d026b75f5a86b7ccb2093e2f546235faf61fd86e6b5d'
            'c7406ee2571ad4e08aa1c58cbb675d531ae0d849743877cebd3d900f84e83e23'
            'cfe1218b5897ad9ec88dab6dd86f592f941feac464646df619bb8ccb90bbdc4e'
            '0efe17145002fc2abd349e051762faf45c89a2db8e8eb83f99660942a9116319')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  apply_patch_with_msg \
    cmake-mingw.patch \
    0003-fix-building-with-gcc.patch

  # llvm 19 fixes
  # https://github.com/doxygen/doxygen/issues/10928
  apply_patch_with_msg \
    0002-clang-19-fix.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CARCH} != i686 ]]; then
    _extra_config+=("-Dbuild_wizard=ON" "-Duse_libclang=ON")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake -Wno-dev \
    -G"Ninja" \
    "${_extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_INSTALL_LIBDIR=lib \
    -DCMAKE_INSTALL_BINDIR=bin \
    -Dbuild_parse=ON \
    -Dbuild_search=ON \
    -Duse_sys_fmt=ON \
    -Duse_sys_spdlog=ON \
    -Duse_sys_sqlite3=ON \
    -Dforce_qt="Qt6" \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
