# Maintainer: Hiroshi Hatake <cosmo0920.wp@gmail.com>

_realname=groonga
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=15.1.7
pkgrel=2
pkgdesc="An opensource fulltext search engine (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://groonga.org/"
license=('spdx:LGPL-2.1-or-later')
source=(https://packages.groonga.org/source/${_realname}/${_realname}-${pkgver}.tar.gz{,.asc}
        001-aarch64.patch)
depends=("${MINGW_PACKAGE_PREFIX}-arrow"
         "${MINGW_PACKAGE_PREFIX}-blosc2"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-mecab"
         "${MINGW_PACKAGE_PREFIX}-mecab-naist-jdic"
         "${MINGW_PACKAGE_PREFIX}-msgpack-c"
         "${MINGW_PACKAGE_PREFIX}-onigmo"
         "${MINGW_PACKAGE_PREFIX}-xxhash"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-rapidjson"
             "${MINGW_PACKAGE_PREFIX}-ruby"
             "${MINGW_PACKAGE_PREFIX}-xsimd")
sha256sums=('899a414a0636f7568d1a11845fe3dd75b701f72106a7a26f30bc950af5876616'
            'SKIP'
            '5fbb336487a6522e2f7bff01ea3cdd714d4c27de3cc65d5a6ecb2eedeb6fc2c4')
validpgpkeys=('2701F317CFCCCB975CADE9C2624CF77434839225') # Groonga Key <packages@groonga.org>

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

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

  apply_patch_with_msg \
    001-aarch64.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

  CFLAGS+=" -Wno-incompatible-pointer-types" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G Ninja \
      "${_extra_config[@]}" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DGRN_WITH_APACHE_ARROW=ON \
      -DGRN_WITH_BLOSC=auto \
      -DGRN_WITH_MRUBY=ON \
      ../${_realname}-${pkgver}

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

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

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  pushd "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
  for pc in *.pc; do
    sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i ${pc}
  done
  popd

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