# Maintainer: No one

_realname=spatialindex
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.1.0
pkgrel=1
pkgdesc="Extensible framework that supports robust spatial indexing methods and sophisticated spatial queries (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://libspatialindex.github.io'
msys2_repository_url="https://github.com/libspatialindex/libspatialindex"
msys2_references=(
  'archlinux: spatialindex'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${msys2_repository_url}/releases/download/${pkgver}/${_realname}-src-${pkgver}.tar.bz2"
        "001-install-pkgconf.patch")
sha256sums=('c59932395e98896038d59199f2e2453595df6d730ffbe09d69df2a661bcb619b'
            'd2da9b682a973bf90f28782be80ddda093a51cd1b03833fe01618b6329548315')

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

prepare() {
  cd "${srcdir}"/${_realname}-src-${pkgver}
  apply_patch_with_msg \
    001-install-pkgconf.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DBUILD_TESTING=OFF \
      ../${_realname}-src-${pkgver}

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

check() {
  cd "${srcdir}"/build-${MSYSTEM}

  ${MINGW_PREFIX}/bin/cmake \
    -DBUILD_TESTING=ON \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    ../${_realname}-src-${pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .

  ${MINGW_PREFIX}/bin/ctest
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

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