# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=extra-cmake-modules
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=6.19.0
pkgrel=1
pkgdesc='Extra modules and scripts for CMake (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'archlinux: extra-cmake-modules'
)
msys2_repository_url='https://invent.kde.org/frameworks/extra-cmake-modules/'
url='https://community.kde.org/Frameworks/'
license=('spdx:BSD-3-Clause')
groups=("${MINGW_PACKAGE_PREFIX}-kf6")
depends=(
  "${MINGW_PACKAGE_PREFIX}-cmake"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-ninja"
)
source=("https://download.kde.org/stable/frameworks/${pkgver%.*}/${_realname}-${pkgver}.tar.xz"{,.sig}
        "tar-force-local.patch")
sha256sums=('a4f0c1d8181f43e9af4b9b44696c77760b5bc9dae5bdb921f090bce664e9ca84'
            'SKIP'
            '0f0cec3a0f51a0e21299f468361489d339a3e8c25119f94fce87decadebaaae2')
validpgpkeys=(
  'E0A3EB202F8E57528E13E72FD7574483BB57B18D' # Jonathan Esk-Riddell <jr@jriddell.org>
  '90A968ACA84537CC27B99EAF2C8DF587A6D4AAC1' # Nicolas Fella <nicolas.fella@kde.org>
)

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

  # Required for extracting kioworker6.tar.bz2 in kio package
  # It fails to extract because full win-style path contains colon
  patch -p1 -i "${srcdir}/tar-force-local.patch"

  # Make sure to update patch in git style because CMake
  # installs *.orig files if hunks are in different offset
  find . -name "*.orig" -exec rm -f {} \;
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_HTML_DOCS=OFF \
      -DBUILD_QTHELP_DOCS=OFF \
      -DBUILD_TESTING=OFF \
      "${_extra_config[@]}" \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"
}

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