# Maintainer: Marcelo Duarte @marcelotduarte
# makepkg-mingw -sCLf

_realname=freeze-core
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.4.2
pkgrel=1
pkgdesc="Core dependency for cx_Freeze (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/marcelotduarte/freeze-core/"
msys2_references=(
  'pypi: freeze-core'
)
license=('spdx:MIT')
depends=(
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-python-cabarchive"
  "${MINGW_PACKAGE_PREFIX}-python-filelock"
  "${MINGW_PACKAGE_PREFIX}-python-striprtf"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-tools"
  "${MINGW_PACKAGE_PREFIX}-python-build"
  "${MINGW_PACKAGE_PREFIX}-python-installer"
  "${MINGW_PACKAGE_PREFIX}-python-setuptools"
  "${MINGW_PACKAGE_PREFIX}-python-cx-logging"
)
checkdepends=(
  "${MINGW_PACKAGE_PREFIX}-python-pip"
  "${MINGW_PACKAGE_PREFIX}-python-coverage"
  "${MINGW_PACKAGE_PREFIX}-python-pytest"
)
options=(!strip)
source=(
  "https://pypi.org/packages/source/${_realname::1}/${_realname/-/_}/${_realname/-/_}-${pkgver}.tar.gz"
)
sha256sums=(
  "3e1942b0908b9399b164f66712f8b222f38512950e61d16a5064d9795f0b0ac7"
)

prepare() {
  cd "${srcdir}"/${_realname/-/_}-${pkgver}
  # ignore version check for setuptools
  sed -i 's/"setuptools>=.*"/"setuptools"/' pyproject.toml

  rm -Rf "${srcdir}"/python-${_realname}-${MSYSTEM}
  cp -a "${srcdir}"/${_realname/-/_}-${pkgver} "${srcdir}"/python-${_realname}-${MSYSTEM}
}

pkgver() {
  cd python-${_realname}-${MSYSTEM}
  grep "__version__ = " src/${_realname/-/_}/__init__.py | sed 's/-dev./.dev/' | awk -F\" '{print $2}'
}

build() {
  cd python-${_realname}-${MSYSTEM}
  python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  cd python-${_realname}-${MSYSTEM}
  pip install ${_realname} -f dist --no-deps --no-index

  mkdir -p "${srcdir}/python-test-core"
  cp pyproject.toml "${srcdir}/python-test-core/"
  cp -a tests "${srcdir}/python-test-core/tests/"

  cd "${srcdir}/python-test-core"
  coverage run
  coverage combine
  coverage report
}

package() {
  cd python-${_realname}-${MSYSTEM}
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
