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

_realname=coreutils
pkgname="uutils-${_realname}"
pkgver=0.6.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=("gcc-libs" "oniguruma")
makedepends=("git" "rust" "pkgconf")
source=("https://github.com/uutils/coreutils/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "git+https://github.com/nix-rust/nix#tag=v0.30.1"
        "nix-ioctl.patch")
sha256sums=('f751b8209ec05ae304941a727e42a668dcc45674986252f44d195ed43ccfad2f'
            'aebec7d9c51d26d003333bc53158ac75b8e960ca5cb75d24fe8a5cb027c59842'
            'b0b8db1334dfcbc805d087969e1860e67ec0dc13a54589741b22684ee3144cee')

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

  git -C ../nix cherry-pick -n 71f6ee9f4380d5caad3c049dbd352627b9d6eb0a # add sync
  git -C ../nix apply -v -p1 ../nix-ioctl.patch
  cat >> Cargo.toml <<END

[patch.crates-io]
nix.path = "../nix"
END

  cargo update -p nix
  cargo fetch --locked --target "${RUST_CHOST}"
}

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

  export RUSTONIG_DYNAMIC_LIBONIG=1

  make install \
    DESTDIR="${pkgdir}" \
    PREFIX="/usr" \
    PROFILE=release-fast \
    SKIP_UTILS="stdbuf" \
    PROG_PREFIX="uu-" \
    MULTICALL=y

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