# Maintainer (MSYS2): Michael Hansen (zrax0111@gmail.com)
# Contributor: Zach Bacon <zachbacon@vba-m.com>

_variant=-${KF5_VARIANT:-shared}
source "$(dirname ${BASH_SOURCE[0]})"/../mingw-w64-PKGBUILD-common/kde-frameworks5
_kde_f5_init_package "${_variant}" "kdoctools"
pkgver=5.75.0
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
pkgdesc="KF5 documentation framework using docbook (mingw-w64-qt5${_namesuff})"
license=('LGPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-extra-cmake-modules>=${pkgver}"
             "perl-URI")
depends=()
_kde_f5_add_depends "${_variant}" "${MINGW_PACKAGE_PREFIX}-qt5${_namesuff}"
_kde_f5_add_depends "${_variant}" "${MINGW_PACKAGE_PREFIX}-libxslt${_namesuff}"
_kde_f5_add_depends "${_variant}" "${MINGW_PACKAGE_PREFIX}-docbook-xsl${_namesuff}"
_kde_f5_add_depends "${_variant}" "${MINGW_PACKAGE_PREFIX}-ki18n-qt5${_namesuff}>=${pkgver}"
_kde_f5_add_depends "${_variant}" "${MINGW_PACKAGE_PREFIX}-karchive-qt5${_namesuff}>=${pkgver}"
groups=('kf5')
source=("https://download.kde.org/stable/frameworks/${pkgver%.*}/${_basename}-${pkgver}.tar.xz"
        catalog_path.patch)
sha256sums=('4463dd3021ebff078a92d0556f37cbfa1b16ba970e8ad510bf397bd55f49381a'
            '7dafde4dd5a202c82b3e88480c0c80a045845f9bf35576e061a54a7fc2d31303')

prepare() {
  mkdir -p build-${CARCH}${_variant}

  # Passing CMAKE_INSTALL_PREFIX=/mingw64 causes meinproc to look in
  # C:/mingw64 for its catalog files, which is almost never correct
  cd ${_basename}-${pkgver}
  patch -p1 -i "${srcdir}/catalog_path.patch"
}

build() {
  local -a extra_config
  cd build-${CARCH}${_variant}
  if [ "${_variant}" = "-static" ]; then
    extra_config+=( -DBUILD_SHARED_LIBS=NO )
    QT5_PREFIX=${MINGW_PREFIX}/qt5-static
    export PATH=${QT5_PREFIX}/bin:"$PATH"
  else
    QT5_PREFIX=${MINGW_PREFIX}
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DECM_MKSPECS_INSTALL_DIR=" \
    cmake ../${_basename}-${pkgver} \
      -DCMAKE_BUILD_TYPE=$(_kde_f5_CMAKE_BUILD_TYPE) \
      -DCMAKE_INSTALL_PREFIX=${QT5_PREFIX} \
      -DMSYS_REAL_INSTALL_DIR=${QT5_PREFIX} \
      -DCMAKE_INSTALL_DATAROOTDIR=share \
      -DKDE_INSTALL_LIBDIR=lib \
      -DECM_MKSPECS_INSTALL_DIR=${QT5_PREFIX}/share/qt5/mkspecs/modules \
      -DPERL_EXECUTABLE=/usr/bin/perl \
      -DBUILD_QCH=OFF \
      -DBUILD_TESTING=OFF \
      -DECM_DIR=${MINGW_PREFIX}/share/ECM \
      "${extra_config[@]}" \
      -G'MSYS Makefiles'
  make
}

package() {
  cd build-${CARCH}${_variant}
  make DESTDIR="${pkgdir}" install
}
