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

pkgbase=python
pkgname=('python' 'python-devel')
pkgver=3.12.12
pkgrel=1
_pybasever=${pkgver%.*}
pkgdesc="Next generation of the python high-level scripting language"
arch=('i686' 'x86_64')
license=('custom')
url="https://www.python.org/"
msys2_references=(
  "cpe: cpe:/a:python:python"
  "cpe: cpe:/a:python_software_foundation:python"
)
makedepends=(
  'libbz2-devel'
  'libxcrypt-devel'
  'libexpat-devel'
  'mpdecimal-devel'
  'libsqlite-devel'
  'libffi-devel'
  'ncurses-devel'
  'libreadline-devel'
  'liblzma-devel'
  'openssl-devel'
  'zlib-devel'
  'autotools'
  'autoconf-archive'
  'gcc'
)
source=(https://www.python.org/ftp/python/${pkgver%rc*}/Python-${pkgver}.tar.xz
        003-3.4-select-cygwin.patch
        005-3.7-ctypes-cygwin.patch
        009-3.8-export-PySignal_SetWakeupFd.patch
        012-3.8-pep3149-cygwin.patch
        027-install-import-library.patch
        900-msysize.patch
        920-allow-win-drives-in-os-path-isabs.patch
        940-rebase-python-dll.patch
        950-rebase-dlls.patch
        970-ossaudiodev.patch
        980-fix-module-lib-dep.patch
        990-cygwin-default-to-spawn-for-multiprocessing-and-disa.patch
        EXTERNALLY-MANAGED)
sha256sums=('fb85a13414b028c49ba18bbd523c2d055a30b56b18b92ce454ea2c51edc656c4'
            '82cfafc5b31ad4c9bb4c9786044c39c75762dbc2656abdfdc433c23fee69c02f'
            'f0bb75ca69c63894fc43e0f8218c9dbcc746935bf5ea095a724e6fb2f5dcc566'
            '70f854f00de71372e49f2ebbff7491e09e9e036e8e3f3646fe2984e30fb4a9da'
            '24cc76e155b5e017d2e46e784eb3407de60f2f1fd26e691ef222bc2088bbb9d5'
            '9d0efb7e7e913e96b9f7953802311bbc2d046a7bfdcc3ffaf11fc509731b4d18'
            'a0acb9da960df3d509dfb530f548adeffbcab616e7f4f2fc92fc98659649d43b'
            '85d9d042e0fa3493ec54b364e6b4fb6d2b12ba09641e01047bc84a09605ef76b'
            'b439ff4f0a1f13858f8fb596414b74ed2c14fc3103d90287bb8e461ee89288b9'
            'bcdb4e7922e30f7dfbd3993ffe6db2dfd0df29326bebd12203dce376ea3451d9'
            'ee109d91a1c7ea84d278d9a8b0e1feb397e691b8868d79f77ea7bb6b1b3b1968'
            'e2861218f05741bfe99b05bb41cf88e14f57747aedec251626691b05482a50bd'
            'd04ca4778f150b880e23b9bc1fe5c5385e41228399093320c80ad4d5e29c6aab'
            '2c8cdad18085b8736e985653c0f18523958f29b72125e15124806a0f3d1a20ee')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

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

  apply_patch_with_msg \
    003-3.4-select-cygwin.patch \
    005-3.7-ctypes-cygwin.patch \
    009-3.8-export-PySignal_SetWakeupFd.patch \
    012-3.8-pep3149-cygwin.patch \
    027-install-import-library.patch \
    900-msysize.patch \
    920-allow-win-drives-in-os-path-isabs.patch \
    940-rebase-python-dll.patch \
    950-rebase-dlls.patch \
    970-ossaudiodev.patch \
    980-fix-module-lib-dep.patch

  # https://github.com/msys2/MSYS2-packages/pull/4743#issuecomment-2213043559
  apply_patch_with_msg \
    990-cygwin-default-to-spawn-for-multiprocessing-and-disa.patch

  # Ensure that we are using the system copy of various libraries (expat, zlib and libffi),
  # rather than copies shipped in the tarball
  rm -r Modules/expat
  rm -r Modules/_decimal/libmpdec
  autoreconf -fiv
}

build() {
  cd "${srcdir}/Python-${pkgver}"

  export MSYSTEM=CYGWIN
  ./configure \
    --prefix=/usr \
    --build=${CHOST} \
    --host=${CHOST} \
    --enable-shared \
    --enable-ipv6 \
    --with-dbmliborder=gdbm \
    --with-libc= \
    --with-libm= \
    --with-system-expat \
    --with-system-ffi \
    --with-system-libmpdec \
    --enable-loadable-sqlite-extensions \
    --without-ensurepip

  make
}

check() {
  cd "${srcdir}/Python-${pkgver}"
  "${srcdir}/Python-${pkgver}/python.exe" -m test.regrtest -x test_posixpath test_logging
}

package_python() {
  depends=('libbz2' 'libexpat' 'libffi' 'liblzma' 'ncurses' 'libopenssl' 'libreadline' 'mpdecimal' 'libsqlite' 'zlib' 'libxcrypt')
  provides=('python3')
  replaces=('python3')

  cd "${srcdir}/Python-${pkgver}"
  make DESTDIR="${pkgdir}" install

  # Why are these not done by default...
  ln -sf python3               "${pkgdir}"/usr/bin/python.exe
  ln -sf python3-config        "${pkgdir}"/usr/bin/python-config
  ln -sf idle3                 "${pkgdir}"/usr/bin/idle
  ln -sf pydoc3                "${pkgdir}"/usr/bin/pydoc
  ln -sf python${_pybasever}.1 "${pkgdir}"/usr/share/man/man1/python3.1
  ln -sf python${_pybasever}.1 "${pkgdir}"/usr/share/man/man1/python.1

  # PEP668
  install -Dm644 "$srcdir"/EXTERNALLY-MANAGED -t "${pkgdir}/usr/lib/python${_pybasever}/"

  # License
  install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
}

package_python-devel() {
  pkgdesc="Python headers and dev dependencies"
  depends=("python=${pkgver}")
}
