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

_realname=coreutils
pkgbase=mingw-w64-uutils-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-uutils-${_realname}")
pkgver=0.2.2
pkgrel=2
pkgdesc="Cross-platform Rust rewrite of the GNU coreutils (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/uutils/coreutils'
msys2_references=(
  'archlinux: uutils-coreutils'
  'purl: pkg:cargo/coreutils'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-oniguruma" "${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust" "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("https://github.com/uutils/coreutils/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "support-custom-ln.patch"
        "https://github.com/uutils/coreutils/commit/ae5470e7f3141edaf48e0025ae6d80ff0d988d68.patch"
        "https://patch-diff.githubusercontent.com/raw/uutils/coreutils/pull/8926.patch")
sha256sums=('4a847a3aaf241d11f07fdc04ef36d73c722759675858665bc17e94f56c4fbfb3'
            '2a6619e8732ede7802fb81256084561a6635947ac7697064334ad06dfa08b223'
            '1cdd32a98b15d3b57c5964572693aebe22a9e88af50328be850d58b9c910459c'
            'fff2bb0b31bf5cb8362fe19df8fae5fdd2d6a027b38a27f1ad5551c653358177')

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

  patch -Np1 -i ../support-custom-ln.patch
  patch -Np1 -i ../ae5470e7f3141edaf48e0025ae6d80ff0d988d68.patch
  patch -Np1 -i ../8926.patch

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

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

  export RUSTONIG_DYNAMIC_LIBONIG=1
  export RUSTFLAGS="${RUSTFLAGS/+crt-static/-crt-static}"
  make -O install \
    DESTDIR="${pkgdir}" \
    PREFIX="${MINGW_PREFIX}" \
    PROG_PREFIX=uu- \
    PROFILE=release \
    MULTICALL=y \
    LN="ln -vf"

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