# Maintainer: Sébastien "Seblu" Luttringer
# Maintainer: Tobias Powalowski <tpowa@archlinux.org>
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Allan McRae <allan@archlinux.org>
# Contributor: judd <jvinet@zeroflux.org>

pkgname=coreutils
pkgver=9.11
pkgrel=1
pkgdesc='The basic file, shell and text manipulation utilities of the GNU operating system'
arch=('x86_64' 'aarch64')
license=(
  GPL-3.0-or-later
  GFDL-1.3-or-later
)
url='https://www.gnu.org/software/coreutils/'
depends=( 
  acl  
  attr
  glibc
  gmp
  libcap
  openssl
)
makedepends=(
  git
  gperf
  python
  wget
)
source=(
  git+https://https.git.savannah.gnu.org/git/coreutils.git?signed#tag=v${pkgver}
  git+https://https.git.savannah.gnu.org/git/gnulib.git
  https://ftpmirror.gnu.org/gnu/${pkgname}/${pkgname}-${pkgver}.tar.gz{,.sig}
  shorten-test-filenames.patch
)
validpgpkeys=(
 6C37DC12121A5006BC1DB804DF6FD971306037D9 # Pádraig Brady
)
options=(!lto)
b2sums=('24d9727afb79279eccab35496452462d881d3ef6031739e44aeee53c2379a04e42a7c8dad8ae03e0cdc9befbfea44d6b4884829f7bb638a441e22bf31a5e5d2e'
        'SKIP'
        '6c3e1763df577c9bebb91fcf7ee3ce56dcb2aa3cb911bebbf2ec3493cad1a0cbeffb2ab7caeb0351fba92e8b631ae4dfee7a32d9542d8e25c4dcdd2fec8cbcf3'
        'SKIP'
        'SKIP')

prepare() {
  cd "${pkgname}"
  # Skip downloading unstable po files in bootstrap scripts to avoid non-deterministic builds
  export SKIP_PO="1"
  # The $SKIP_PO environment variable is only honored by the gnulib submodule if using its "sh" implementation
  export GNULIB_TOOL_IMPL="sh"

  git submodule init
  git config submodule.gnulib.url ../gnulib
  git -c protocol.file.allow=always submodule update

  ./bootstrap

  # apply patch from the source array (should be a pacman feature)
  local src
  for src in "${source[@]}"; do
    src="${src%%::*}"
    src="${src##*/}"
    [[ $src = *.patch ]] || continue
    echo "Applying patch $src..."
    patch -Np1 < "../$src"
  done
}

build() {
  cd "${pkgname}"
  ./configure \
    --prefix=/usr \
    --libexecdir=/usr/lib \
    --with-openssl
  make
  
  # Generate coreutils mo files from dist tarball
  cd "${srcdir}/${pkgname}-${pkgver}/po"
  for po in *.po; do
    msgfmt "${po}" -o "${po%.po}.mo"
  done
}

check() {
  cd "${pkgname}"
  make check
}

package() {
  cd "${pkgname}"
  make DESTDIR="${pkgdir}" install

  # Install coreutils mo files from dist tarball
  cd "${srcdir}/${pkgname}-${pkgver}/po"
  for mo in *.mo; do
    install -Dm 644 "${mo}" "${pkgdir}/usr/share/locale/${mo%.mo}/LC_MESSAGES/${pkgname}.mo"
    install -Dm 644 "${mo}" "${pkgdir}/usr/share/locale/${mo%.mo}/LC_TIME/${pkgname}.mo"
  done
}

