# Maintainer: Alexey Pavlov <alexpux@gmail.com>
#
# NOTE: Since grep 3.1 support for handling Windows line endings was removed by: 
# https://git.savannah.gnu.org/cgit/grep.git/commit/?h=v3.1&id=5ac3d87a69122d45a837840914c2f9e66b5bb8d4
# https://git.savannah.gnu.org/cgit/grep.git/commit/?h=v3.1&id=c4485ac49a977d666187db4c5c25045215976577
# Removing it lead for MSYS2 broken configure tests for MINGW packages that use autotools.
# See next issues before do grep update:
# https://github.com/msys2/MSYS2-packages/issues/1353
# https://github.com/msys2/MSYS2-packages/pull/2163
# https://github.com/msys2/MINGW-packages/issues/9771


pkgname=grep
pkgver=3.0
pkgrel=3
epoch=1
pkgdesc="A string search utility"
arch=('i686' 'x86_64')
license=('GPL3')
url="https://www.gnu.org/software/grep/"
groups=('base-devel')
depends=('libiconv' 'libintl' 'libpcre' 'sh')
makedepends=('texinfo' 'gettext-devel' 'libiconv-devel' 'pcre-devel')
install=${pkgname}.install
source=(https://ftp.gnu.org/gnu/${pkgname}/${pkgname}-${pkgver}.tar.xz{,.sig}
        grep-2.23-msys2.patch
        grep-3.0-2.src.patch)
sha256sums=('e2c81db5056e3e8c5995f0bb5d0d0e1cad1f6f45c3b2fc77b6e81435aed48ab5'
            'SKIP'
            '9b9b87f5aa12c49e4e4403d27b85fef3f1de252dfe4eed1a7a4f982ef515a95a'
            '0b2e7e54eb4d5b13257c1b4b43bcb234be02ded8fd04dfbbb73a92346e8b2db2')
validpgpkeys=('155D3FC500C834486D1EEA677FD9FCCB000BEEEE')  # Jim Meyering

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

  patch -p1 -i ${srcdir}/grep-2.23-msys2.patch
  # Once this will be commit upstream we need revert it
  #patch -p2 -R -i ${srcdir}/grep-3.0-2.src.patch

  #make test cases work
  sed -i "s|case \$perms in drwx--\[-S\]---\*|case \$perms in drwx\*|" tests/init.sh

  autoreconf -fi
}

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

  echo ${CHOST}
  ./configure \
      --build=${CHOST} \
      --host=${CHOST} \
      --target=${CHOST} \
      --prefix=/usr \
      --without-libiconv-prefix \
      --without-libintl-prefix
  make
}

check() {
  cd ${srcdir}/${pkgname}-${pkgver}
  make check
}

package() {
  cd ${srcdir}/${pkgname}-${pkgver}
  make DESTDIR=${pkgdir} install
}
