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

_realname=dust
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.2.3
pkgrel=1
pkgdesc="A more intuitive version of du in rust (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/bootandy/dust'
msys2_references=(
  'archlinux: dust'
  'purl: pkg:cargo/du-dust'
)
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust")
source=("${url}/archive/v${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=('424b26adfbafeac31da269ecb3f189eca09803e60fad90b3ff692cf52e0aeeee'
            'e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4'
            '9994d5b61f020f85f67df54c56c793bd4faf41e39b62ddd4e67c482b9a11b73e')

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

  patch -d ../ntapi-0.4.1 -i ../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 "${RUST_CHOST}"
}

build() {
  cp -r "${_realname}-${pkgver}" "build-${MSYSTEM}" && cd "build-${MSYSTEM}"

  export WINAPI_NO_BUNDLED_LIBRARIES=1
  cargo build --release --frozen
}

package() {
  cd "build-${MSYSTEM}"

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

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

  install -Dm644 completions/dust.bash "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/dust"
  install -Dm644 completions/dust.fish "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/dust.fish"
  install -Dm644 completions/_dust "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_dust"
  install -Dm644 man-page/dust.1 "${pkgdir}${MINGW_PREFIX}/share/man/man1/dust.1"
}
