# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: @naveen521kk on Github <naveen521kk@gmail.com>

_realname=pip
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=25.3
pkgrel=1
pkgdesc="The PyPA recommended tool for installing Python packages. (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://pip.pypa.io/"
msys2_repository_url="https://github.com/pypa/pip"
msys2_references=(
  'purl: pkg:pypi/pip'
  "cpe: cpe:/a:pypa:pip"
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-setuptools"
         "${MINGW_PACKAGE_PREFIX}-python-distlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-flit-core"
             "${MINGW_PACKAGE_PREFIX}-python-installer")
options=('!strip')
source=(
  "https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
  "0001-use-system-distlib.patch"
  "0002-only-warn-for-externally-managed.patch"
)
sha256sums=('8d0538dbbd7babbd207f261ed969c65de439f6bc9e5dbd3b3b9a77f25d95f343'
            'c7f98690afa60e59324f1ce52e72bd7809cbfaba4e29a2d933f08c1a025f8246'
            'be196f12feee11fe1caccbdeff9a31ac077120de7b6e85679ea54b6dd2df8f5b')

prepare() {
  cd "${_realname}-${pkgver}"
  rm -r src/pip/_vendor/distlib
  patch -p1 -i "${srcdir}"/0001-use-system-distlib.patch

  # To avoid breaking too many systems while still warning the users that it's not recommended
  # to use pip outside of a venv, downgrade the error to a warning, for now.
  # https://github.com/msys2/MINGW-packages/pull/22368#issuecomment-2452503886
  patch -p1 -i "${srcdir}"/0002-only-warn-for-externally-managed.patch
}

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE.txt"
}
