# Contributor: Josip <bpisoj@gmail.com>

_realname=bsddb3
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=6.2.9
pkgrel=6
pkgdesc="Python bindings for Oracle Berkeley DB (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('BSD')
url="https://www.jcea.es/programacion/pybsddb.htm"
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-db")
makedepends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools" "${MINGW_PACKAGE_PREFIX}-cc")
options=('staticlibs' 'strip' '!debug')
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-${CARCH}" | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  cd "${srcdir}/python-build-${CARCH}"

  CFLAGS+=" -DUNICODE -D_UNICODE"

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

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=;--install-purelib=;--install-data=" \
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}/python-build-${CARCH}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=;--install-purelib=;--install-data=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build
}
