# 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.7.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_repository_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"
        "dont-include-unix-progs.patch")
sha256sums=('dc56a3c4632742357d170d60a7dcecb9693de710daeaafa3ad925750b1905522'
            'bd0cb035f590eaea70451dc50211abfc429c33bc80c55193c6f2e80ec76b807f'
            'bf082fc1800e205ee2128ff8729a71ddc52447fa0dd84936c6576e1f6e6c6b0c')
noextract=("tldr-${_tldrver}.zip")

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

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

  patch -p1 -i ../dont-include-unix-progs.patch

  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 \
    MULTICALL=y

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