# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=coreutils
pkgver=8.32
pkgrel=5
pkgdesc="The basic file, shell and text manipulation utilities of the GNU operating system"
arch=('i686' 'x86_64')
license=('GPL3')
url="https://www.gnu.org/software/coreutils"
msys2_references=(
  "cpe: cpe:/a:gnu:coreutils"
)
depends=('gmp' 'libiconv' 'libintl')
makedepends=('groff' 'gmp-devel' 'libiconv-devel' 'gettext-devel' 'help2man' 'autotools' 'gcc')
source=(https://ftp.gnu.org/gnu/${pkgname}/${pkgname}-${pkgver}.tar.xz{,.sig}
        001-coreutils-8.30.patch
        002-coreutils-8.32-enable-stdbuf.patch
        003-coreutils-8.32-fix-test-cases.patch
        004-msystem-osname-cygwin.patch)
sha256sums=('4458d8de7849df44ccab15e16b1548b285224dbba5f08fac070c1c0e0bcc4cfa'
            'SKIP'
            '467bde24da0ccea48260f58d3c94a84de4e027ab598a926003642f791da47ca2'
            'e3be62b9aceb3231f09f05bc3bd8b18f6aaa495f5063781cdd261d560a7e80d0'
            'ad9e0582373500c0668e38483401169f48d6a2f8ad8d28ef8e7b06e4a3e08a74'
            '3922afd54b2323772f5cbd0638d0887ef858199bbaf4076a1db318173021c3dc')
validpgpkeys=('6C37DC12121A5006BC1DB804DF6FD971306037D9')

prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}

  patch -p1 -i ${srcdir}/001-coreutils-8.30.patch
  patch -p1 -i ${srcdir}/002-coreutils-8.32-enable-stdbuf.patch
  patch -p1 -i ${srcdir}/003-coreutils-8.32-fix-test-cases.patch

  # https://github.com/msys2/MSYS2-packages/issues/3012
  patch -p1 -i ${srcdir}/004-msystem-osname-cygwin.patch

  autoreconf -fi
}

build() {
  [[ -d ${srcdir}/build-${CHOST} ]] && rm -rf ${srcdir}/build-${CHOST}
  mkdir -p ${srcdir}/build-${CHOST} && cd ${srcdir}/build-${CHOST}

  export gl_cv_have_weak=no

  ../${pkgname}-${pkgver}/configure \
    --build=${CHOST} \
    --prefix=/usr \
    --libexecdir=/usr/lib \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --program-transform-name=s/kill/gkill/ \
    --without-libintl-prefix \
    --without-libiconv-prefix \
    --enable-install-program=arch,hostname \
    --enable-no-install-program=uptime \
    gl_cv_func_strtod_works=yes \
    gl_cv_func_strtold_works=yes

  make
}

check() {
  cd ${srcdir}/build-${CHOST}
  make check
}

package() {
  cd ${srcdir}/build-${CHOST}
  make DESTDIR=${pkgdir} install

  test ! -f ${pkgdir}/usr/lib/coreutils/libstdbuf.so.exe ||
  mv ${pkgdir}/usr/lib/coreutils/libstdbuf.so.exe \
    ${pkgdir}/usr/lib/coreutils/libstdbuf.dll

  install -Dm644 ${srcdir}/${pkgname}-${pkgver}/src/dircolors.hin ${pkgdir}/etc/DIR_COLORS
}
