pkgname=coreutils
pkgver=9.7
pkgdesc='The basic file, shell and text manipulation utilities of the GNU operating system'
arch=('x86_64')
license=(
  GPL-3.0-or-later
  GFDL-1.3-or-later
)
homepage='https://www.gnu.org/software/coreutils/'
sources=("${pkgname}-${pkgver}.tar.xz"
  "${pkgname}-${pkgver}-upstream_fix-1.patch"
  "${pkgname}-${pkgver}-i18n-1.patch"
)
urls=("https://ftp.gnu.org/gnu/${pkgname}/${sources[0]}"
  "https://www.linuxfromscratch.org/patches/lfs/12.4/${sources[1]}"
  "https://www.linuxfromscratch.org/patches/lfs/12.4/${sources[2]}"
)
md5sums=("6b7285faf7d5eb91592bdd689270d3f1"
  "96382a5aa85d6651a74f94ffb61785d9"
  "33ebfad32b2dfb8417c3335c08671206"
)

src_prepare() {
  tar -xf ${distdir}/${sources[0]} --strip-components=1
  patch -Np1 -i ${distdir}/${sources[1]}
  patch -Np1 -i ${distdir}/${sources[2]}
}

src_build() {
  autoreconf -fv
  automake -af
  FORCE_UNSAFE_CONFIGURE=1 ./configure \
              --prefix=/usr            \
              --enable-no-install-program=kill,uptime
  make
}

src_check() {
  make NON_ROOT_USERNAME=leaf check-root
  # non-root test
  chown -R leaf .
  su leaf -c "PATH=$PATH make -k RUN_EXPENSIVE_TESTS=yes check" \
   < /dev/null
}

src_install() {
  make DESTDIR=${pkgdir} install
  install -d -m 755 ${pkgdir}/usr/sbin/
  mv -v ${pkgdir}/usr/bin/chroot ${pkgdir}/usr/sbin
  install -d -m 755 ${pkgdir}/usr/share/man/man8/
  mv -v ${pkgdir}/usr/share/man/man1/chroot.1 ${pkgdir}/usr/share/man/man8/chroot.8
  sed -i 's/"1"/"8"/' ${pkgdir}/usr/share/man/man8/chroot.8
}
