# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>
# Contributor: Christopher Degawa <ccom@randomderp.com>

_realname=ripgrep
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=15.0.0
pkgrel=1
pkgdesc='line-oriented search tool that recursively searches your current directory for a regex pattern (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/BurntSushi/ripgrep'
msys2_references=(
  'archlinux: ripgrep'
  'purl: pkg:cargo/ripgrep'
  "cpe: cpe:2.3:a:ripgrep_project:ripgrep"
)
license=('spdx:MIT OR Unlicense')
depends=("${MINGW_PACKAGE_PREFIX}-pcre2")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust" "${MINGW_PACKAGE_PREFIX}-pkgconf")
options=('!strip')
source=("${url}/archive/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('e6b2d35ff79c3327edc0c92a29dc4bb74e82d8ee4b8156fb98e767678716be7a')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  tar -xzf "${_realname}-${pkgver}.tar.gz" || true
  cd "${_realname}-${pkgver}"

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

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

  export RUSTFLAGS="${RUSTFLAGS/+crt-static/-crt-static}"
  cargo build --frozen --features 'pcre2' --profile release-lto
}

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

  cargo test --frozen --features 'pcre2' --profile release-lto
}

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

  cargo install \
    --frozen \
    --offline \
    --no-track \
    --features 'pcre2' \
    --profile release-lto \
    --path . \
    --root "${pkgdir}${MINGW_PREFIX}"

  local _complete="target/release-lto/rg --generate"
  $_complete complete-zsh | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_rg"
  $_complete complete-bash | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/rg"
  $_complete complete-fish | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/rg.fish"
  $_complete man | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/man/man1/rg.1"

  install -Dm644 COPYING LICENSE-MIT UNLICENSE -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
}
