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

pkgbase=pcre2
pkgname=('pcre2' 'libpcre2_8' 'libpcre2_16' 'libpcre2_32' 'libpcre2posix' 'pcre2-devel')
pkgver=10.37
pkgrel=1
pkgdesc="A library that implements Perl 5-style regular expressions"
arch=('i686' 'x86_64')
url="https://www.pcre.org/"
license=('BSD')
makedepends=('libreadline-devel' 'libbz2-devel' 'zlib-devel')
source=(https://ftp.pcre.org/pub/pcre/${pkgname}-${pkgver}.tar.bz2{,.sig}
        pcre2-10.31-msys2.patch)
sha256sums=('4d95a96e8b80529893b4562be12648d798b957b1ba1aae39606bbc2ab956d270'
            'SKIP'
            '3b1b15dbc5bb56cddd12069eb1173c93f76edf7355a1ee1da84f96333b1a5265')
validpgpkeys=('45F68D54BBE23FB3039B46E59766E084FB0F43D8') # Philip Hazel <ph10@hermes.cam.ac.uk>

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

  # MSYS2 patches
  patch -p1 -i ${srcdir}/pcre2-10.31-msys2.patch
  autoreconf -fi
}

build() {
  cd "${srcdir}"/${pkgname}-${pkgver}
  ./configure \
      --build=${CHOST} \
      --prefix=/usr \
      --disable-jit \
      --enable-pcre2-8 \
      --enable-pcre2-16 \
      --enable-pcre2-32 \
      --enable-newline-is-anycrlf \
      --enable-unicode \
      --disable-pcre2grep-jit \
      --enable-pcre2grep-libbz2 \
      --enable-pcre2grep-libz \
      --disable-pcre2test-libedit \
      --enable-pcre2test-libreadline \
      ac_cv_header_windows_h=no

  make
  make DESTDIR="${srcdir}/dest" install
}

check() {
  cd "${srcdir}"/${pkgname}-${pkgver}
  make -j1 check || true
}

package_pcre2() {
  depends=('libreadline' 'libbz2' 'zlib' "libpcre2_8=${pkgver}" "libpcre2_16=${pkgver}" "libpcre2_32=${pkgver}" "libpcre2posix=${pkgver}")

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
  install -Dm644 ${srcdir}/${pkgname}-${pkgver}/LICENCE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}

package_libpcre2_8() {
  groups=('libraries')
  depends=('gcc-libs')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*pcre2-8-0.dll ${pkgdir}/usr/bin/
}

package_libpcre2_16() {
  groups=('libraries')
  depends=('gcc-libs')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*pcre2-16-0.dll ${pkgdir}/usr/bin/
}

package_libpcre2_32() {
  groups=('libraries')
  depends=('gcc-libs')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*pcre2-32-0.dll ${pkgdir}/usr/bin/
}

package_libpcre2posix() {
  depends=("libpcre2_8=${pkgver}")
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*pcre2-posix-3.dll ${pkgdir}/usr/bin/
}

package_pcre2-devel() {
  pkgdesc="PCRE headers and libraries"
  options=('staticlibs')
  groups=('development')
  depends=("libpcre2_8=${pkgver}" "libpcre2_16=${pkgver}" "libpcre2_32=${pkgver}" "libpcre2posix=${pkgver}")

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*-config ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
}
