# Maintainer: Drew Waranis <drew@waran.is>

_realname=apr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.7.6
pkgrel=1
pkgdesc="The Apache Portable Runtime (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://apr.apache.org/"
msys2_references=(
  "cpe: cpe:/a:apache:portable_runtime"
)
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-python")
source=(https://downloads.apache.org/apr/${_realname}-${pkgver}.tar.bz2{,.asc}
        0002-apr-remove-full-path.patch
        0003-apr-prevent-override-build-flags.patch
        0004-apr-install-missing-m4-files.patch
        'apr_ssize_t.patch'
        'apr_wtypes.patch')
sha256sums=('49030d92d2575da735791b496dc322f3ce5cff9494779ba8cc28c7f46c5deb32'
            'SKIP'
            'cc52201e09dbb080ffc259a57e1dd27267c751e2d3be8368d82adbbeb9de46c8'
            'd11b3ec7dbd71cfc2c6677dcc06f01adf9182881f3edb7f70f562a51923e8209'
            '254686b307b8c5189aa467eff159d99521571eab26864ce9da5b22a6366451c8'
            'ba7d6de7e7930801df483d444b97c159af4ff11b4ce27e1337aea5e0417e6066'
            'b82dd98ec8cff2273fb071dc9f1d2ee7466905c9b82a12d3d83ce1cb5920a5d6')
# pgp keys are listed here https://downloads.apache.org/apr/KEYS
validpgpkeys=('CBA1525BAA7A47BE28FF23DDD3B13FCA2CE60BED') # Graham Leggett <minfrin@redwax.eu>

_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 \
    0002-apr-remove-full-path.patch \
    0003-apr-prevent-override-build-flags.patch \
    0004-apr-install-missing-m4-files.patch

  patch -p0 -i "${srcdir}"/apr_ssize_t.patch
  patch -p0 -i "${srcdir}"/apr_wtypes.patch

  ./buildconf
  # autoreconf -fi
}

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

  CFLAGS+=" -Wno-incompatible-pointer-types"

  # Disable IPv6.
  ../${_realname}-${pkgver}/configure \
    --prefix="${MINGW_PREFIX}" \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-static \
    --enable-shared \
    --includedir="${MINGW_PREFIX}/include/apr-1" \
    --with-installbuilddir="${MINGW_PREFIX}/share/apr-1/build" \
    --enable-nonportable-atomics \
    --with-devrandom=/dev/urandom \
    --disable-ipv6

  make
}

#check() {
#  cd "${srcdir}/build-${MSYSTEM}"
#  make -j1 check
#}

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

  # remove full path reference
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}"/share/apr-1/build/libtool
}
