# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=setuptools
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
           "${MINGW_PACKAGE_PREFIX}-python2-setuptools<42.0.2")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=75.1.0
pkgrel=1
pkgdesc="Easily download, build, install, upgrade, and uninstall Python packages (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
msys2_references=(
  'pypi: setuptools'
  "cpe: cpe:/a:python:setuptools"
)
license=('spdx:MIT')
url="https://github.com/pypa/setuptools"
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
checkdepends=(
  "${MINGW_PACKAGE_PREFIX}-python-pytest"
  "${MINGW_PACKAGE_PREFIX}-python-pytest-checkdocs"
  "${MINGW_PACKAGE_PREFIX}-python-pytest-flake8"
  "${MINGW_PACKAGE_PREFIX}-python-pytest-cov"
  "${MINGW_PACKAGE_PREFIX}-python-pytest-enabler"
  "${MINGW_PACKAGE_PREFIX}-python-mock"
  "${MINGW_PACKAGE_PREFIX}-python-virtualenv"
  "${MINGW_PACKAGE_PREFIX}-python-wheel"
  "${MINGW_PACKAGE_PREFIX}-python-pip"
  "${MINGW_PACKAGE_PREFIX}-python-jaraco.envs"
  "${MINGW_PACKAGE_PREFIX}-python-pytest-xdist"
  "${MINGW_PACKAGE_PREFIX}-python-sphinx"
  "${MINGW_PACKAGE_PREFIX}-python-jaraco.path" 
  "${MINGW_PACKAGE_PREFIX}-python-pytest-black"
  "${MINGW_PACKAGE_PREFIX}-python-pytest-fixture-config"
  "${MINGW_PACKAGE_PREFIX}-python-pytest-virtualenv"
  'git'
)
options=(!strip)
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        '0001-mingw-python-fix.patch'
        '0002-Allow-usr-bin-env-in-script.patch'
        '0003-MinGW-w64-Look-in-same-dir-as-script-for-exe.patch'
        '0005-execv-warning.patch'
        '0006-fix-tests-path.patch'
        '0007-windows-arm64.patch'
        '0009-launcher-revert-symlink-resolve.patch'
        '0010-support-py_limited_api.patch')
sha256sums=('d59a21b17a275fb872a9c3dae73963160ae079f1049ed956880cd7c09b120538'
            '7b196884ac0151801f9638dabbf9a789fa9070a3b07a4c9e625c5523e8d47387'
            'fa54581e3dddb9f4edd332dedbc101f48939a9ca5878e13cf9cf9b3408bc8c22'
            '39503256652c7c23ce07e26539e8123d269eb5c09f5d9b07e5784b2d7fb8c96f'
            'a356b0663f67a296624d1b178b42437b380b48a4bbe05a560d3bf29e93a4c623'
            'dbdd96a7ead797b2db9f02dfe19ce853d3775337ccf8fd836377fe3c2a9d1c24'
            'cb897bc7292a733167fa48b5ca18323d76d3374683a7f5e9986c9df63f5332cc'
            '391d682a8b8832d7e4be3ac0c3327cfe3c8e520f16e25b43d4441df8036bf147'
            'ccb276f9d2d26587d29ecfd4d47a3d4e96fb58738cfaf3a600a8b942c79fc439')

prepare() {
  cd "${srcdir}/setuptools-${pkgver}"

  patch -p1 -i ${srcdir}/0001-mingw-python-fix.patch
  patch -p1 -i ${srcdir}/0002-Allow-usr-bin-env-in-script.patch
  patch -p1 -i ${srcdir}/0003-MinGW-w64-Look-in-same-dir-as-script-for-exe.patch
  patch -p1 -i ${srcdir}/0005-execv-warning.patch
  patch -p1 -i ${srcdir}/0006-fix-tests-path.patch
  patch -p1 -i ${srcdir}/0007-windows-arm64.patch

  # Partially reverts https://github.com/pypa/setuptools/commit/4bf6f127f4056c42dbcbc723ecf40dbb91569f7f
  # GetFinalPathNameByHandle converts the script path to a length-extended path (starting with \\?\)
  # and our Python can't deal with that for some reason
  # (from what I see the launcher code itself deals with it fine)
  patch -R -p1 -i ${srcdir}/0009-launcher-revert-symlink-resolve.patch

  # https://github.com/msys2-contrib/cpython-mingw/issues/149
  patch -p1 -i ${srcdir}/0010-support-py_limited_api.patch

  cd "${srcdir}"
  rm -rf python-build-${MSYSTEM} | true
  cp -r "${_realname}-$pkgver" "python-build-${MSYSTEM}"
}

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

  rm -f setuptools/{gui,cli}{-32,-64,-arm64,}.exe
  local _gui_args="-DGUI=1 -mwindows -O2"
  local _cli_args="-DGUI=0 -O2"
  if check_option "strip" "y"; then
    _gui_args+=" -s"
    _cli_args+=" -s"
  fi
  case "${MINGW_CHOST}" in
    i686-w64-mingw32)
      cc $_cli_args -o setuptools/cli-32.exe launcher.c
      cc $_gui_args -o setuptools/gui-32.exe launcher.c
      cp setuptools/cli-32.exe setuptools/cli.exe
      cp setuptools/gui-32.exe setuptools/gui.exe
      ;;
    x86_64-w64-mingw32)
      cc $_cli_args -o setuptools/cli-64.exe launcher.c
      cc $_gui_args -o setuptools/gui-64.exe launcher.c
      ;;
    aarch64-w64-mingw32)
      cc $_cli_args -o setuptools/cli-arm64.exe launcher.c
      cc $_gui_args -o setuptools/gui-arm64.exe launcher.c
      ;;
    *)
      echo "Unsupported CHOST ${MINGW_CHOST}" && false
      ;;
  esac

  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

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

  ${MINGW_PREFIX}/bin/python -m pytest || true
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  # https://github.com/pypa/distutils/pull/282
  find "${pkgdir}${MINGW_PREFIX}" -name 'wininst-*.exe' -exec rm {} \;

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