# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=autoconf
pkgname=${_realname}2.71
pkgver=2.71
pkgrel=4
pkgdesc="A GNU tool for automatically configuring source code"
arch=('any')
license=('spdx:GPL-2.0-or-later' 'spdx:GPL-3.0-or-later' 'spdx:Autoconf-exception-3.0')
url="https://www.gnu.org/software/autoconf"
depends=('awk' 'm4' 'diffutils' 'bash' 'perl' 'sed')
makedepends=('make')
source=(https://ftp.gnu.org/pub/gnu/${_realname}/${_realname}-${pkgver}.tar.xz
        0001-autoconf-2.70-MSYS2-specifics-AC_CYGWIN.patch
        0002-autoconf-2.70-MSYS2-Autotest-Unify-EOLs-By-Default.patch
        0003-autoconf-2.70-MSYS2-specifics-config.guess.patch
        0004-LLVM-Flang.patch
        0005-package.patch
        autoconf-2.71-AC_C_BIGENDIAN-lto.patch
        autoconf-2.71-AC_LANG_CALL_C_cxx.patch
        autoconf-2.71-K-R-decls-clang-deux.patch
        autoconf-2.71-K-R-decls-clang.patch
        autoconf-2.71-make-4.4.patch)
sha256sums=('f14c83cfebcc9427f2c3cea7258bd90df972d92eb26752da4ddad81c87a0faa4'
            '3a5337e2269407405ee4c7feb12dbe2ae711610c3144c07cd085272676005db6'
            'c4e4952cc63a2ddaf7bf539f6e59353c4fadbf04cd48c3cfa789f8b175d0bd27'
            'a7671bef559a5f3edbd811a7cf9b557f216f1d3e9502dc046fd3a0ea665da7da'
            'b055f013c85720dabe21947d2dbb67d0a282383d44b39d1a78ba05949834e649'
            '01894e38f901629373ff8ca6c55571b14450eacc625e717cd0da0ca65860f5e5'
            '1499271fa3004079bc2ce9db2697a7f4bd29666b2dfd7e1c39ad8fa397a4c08e'
            'befc2e2ae932e2247eee7fdd17d17e6b3065a741a47e0154670a009907d0860b'
            'c0e2215f983763587521c3925e6ebb73c6e2e8cd9ac1e5d649720252a26cca78'
            '65f2b02e96db437fc8fa974ba4b9db76eb9d053ceebcaf309c19f87eae3f8158'
            '402350041cbfb092321a5998f6abec6b0c59d3d03ae63735120e96a5a4a09d47')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

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

  # MSYS2
  apply_patch_with_msg \
    0001-autoconf-2.70-MSYS2-specifics-AC_CYGWIN.patch \
    0002-autoconf-2.70-MSYS2-Autotest-Unify-EOLs-By-Default.patch \
    0003-autoconf-2.70-MSYS2-specifics-config.guess.patch \
    0004-LLVM-Flang.patch \
    0005-package.patch

  # from gentoo: https://gitweb.gentoo.org/repo/gentoo.git/tree/sys-devel/autoconf/files
  apply_patch_with_msg \
    autoconf-2.71-AC_LANG_CALL_C_cxx.patch \
    autoconf-2.71-AC_C_BIGENDIAN-lto.patch \
    autoconf-2.71-K-R-decls-clang.patch \
    autoconf-2.71-make-4.4.patch \
    autoconf-2.71-K-R-decls-clang-deux.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  ../${_realname}-${pkgver}/configure \
    --prefix=/usr \
    --program-suffix=-2.71

  make
}

check() {
  cd build-${MSYSTEM}
  # CAUTION: The tests run very very long, even on a server.
  make check TESTSUITEFLAGS='-j16'
}

package() {
  make -C build-${MSYSTEM} DESTDIR=${pkgdir} install

  # license exception
  install -Dm644 ${_realname}-${pkgver}/COPYING.EXCEPTION \
    $pkgdir/usr/share/licenses/autoconf${pkgver}/COPYING.EXCEPTION

  find ${pkgdir}/usr/share

  # conflict with bintuils
  rm -f ${pkgdir}/usr/share/info/standards.info
  # conflicts with autoconf
  rm -rf ${pkgdir}/usr/share/{emacs,info}
}
