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

_realname=build
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.5.1
pkgrel=1
pkgdesc="A simple, correct PEP517 package builder (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://github.com/pypa/build'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python-colorama"
         "${MINGW_PACKAGE_PREFIX}-python-packaging"
         "${MINGW_PACKAGE_PREFIX}-python-pep517"
         "${MINGW_PACKAGE_PREFIX}-python-toml"
         "${MINGW_PACKAGE_PREFIX}-python-virtualenv")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-mock"
            "${MINGW_PACKAGE_PREFIX}-python-filelock"
            "${MINGW_PACKAGE_PREFIX}-python-pytest")
source=("${_realname}-$pkgver.tar.gz"::"https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "0001-pass-ignore_errors-to-shutil.patch")
sha512sums=('72e82ed1f4d6185a862726b2c2f3b4bd05c3105ff5c3bf8e7595c0c1e0ab457304eda7849c2312e41a3bb4a9262c1099263fc87c3a759fc801dbe5cb6ccaef52'
            '759940325ebaa073f3adb6b59b9afc8a7bf85583db1b98d48effca2f4c26602b134905778a72041855eef177cca317d9804c89f76d0a51262688f2c0274c9dd7')


prepare() {
  rm -rf python-build-${CARCH}| true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
  cd "python-build-${CARCH}"
  patch -p1 -i ${srcdir}/0001-pass-ignore_errors-to-shutil.patch
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

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

check() {
  msg "Python test for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"

  PYTHONPATH=src ${MINGW_PREFIX}/bin/python -m pytest
}

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

  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -i 's/\#\!.*//' $_f
  done
}
