_realname=watchexec
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.3.1
pkgrel=1
msys2_repository_url="https://github.com/watchexec/watchexec/"
url="https://watchexec.github.io/"
pkgdesc="Executes commands in response to file modifications (mingw-w64)"
license=('spdx:Apache-2.0')
msys2_references=(
  'archlinux: watchexec'
  'purl: pkg:cargo/watchexec-cli'
)
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust")
options=('!strip')
source=("https://github.com/watchexec/watchexec/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('b4d8199ad4f697a43122769b321ecd52a9020bfe21e5cb960a857ef2734ef86a')

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

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

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

  WINAPI_NO_BUNDLED_LIBRARIES=1 \
    cargo build --release --frozen -p watchexec-cli
}

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

  WINAPI_NO_BUNDLED_LIBRARIES=1 \
    cargo test --release --frozen -p watchexec-cli
}

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

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

  install -Dm644 completions/bash "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/watchexec"
  install -Dm644 completions/zsh "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_watchexec"
  install -Dm644 completions/fish "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/watchexec.fish"
  install -Dm644 doc/watchexec.1 "${pkgdir}${MINGW_PREFIX}/share/man/man1/watchexec.1"
  install -Dm644 doc/watchexec.1.md "${pkgdir}${MINGW_PREFIX}/share/doc/watchexec/watchexec.1.md"
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/watchexec/LICENSE"
}
