# Maintainer: @naveen521kk on Github Naveen M K <naveen521kk@gmail.com>

_realname=poetry-core
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=1.0.3
pkgrel=1
pkgdesc="Poetry PEP 517 Build Backend (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/python-poetry/poetry-core"
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-build")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-mock"
            "${MINGW_PACKAGE_PREFIX}-python-pep517"
            "${MINGW_PACKAGE_PREFIX}-python-pytest")
depends=("${MINGW_PACKAGE_PREFIX}-python")
source=("${_realname}-$pkgver.tar.gz"::"https://github.com/python-poetry/${_realname}/archive/${pkgver}.tar.gz"
        "0001-mingw-fix-tests.patch")
sha512sums=('ca3b599890e0ebe96b0a45c174b911c45652711db876666f4fabbe2c8cfb839b22d21d95319bb8525391d7fb05bd854ff27d3a21bb9017c18da665dc97c65fca'
            'f58cfb6fb02c50c6528ecbf2efd0acdc0eb25a0ebffed45d5a25e089cc5af6ee727deb8bd6a9c816907b2e2017ae674d7f149d0efbc63e34648541783230f88e')

prepare() {
  cp -r "${srcdir}/${_realname}-${pkgver}" "$TMP/${_realname}-${pkgver}"
  
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/0001-mingw-fix-tests.patch
  # Directly running build here creates an archive
  # which doesn't has the package, here `poetry`.
  # We should move to temporary directory and create
  # the archive there and then proceed like a normal 
  # python install.
  cd "$TMP/${_realname}-${pkgver}"
  ${MINGW_PREFIX}/bin/python -m build . -s -x -n
  cd dist
  tar -xf ${_realname}-$pkgver.tar.gz
  rm -rf ${srcdir}/python-build-${CARCH}| true
  cp -r "${_realname}-${pkgver}" ${srcdir}/"python-build-${CARCH}"
}

check(){
  cd "${srcdir}/${_realname}-${pkgver}"
  ${MINGW_PREFIX}/bin/python -m pytest || echo "test failed"
}

build() {
  msg "Python build for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -D -m644 LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/LICENSE
}
