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

pkgbase=pcre2
pkgname=('pcre2' 'libpcre2_8' 'libpcre2_16' 'libpcre2_32' 'libpcre2posix' 'pcre2-devel')
pkgver=10.46
pkgrel=1
pkgdesc="A library that implements Perl 5-style regular expressions"
arch=('i686' 'x86_64')
url="https://www.pcre.org/"
msys2_repository_url="https://github.com/PCRE2Project/pcre2"
msys2_changelog_url="https://github.com/PCRE2Project/pcre2/blob/master/NEWS"
msys2_references=(
  "archlinux: pcre2"
  "cpe: cpe:/a:pcre:pcre"
  "cpe: cpe:/a:pcre:pcre2"
)
license=('BSD')
makedepends=('libreadline-devel' 'libbz2-devel' 'zlib-devel' 'autotools' 'gcc')
source=(https://github.com/PCRE2Project/pcre2/releases/download/${pkgname}-${pkgver}/${pkgname}-${pkgver}.tar.bz2{,.sig})
sha256sums=('15fbc5aba6beee0b17aecb04602ae39432393aba1ebd8e39b7cabf7db883299f'
            'SKIP')
validpgpkeys=('A95536204A3BB489715231282A98E77EB6F24CA8') # Nicholas Wilson <nicholas@nicholaswilson.me.uk>

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

  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.md "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE.md
}

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/
}
