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

_realname=delta
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.18.2
pkgrel=2
pkgdesc='A syntax-highlighting pager for git and diff output (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/dandavison/delta"
msys2_references=(
  'archlinux: git-delta'
  'aur'
  'cygwin'
  'purl: pkg:cargo/git-delta'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-oniguruma"
         'git')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust" "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("https://github.com/dandavison/delta/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "ntapi-rs.tar.gz::https://crates.io/api/v1/crates/ntapi/0.4.1/download"
        "ntapi-link-ntdll-arm64.patch")
sha256sums=('64717c3b3335b44a252b8e99713e080cbf7944308b96252bc175317b10004f02'
            'e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4'
            '9994d5b61f020f85f67df54c56c793bd4faf41e39b62ddd4e67c482b9a11b73e')

_env() {
  # Not compatible with libgit2 1.8+
  # export LIBGIT2_NO_VENDOR=1
  export RUSTONIG_DYNAMIC_LIBONIG=1
  export WINAPI_NO_BUNDLED_LIBRARIES=1
}

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

  patch -d ../ntapi-0.4.1 -Np1 -i "${srcdir}/ntapi-link-ntdll-arm64.patch"
  cat >> Cargo.toml <<END

[patch.crates-io]
ntapi = { path = "../ntapi-0.4.1" }
END

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

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

  _env
  cargo build --release --frozen
}

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

  _env
  cargo test --release --frozen
}

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

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

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 etc/completion/completion.bash "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/${_realname}"
  install -Dm644 etc/completion/completion.zsh  "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_${_realname}"
  install -Dm644 etc/completion/completion.fish "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/${_realname}.fish"
}
