# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=meilisearch
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.31.0
pkgrel=1
pkgdesc="A lightning-fast search engine API bringing AI-powered hybrid search to your sites and applications (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://www.meilisearch.com'
msys2_documentation_url='https://www.meilisearch.com/docs/home'
msys2_repository_url='https://github.com/meilisearch/meilisearch'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-oniguruma"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust" "${MINGW_PACKAGE_PREFIX}-pkgconf" 'git')
source=("git+${msys2_repository_url}#tag=v${pkgver}"
        "ntapi-rs.tar.gz::https://crates.io/api/v1/crates/ntapi/0.4.1/download"
        "zstd-sys.tar.gz::https://crates.io/api/v1/crates/zstd-sys/2.0.16+zstd.1.5.7/download"
        "ntapi-link-ntdll-arm64.patch"
        "zstd-sys-remove-statik.patch")
sha256sums=('09599c2a583d613f61bb038632c3451e9707e10421cb8a3dd8d911bd35399ea5'
            'e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4'
            '91e19ebc2adc8f83e43039e79776e3fda8ca919132d68a1fed6a5faca2683748'
            '9994d5b61f020f85f67df54c56c793bd4faf41e39b62ddd4e67c482b9a11b73e'
            '48f4900ceb02d3aaf9a1020f33d56629156e96759f456c0e7ca18bfcf910767b')

_env() {
  export ZSTD_SYS_USE_PKG_CONFIG=1
  export RUSTONIG_DYNAMIC_LIBONIG=1
  export WINAPI_NO_BUNDLED_LIBRARIES=1
  export RUSTFLAGS="${RUSTFLAGS/+crt-static/-crt-static}"
}

prepare() {
  cd "${_realname}"
  rm rust-toolchain.toml

  patch -d ../ntapi-0.4.1 -i ../ntapi-link-ntdll-arm64.patch
  patch -d ../zstd-sys-2.0.16+zstd.1.5.7 -i ../zstd-sys-remove-statik.patch
  cat >> Cargo.toml <<END

[patch.crates-io]
ntapi.path = "../ntapi-0.4.1"
zstd-sys.path = "../zstd-sys-2.0.16+zstd.1.5.7"
END

  cargo update -p ntapi -p zstd-sys --config='net.git-fetch-with-cli=true'
  cargo fetch --locked --target "${RUST_CHOST}"
}

build() {
  cd "${_realname}"

  _env
  cargo build --release --frozen -p meilisearch
}

check() {
  cd "${_realname}"

  _env
  cargo test --release --frozen -p meilisearch
}

package() {
  cd "${_realname}"

  _env
  cargo install \
    --offline \
    --no-track \
    --frozen \
    --path crates/meilisearch \
    --root "${pkgdir}${MINGW_PREFIX}"

  # we package only MIT-licensed code
  install -Dm644 LICENSE-MIT "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
