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

_realname=doxygen
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.9.1
pkgrel=3
pkgdesc="A documentation system for C++, C, Java, IDL and PHP (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="http://www.doxygen.org/"
options=('strip' 'staticlibs')
license=('GPL')
depends=(#"${MINGW_PACKAGE_PREFIX}-clang" # we dont want this huge dependency
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-xapian-core")
makedepends=(#"${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             #"${MINGW_PACKAGE_PREFIX}-polly"
             "${MINGW_PACKAGE_PREFIX}-python"
             $( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* || ${MINGW_PACKAGE_PREFIX} == *-ucrt-* ]] || echo "${MINGW_PACKAGE_PREFIX}-qt5" )
             'flex'
             'bison')
optdepends=("${MINGW_PACKAGE_PREFIX}-qt5")
source=("http://doxygen.nl/files/${_realname}-${pkgver}.src.tar.gz"
        cmake-mingw.patch)
sha256sums=('67aeae1be4e1565519898f46f1f7092f1973cce8a767e93101ee0111717091d1'
            '7a74cdd4cfaba3f5521b5179a91eec4ad96c1da89534576be356238809df4aa6')

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
}

build() {
  export QTDIR=${MINGW_PREFIX}

  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  mkdir build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -Dbuild_wizard=$( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* || ${MINGW_PACKAGE_PREFIX} == *-ucrt-* ]] && echo "OFF" || echo "ON" ) \
    -Dbuild_search=ON \
    -Duse_sqlite3=ON \
    -Duse_libclang=OFF \
    -Dbuild_parse=ON \
    ../${_realname}-${pkgver}

  # fix some generated makefiles: replace LLVM-NOTFOUND by the path to LLVM.dll
  # llvm_path_mixed=$(cygpath -m `which libLLVM.dll`)
  # llvm_path_unix=$(echo ${llvm_path_mixed} | sed 's|C:|/C|')
  # sed -e "s|\(bin/.*\.exe: \)LLVM-NOTFOUND|\1${llvm_path_mixed}|" \
      # -e "s|\(.*libclangBasic.a \)LLVM-NOTFOUND|\1${llvm_path_unix}|" \
      # -i ./addon/doxyparse/CMakeFiles/doxyparse.dir/build.make \
         # ./src/CMakeFiles/doxygen.dir/build.make

  make
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install
}
