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

pkgbase=pcre
pkgname=('pcre' 'libpcre' 'libpcre16' 'libpcre32' 'libpcrecpp' 'libpcreposix' 'pcre-devel')
pkgver=8.45
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}
        pcre-8.33-msys2-fix-ln.patch
        pcre-8.33-msys2.patch)
sha256sums=('4dae6fdcd2bb0bb6c37b5f97c33c2be954da743985369cddac3546e3218bffb8'
            'SKIP'
            'ef9fffef05c53450d196c7997a7de8883e58b8d8a5bbc0abb412d8a22797bb34'
            '6e6e42b6a11bbb254541b0c6ea033cab5054272dfb54d7568976fcd08c5bd490')
validpgpkeys=('45F68D54BBE23FB3039B46E59766E084FB0F43D8') # Philip Hazel <ph10@hermes.cam.ac.uk>

prepare() {
  cd "${srcdir}"/${pkgname}-${pkgver}
  patch -p1 -i ${srcdir}/pcre-8.33-msys2-fix-ln.patch
  patch -p1 -i ${srcdir}/pcre-8.33-msys2.patch

  autoreconf -fi
}

build() {
  cd "${srcdir}"/${pkgname}-${pkgver}
  ./configure --build=${CHOST} \
      --prefix=/usr \
      --enable-pcre16 \
      --enable-pcre32 \
      --enable-newline-is-anycrlf \
      --enable-unicode-properties \
      --enable-utf8 \
      --enable-pcregrep-libbz2 \
      --enable-pcregrep-libz \
      --enable-pcretest-libreadline \
      --disable-stack-for-recursion

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

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

package_pcre() {
  depends=('libreadline' 'libbz2' 'zlib' 'libpcre' 'libpcre16' 'libpcre32' 'libpcrecpp' 'libpcreposix')

  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_libpcre() {
  groups=('libraries')
  depends=('gcc-libs')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*pcre-1.dll ${pkgdir}/usr/bin/
}

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

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

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

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

package_libpcrecpp() {
  groups=('libraries')
  depends=('libpcre' 'gcc-libs')

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

package_libpcreposix() {
  depends=('libpcre')
  groups=('libraries')

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

package_pcre-devel() {
  pkgdesc="PCRE headers and libraries"
  options=('staticlibs')
  groups=('development')
  depends=("libpcre=$pkgver" "libpcre16=$pkgver" "libpcre32=$pkgver" "libpcreposix=$pkgver" "libpcrecpp=$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/
}
