# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>
# Contributor: Naveen M K <naveen521kk@gmail.com>

_realname=bat
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.26.1
pkgrel=1
pkgdesc="Cat clone with syntax highlighting and git integration (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/sharkdp/bat'
msys2_references=(
  'archlinux: bat'
  'purl: pkg:cargo/bat'
  "cpe: cpe:/a:bat_project:bat"
)
license=('spdx:MIT OR Apache-2.0')
depends=(
  "${MINGW_PACKAGE_PREFIX}-oniguruma"
  "${MINGW_PACKAGE_PREFIX}-zlib"
  "${MINGW_PACKAGE_PREFIX}-libgit2"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-rust"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
  'git'
)
options=('!strip')
source=("git+${url}.git#tag=v${pkgver}")
sha256sums=('c5e58194fe9c35c7fefe370de70e42da2b940c142d6f0b96bee9bfafeaa61269')

prepare() {
  cd "${_realname}"

  cargo fetch --locked --target "${RUST_CHOST}"
}

build() {
  cd "${_realname}"

  LIBGIT2_NO_VENDOR=1 \
  RUSTONIG_DYNAMIC_LIBONIG=1 \
  RUSTFLAGS="${RUSTFLAGS/+crt-static/-crt-static}" \
    cargo build --release --frozen
}

package() {
  cd "${_realname}"

  install -Dm755 target/release/bat "${pkgdir}${MINGW_PREFIX}/bin/bat"

  # Package licenses
  install -Dm644 LICENSE-{APACHE,MIT} -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"

  cd target/release/build

  # Package the man page
  find . -name bat.1 -type f -exec install -Dm644 {} \
    "${pkgdir}${MINGW_PREFIX}"/share/man/man1/bat.1 \;

  # Package the shell completions
  find . -name bat.bash -type f -exec install -Dm644 {} \
    "${pkgdir}${MINGW_PREFIX}"/share/bash-completion/completions/bat \;

  find . -name bat.zsh -type f -exec install -Dm644 {} \
    "${pkgdir}${MINGW_PREFIX}"/share/zsh/site-functions/_bat \;

  find . -name bat.fish -type f -exec install -Dm644 {} \
    "${pkgdir}${MINGW_PREFIX}"/share/fish/vendor_completions.d/bat.fish \;
}
