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

_realname=apr-util
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.6.3
pkgrel=1
pkgdesc="The Apache Portable Runtime (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://apr.apache.org/"
msys2_references=(
  "cpe: cpe:/a:apache:apr-util"
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-apr"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-unixodbc"
         "${MINGW_PACKAGE_PREFIX}-postgresql"
         "${MINGW_PACKAGE_PREFIX}-openldap"
         "${MINGW_PACKAGE_PREFIX}-nss"
         "${MINGW_PACKAGE_PREFIX}-gdbm"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
source=(https://www.apache.org/dist/apr/${_realname}-${pkgver}.tar.bz2{,.asc}
        'plugins.patch'
        'fix-dll-build.patch'
        '001-apr-hooks-add-stdcall.patch'
        '002-add_mod-static.patch')
sha256sums=('a41076e3710746326c3945042994ad9a4fcac0ce0277dd8fea076fec3c9772b5'
            'SKIP'
            '3280d6ed8e577b626e60d495856d16f6944c3144c495fe9ed8cad6b39332824c'
            '2137c28efdc0e6cff22da6413fdf213273b0140f0ecef306e20212f7b3d810aa'
            'afa4ecd31ae2fccc64e792c4f352a4305bd1b9410b5f2090c6cc7caf10c7495f'
            'a4c3c565cbe3d634cf4a583c01bcee605c6a3811260a8b5e16bd26835b87aff4')
# pgp keys are listed here https://people.apache.org/keys/committer/
validpgpkeys=('65B2D44FE74BD5E3DE3AC3F082781DE46D5954FA') # Eric Covener <covener@apache.org>

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

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

  _apply_patch_with_msg \
    plugins.patch \
    fix-dll-build.patch \
    001-apr-hooks-add-stdcall.patch \
    002-add_mod-static.patch

  #./buildconf --with-apr=${MINGW_PREFIX}
  autoreconf -fi
}

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

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --with-apr=${MINGW_PREFIX} \
    --with-expat=${MINGW_PREFIX} \
    --with-iconv=${MINGW_PREFIX} \
    --with-crypto \
    --with-openssl=yes \
    --with-nss=yes \
    --with-dbm=gdbm \
    --with-gdbm=yes \
    --with-berkeley-db=no \
    --with-pgsql=yes \
    --with-sqlite3=yes \
    --with-ldap=yes \
    --with-oracle=no \
    --with-sqlite2=no \
    --with-mysql=yes \
    --with-odbc=yes \
    --with-commoncrypto=no

  make -j1
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  # in case of failing tests
  make -j1 check || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install
}
