# 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.5.0
pkgrel=1
_tldrver=2.3
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"
        "tldr-${_tldrver}.zip::https://github.com/tldr-pages/tldr/releases/download/v${_tldrver}/tldr.zip")
sha256sums=('83535e10c3273c31baa2f553dfa0ceb4148914e9c1a9c5b00d19fbda5b2d4d7d'
            '30ab57b78bfd8ff51a5d4acfb2d8e86a9e281437bf665dd0257ebd13d70c507b')
noextract=("tldr-${_tldrver}.zip")

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

  # Embedded tldr example to manpages
  mv "${srcdir}/tldr-${_tldrver}.zip" docs/tldr.zip

  cargo fetch --locked --target "${RUST_CHOST}"
}

# 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 install \
    DESTDIR="${pkgdir}" \
    PREFIX="${MINGW_PREFIX}" \
    PROG_PREFIX=uu- \
    PROFILE=release-fast \
    MULTICALL=y

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