# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>
# Contributor: Johannes Schindelin <johannes.schindelin@gmx.de>

_realname=jj
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=0.36.0
pkgrel=1
pkgdesc="Jujutsu (an experimental VCS) (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/jj-vcs/jj"
msys2_documentation_url="https://docs.jj-vcs.dev/latest/"
msys2_changelog_url='https://github.com/jj-vcs/jj/blob/main/CHANGELOG.md'
msys2_references=(
  'anitya: 376676'
  'archlinux: jujutsu'
  'aur: jujutsu-git'
  'purl: pkg:cargo/jj-cli'
)
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             'git')
source=("git+${url}.git#tag=v${pkgver}")
sha256sums=('09f9aef8354b1fbcf5e736db14f22478baa20bdfcba82f1a1ec67d93b9664c86')

prepare() {
  cd "${_realname}"

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

build() {
  cd "${_realname}"

  cargo build --release --frozen -p jj-cli
}

check() {
  cd "${_realname}"

  cargo test --release --frozen -p jj-cli
}

package_jj() {
  cd "${_realname}"

  cargo install \
    --frozen \
    --offline \
    --no-track \
    --path cli \
    --root "${pkgdir}${MINGW_PREFIX}"

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

  local _complete="target/release/jj util completion"
  $_complete bash | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/jj"
  $_complete fish | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/jj.fish"
  $_complete zsh | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_jj"
}

package_jj-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  install -Dm644 "${_realname}"/README.md "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/README.md"
  cp -r "${_realname}"/docs "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
