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

pkgbase=python
pkgname=('python' 'python-devel')
pkgver=3.9.6
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/"
makedepends=(
  'libbz2-devel'
  'libcrypt-devel'
  'libexpat-devel'
  'mpdecimal-devel'
  'libsqlite-devel'
  'libffi-devel'
  'ncurses-devel'
  'libreadline-devel'
  'liblzma-devel'
  'openssl-devel'
  'zlib-devel'
  'autoconf-archive'
)
source=(https://www.python.org/ftp/python/${pkgver%rc*}/Python-${pkgver}.tar.xz
        001-3.1-enable-new-dtags.patch
        003-3.4-select-cygwin.patch
        005-3.7-ctypes-cygwin.patch
        006-3.7-ftm.patch
        007-3.8-dbm-cygwin.patch
        008-3.8-distutils-cygwin.patch
        009-3.8-export-PySignal_SetWakeupFd.patch
        010-3.8-nis-cygwin.patch
        011-3.8-parser-cygwin.patch
        012-3.8-pep3149-cygwin.patch
        013-3.8-tkinter-cygwin.patch
        014-3.8-xxsubinterpreters-cygwin.patch
        015-rpath.patch
        025-MINGW-compiler-customize-mingw-cygwin-compilers.patch
        026-3.7-mpdec-msys.patch
        027-install-import-library.patch
        900-msysize.patch
        920-allow-win-drives-in-os-path-isabs.patch
        930-fix-missing-tcp-include.patch
        940-rebase-python-dll.patch
        950-rebase-dlls.patch
        960-fix-parallel-make.patch)
sha256sums=('397920af33efc5b97f2e0b57e91923512ef89fc5b3c1d21dbfc8c4828ce0108a'
            'be96ddaca58a39ddaf1e3e5bb7900b34c0e6d00e9b64c4e0f8a3565a74a44e84'
            '82cfafc5b31ad4c9bb4c9786044c39c75762dbc2656abdfdc433c23fee69c02f'
            'f0bb75ca69c63894fc43e0f8218c9dbcc746935bf5ea095a724e6fb2f5dcc566'
            '098a756dfdb69b43bece4efe9d53a4f6ffc05a9a19a3d48854de787e69fc453c'
            'dc044b0651a67f7c76e4119d65085e42277c0513b8e9653f0972d955c3dde2c5'
            '8c460bf9522e50e06aa2c21a921eb42543d7a4cac6c598b13be43940a96966bf'
            '70f854f00de71372e49f2ebbff7491e09e9e036e8e3f3646fe2984e30fb4a9da'
            '62588723eb4051dccd0c8ab4183455e7ab99a3dca6c3c07e7403e1919b934ef0'
            'd6b15b6624183d64fc77e5a4e5d100f05266ac8a83fc4e21262c7969e2abf4e5'
            '9b84b5e027ff39ed3f827e805079f260db319a78571012815da30765ee2ceabc'
            '4c6d9d786da1673208b0ef2bc3340e1acf08ce861b49193eb1592c53c3a4bd26'
            'e48c719be36cb266ac3ceae834739fb1faae6740524cd672fd367f9304894a39'
            '437332e1a7195dd0adcd505ecdeab5c3a7a793efdd7fea7bec55f97957bb8934'
            '5b1083e9b50e149d623d863dee38ac1fb8d142f1bb78c8a01dcb09bfd97f4118'
            'fc6164a90f0ca2a9341aaf4448b4334c3393459ca5cbad6d7149f8bcf70da5fe'
            'b4042475c5c75e0b4c7c08ccad2891eb8098c066c5ba524a988a0519102e8e5d'
            'fead37f619cb76e0fde66acdc13abee74e89c8f4797e0b21e0cca014ee2ecabe'
            '85d9d042e0fa3493ec54b364e6b4fb6d2b12ba09641e01047bc84a09605ef76b'
            '17e4ac1b5f0fa8a6c410fb80d1ad99ec9185efef51b8450a31932b553c354ed1'
            'b439ff4f0a1f13858f8fb596414b74ed2c14fc3103d90287bb8e461ee89288b9'
            '612bc5bcbe24c2f623713172d318c65a69ae601d72bcc5a170fe7c4eee458d18'
            'c59025ca9d1219bbb42d620ca8d19398610737c5754f1873db07d55c47b2dad1')

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

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

  # FS#23997
  sed -i -e "s|^#.* /usr/local/bin/python|#!/usr/bin/python|" Lib/cgi.py

  apply_patch_with_msg \
    001-3.1-enable-new-dtags.patch \
    003-3.4-select-cygwin.patch \
    005-3.7-ctypes-cygwin.patch \
    006-3.7-ftm.patch \
    007-3.8-dbm-cygwin.patch \
    008-3.8-distutils-cygwin.patch \
    009-3.8-export-PySignal_SetWakeupFd.patch \
    010-3.8-nis-cygwin.patch \
    011-3.8-parser-cygwin.patch \
    012-3.8-pep3149-cygwin.patch \
    013-3.8-tkinter-cygwin.patch \
    014-3.8-xxsubinterpreters-cygwin.patch \
    015-rpath.patch \
    025-MINGW-compiler-customize-mingw-cygwin-compilers.patch \
    026-3.7-mpdec-msys.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 \
    960-fix-parallel-make.patch
  
  # https://bugs.python.org/issue41374
  apply_patch_with_msg \
    930-fix-missing-tcp-include.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/_ctypes/{darwin,libffi}*
  rm -r Modules/_decimal/libmpdec
  autoreconf -fiv
}

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

  CPPFLAGS+=" -I/usr/include/ncursesw"
  LDFLAGS+=" -L."

  CXX=/usr/bin/g++ \
  ./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 \
    ac_cv_func_bind_textdomain_codeset=yes

  LC_CTYPE=en_US.UTF-8 make EXTRA_CFLAGS="$CFLAGS" > >(tee make-python.log)
  sed -ne '/^\s*Failed to build these modules/,$ {p; /^\s*$/q42}' make-python.log
}

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

package_python() {
  #optdepends=('tk: for tkinter' 'sqlite')
  depends=('libbz2' 'libexpat' 'libffi' 'liblzma' 'ncurses' 'libopenssl' 'libreadline' 'mpdecimal' 'libsqlite' 'zlib')
  provides=('python3')
  replaces=('python3')

  cd "${srcdir}/Python-${pkgver}"
  make DESTDIR="${pkgdir}" EXTRA_CFLAGS="$CFLAGS" 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

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

  # fix permissons
  find ${pkgdir}/usr -type f \( -name *.dll -o -name *.exe \) | xargs chmod 0755
}

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