# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Jakub Klinkovský <lahwaacz at archlinux dot org>
# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Contributor: Dan McGee <dan@archlinux.org>

pkgbase=doxygen
pkgname=(doxygen doxygen-docs)
pkgver=1.11.0
pkgrel=4
pkgdesc='Documentation system for C++, C, Java, IDL and PHP'
url='http://www.doxygen.nl'
arch=(x86_64 powerpc64le powerpc64 powerpc riscv64)
license=(GPL-2.0-or-later)
makedepends=(
  clang
  cmake
  flex
  fmt
  gcc-libs
  ghostscript
  glibc
  graphviz
  llvm
  python
  qt6-base
  spdlog
  sqlite
  texlive-fontsrecommended
  texlive-fontutils
  texlive-latexextra
  texlive-plaingeneric
)
source=(${pkgbase}-${pkgver}.tar.gz::https://github.com/doxygen/doxygen/archive/Release_${pkgver//./_}.tar.gz
        10891-fix-buffer-overflow.patch)
sha512sums=('68ab2c8cf570216a7e917f686ec79361179cdc9868966fcc2722ebab49032e2bc9ac60192f101793f978ad2d7236d83c461ab4d2477f7704cf32a003b87311fb'
            'bb48499a17254853522f58246a837113c27a3a960ecc42042f411eeab19faa46587e3d7af6dbdaaca78ec90ccccba8cd2f9e0457688a6b96eaba4c5799070455')
b2sums=('e98254aeec8ea7cedf6ec537e3d82cb898f55d435b95e58108c433f920432789fb7feba290b9a365f90ee56eec9120376b18ff26e31b1e4219b8f89f351daa8f'
        '500304f5727316ee3e6114076525100d30613de1821108c3ed805e862602252de0b9439c0384acad6ba9d175ccab2f84cccafba5942740282ef388613d9d39a5')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  cd ${pkgbase}-Release_${pkgver//./_}
  # Install the man pages in the right place
  sed -i 's:DESTINATION man/man1:DESTINATION "${CMAKE_INSTALL_PREFIX}/share/man/man1":g' \
    doc/CMakeLists.txt

  # Fix buffer overflow in Markdown parser https://github.com/doxygen/doxygen/pull/10891
  patch -Np1 -i ../10891-fix-buffer-overflow.patch
}

build() {
  #FIXME: on powerpc64le doxygen pulls in clang which breaks libreoffice
  case "${CARCH}" in
    powerpc64le) _cmake_options=(-Duse_libclang:BOOL=OFF) ;;
    *)           _cmake_options=(-Duse_libclang:BOOL=ON) ;;
  esac

  cmake -B build -S $pkgbase-Release_${pkgver//./_} \
    -DCMAKE_BUILD_TYPE:STRING=None \
    -DCMAKE_INSTALL_PREFIX:PATH=/usr \
    -DDOC_INSTALL_DIR:PATH=share/doc/doxygen \
    -Dbuild_doc:BOOL=ON \
    -Dbuild_wizard:BOOL=ON \
    -Duse_sys_spdlog:BOOL=ON \
    -Duse_sys_sqlite3:BOOL=ON \
    -Duse_libclang:BOOL=ON ${_cmake_options[@]}
  cmake --build build --verbose
  cmake --build build --target docs
}

check() {
  ctest --test-dir build --output-on-failure
}

package_doxygen() {
  depends=(
    fmt libfmt.so
    gcc-libs
    glibc
    spdlog libspdlog.so
    sqlite libsqlite3.so
  )
  depends_powerpc=(clang)
  depends_powerpc64=(clang)
  depends_riscv64=(clang)
  depends_x86_64=(clang)
  optdepends=(
    'graphviz: for caller/callee graph generation'
    'qt6-base: for doxywizard'
    'texlive-fontsrecommended: for generating LaTeX, Postscript and PDF output'
    'texlive-fontutils: for generating LaTeX, Postscript and PDF output'
    'texlive-latexextra: for generating LaTeX, Postscript and PDF output'
    'texlive-plaingeneric: for generating LaTeX, Postscript and PDF output'
  )

  DESTDIR="$pkgdir" cmake --install build
  (
    cd "$pkgdir"
    _pick $pkgbase-docs usr/share/doc
  )
  install -vDm 644 $pkgbase-Release_${pkgver//./_}/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
}

package_doxygen-docs() {
  pkgdesc='Developer documentation for doxygen'

  mv -v $pkgname/* "$pkgdir"
  install -vDm 644 $pkgbase-Release_${pkgver//./_}/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
}

# vim: ts=2 sw=2 et:
