# Contributor: Josip <bpisoj@gmail.com>

_realname=bsddb3
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=6.2.9
pkgrel=11
pkgdesc="Python bindings for Oracle Berkeley DB (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.jcea.es/programacion/pybsddb.htm"
msys2_repository_url="https://hg.jcea.es/pybsddb/"
msys2_references=(
  'archlinux: python-bsddb'
  'purl: pkg:pypi/bsddb3'
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-db")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-python-setuptools"
  "${MINGW_PACKAGE_PREFIX}-python-build"
  "${MINGW_PACKAGE_PREFIX}-python-installer"
)
source=("https://pypi.python.org/packages/source/b/bsddb3/${_realname}-${pkgver}.tar.gz"
        db-6.0-support.patch
        unsupported-runtime-dirs.patch)
sha256sums=('70d05ec8dc568f42e70fc919a442e0daadc2a905a1cfb7ca77f549d49d6e7801'
            'b96b05360f9a86af62e4c644be0fffc368f755d89ce14e23a93a75442c570cd6'
            '1f686654150ff23c485c34a63d664d537e4ac47316e1f0ef74a9644e27d77442')

prepare() {
  plain "Patching setup files ..."
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/db-6.0-support.patch
  patch -p1 -i "${srcdir}"/unsupported-runtime-dirs.patch

  sed -i -e "s|if os.name == 'posix':|if os.name == 'nt':|g" setup{2,3}.py
  sed -i -e "s|elif os.name == 'nt':|elif os.name == 'FOO':|g" setup{2,3}.py

  cd "${srcdir}"
  rm -rf "python-build-${MSYSTEM}" | true
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"
}

build() {
  cd "python-build-${MSYSTEM}"

  CFLAGS+=" -DUNICODE -D_UNICODE -Wno-error=incompatible-pointer-types"

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CFLAGS+=" -Wno-incompatible-function-pointer-types"
  fi

  export YES_I_HAVE_THE_RIGHT_TO_USE_THIS_BERKELEY_DB_VERSION=YES
  export BERKELEYDB_DIR="${MINGW_PREFIX}"

  python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd "python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl
}
