# Maintainer: Egor Savkin <es@m-labs.hk>

_realname=lmdb
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=1.6.2
pkgrel=1
pkgdesc="Universal Python binding for the LMDB 'Lightning' Database (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/jnwatson/py-lmdb"
msys2_references=(
  'purl: pkg:pypi/lmdb'
  "cpe: cpe:/a:py-lmdb_project:py-lmdb"
)
license=('spdx:OLDAP-2.8')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-lmdb")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-build")
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "001-py-lmdb-1.4.1-mingw-build.patch")
sha256sums=('d28e3fa59935ff688858760ec52f202ecb8c1089a3f68d1f162ea3078d151e73'
            '231fef4195024fb3db57d853f471e8cb9ccc3e60b54b39dc18a6dc6026b37a86')

prepare() {
  cd "${srcdir}"
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"
  cd "python-build-${MSYSTEM}"
  patch -Np1 -i "${srcdir}"/001-py-lmdb-1.4.1-mingw-build.patch
  export LMDB_FORCE_SYSTEM=1
  export LMDB_BUILDLIB=build/lib.`python -c "import sysconfig;print(sysconfig.get_platform().replace('-', '_').replace('.', '_')+'-'+sysconfig.get_python_version())"`
}

build() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  cp -r tests ${LMDB_BUILDLIB}/tests
  "${MINGW_PREFIX}"/bin/python setup.py check
  cd ${LMDB_BUILDLIB}
  "${MINGW_PREFIX}"/bin/python tests/crash_test.py
  "${MINGW_PREFIX}"/bin/python tests/cursor_test.py
  "${MINGW_PREFIX}"/bin/python tests/getmulti_test.py
  "${MINGW_PREFIX}"/bin/python tests/iteration_test.py
  "${MINGW_PREFIX}"/bin/python tests/package_test.py
  "${MINGW_PREFIX}"/bin/python tests/tool_test.py
  "${MINGW_PREFIX}"/bin/python tests/txn_test.py
  "${MINGW_PREFIX}"/bin/python tests/env_test.py
  rm -rf tests
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-purelib=;" \
  ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl
  install -D -m644 LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/LICENSE
}

