# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>
# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=coreutils
pkgname="uutils-${_realname}"
pkgver=0.5.0
pkgrel=1
pkgdesc="Cross-platform Rust rewrite of the GNU coreutils"
arch=('any')
url='https://github.com/uutils/coreutils'
msys2_references=(
  'archlinux: uutils-coreutils'
  'purl: pkg:cargo/coreutils'
)
license=('spdx:MIT')
depends=("oniguruma" "gcc-libs")
makedepends=("rust" "pkgconf")
source=("https://github.com/uutils/coreutils/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "nix-0.30.1.tar.gz::https://crates.io/api/v1/crates/nix/0.30.1/download"
        "nix-add-sync.patch")
sha256sums=('83535e10c3273c31baa2f553dfa0ceb4148914e9c1a9c5b00d19fbda5b2d4d7d'
            '74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6'
            '14f430635fb87d0bc12d1b6870365614f7ae259f7c235c420454303250aef7dd')

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

  patch -d ../nix-0.30.1 -p1 -i ../nix-add-sync.patch
  cat >> Cargo.toml <<END

[patch.crates-io]
mio = { git = "https://github.com/tokio-rs/mio", rev = "f352b81d27e39ca3ee1da08c12d8f5795a9dca12" }
nix.path = "../nix-0.30.1"
END

  cargo update -p mio -p nix
  cargo update -p libc --precise 0.2.178
  cargo fetch --locked --target "${RUST_CHOST}"
}

# spliting build() sometimes cause building twice at make build
package() {
  cd "${_realname}-${pkgver}"

  # Remove utils if MinGW version does not have cygwin path issue
  export SKIP_UTILS="arch date expr factor false sleep true uname yes"
  export RUSTONIG_DYNAMIC_LIBONIG=1
  make -O install \
    DESTDIR="${pkgdir}" \
    PREFIX="/usr" \
    PROFILE=release-fast \
    PROG_PREFIX="uu-" \
    MULTICALL=y

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