# 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=1
pkgdesc="Python bindings for Oracle Berkeley DB (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
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")
options=('staticlibs' 'strip' '!debug')
source=("https://pypi.python.org/packages/source/b/bsddb3/${_realname}-${pkgver}.tar.gz"
        db-6.0-support.patch)
sha256sums=('70d05ec8dc568f42e70fc919a442e0daadc2a905a1cfb7ca77f549d49d6e7801'
            'b96b05360f9a86af62e4c644be0fffc368f755d89ce14e23a93a75442c570cd6')

prepare() {
  plain "Patching setup files ..."
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/db-6.0-support.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"

  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
}
