# 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}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=21.1.3
pkgrel=1
pkgdesc="The PyPA recommended tool for installing Python packages. (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('MIT')
url="https://pip.pypa.io/"
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-setuptools"
         "${MINGW_PACKAGE_PREFIX}-python-distlib")
# checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest"
#               "${MINGW_PACKAGE_PREFIX}-python-scripttest"
#               "${MINGW_PACKAGE_PREFIX}-python-csv23"
#               "${MINGW_PACKAGE_PREFIX}-python-cryptography"
#               "${MINGW_PACKAGE_PREFIX}-python-werkzeug"
#               "${MINGW_PACKAGE_PREFIX}-python-virtualenv")
source=(
  "${_realname}-${pkgver}.tar.gz::https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
  "0001-use-system-distlib.patch"
)
sha256sums=('b5b1eb91b36894bd01b8e5a56a422c2f3838573da0b0a1c63a096bb454e3b23f'
            'c7f98690afa60e59324f1ce52e72bd7809cbfaba4e29a2d933f08c1a025f8246')

prepare() {
  rm -rf python-build-${CARCH}| true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
  rm -rf "python-build-${CARCH}"/src/pip/_vendor/distlib
  cd "${srcdir}/python-build-${CARCH}"
  patch -p1 -i ${srcdir}/0001-use-system-distlib.patch
}

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}"
#   ${MINGW_PREFIX}/bin/pytest tests
# }
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 -v

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -i 's/\#\!.*//' $_f
  done

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