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

_realname=ripgrep-all
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.10.9
pkgrel=1
pkgdesc="ripgrep, but also search in PDFs, E-Books, Office documents, zip, tar.gz, etc. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/phiresky/ripgrep-all/'
msys2_references=(
  'purl: pkg:cargo/ripgrep_all'
)
license=('spdx:AGPL-3.0-only')
depends=("${MINGW_PACKAGE_PREFIX}-ripgrep" "${MINGW_PACKAGE_PREFIX}-xz")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust" "${MINGW_PACKAGE_PREFIX}-pkgconf")
optdepends=("${MINGW_PACKAGE_PREFIX}-ffmpeg: for the ffmpeg adapter"
            "${MINGW_PACKAGE_PREFIX}-graphicsmagick: for the pdfpages adapter"
            "${MINGW_PACKAGE_PREFIX}-poppler: for the poppler adapter"
            "${MINGW_PACKAGE_PREFIX}-tesseract-ocr: for the tesseract adapter")
checkdepends=("${MINGW_PACKAGE_PREFIX}-poppler")
source=("https://github.com/phiresky/ripgrep-all/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('a5b3150940dcddd35a26e9de398f11a563d0466a335e5450ceb7ff369e9fef45')

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

  # there are some build issues when you have installed rust from both msys2 and rustup:
  # - it downloads extra deps at build() (MSVC specific)
  # - ...and eventually it failes (problems with compiling using MSVC)
  rm rust-toolchain.toml

  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

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

  WINAPI_NO_BUNDLED_LIBRARIES=1 \
    cargo build --release --frozen
}

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

  WINAPI_NO_BUNDLED_LIBRARIES=1 \
    cargo test --release --frozen
}

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

  WINAPI_NO_BUNDLED_LIBRARIES=1 \
  cargo install \
    --offline \
    --no-track \
    --frozen \
    --path . \
    --root "${pkgdir}${MINGW_PREFIX}"

  install -Dm644 LICENSE.md "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"
}
