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

pkgbase=python
pkgname=('python' 'python-devel')
pkgver=3.11.8
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'
  '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
        001-3.1-enable-new-dtags.patch
        003-3.4-select-cygwin.patch
        005-3.7-ctypes-cygwin.patch
        007-3.8-dbm-cygwin.patch
        008-3.8-distutils-cygwin.patch
        009-3.8-export-PySignal_SetWakeupFd.patch
        012-3.8-pep3149-cygwin.patch
        014-3.8-xxsubinterpreters-cygwin.patch
        015-rpath.patch
        025-MINGW-compiler-customize-mingw-cygwin-compilers.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)
sha256sums=('9e06008c8901924395bc1da303eac567a729ae012baa182ab39269f650383bb3'
            'be96ddaca58a39ddaf1e3e5bb7900b34c0e6d00e9b64c4e0f8a3565a74a44e84'
            '82cfafc5b31ad4c9bb4c9786044c39c75762dbc2656abdfdc433c23fee69c02f'
            'f0bb75ca69c63894fc43e0f8218c9dbcc746935bf5ea095a724e6fb2f5dcc566'
            'dc044b0651a67f7c76e4119d65085e42277c0513b8e9653f0972d955c3dde2c5'
            '8c460bf9522e50e06aa2c21a921eb42543d7a4cac6c598b13be43940a96966bf'
            '70f854f00de71372e49f2ebbff7491e09e9e036e8e3f3646fe2984e30fb4a9da'
            'e3d3f4f8b653ddd7eec305d7af43928c070d409bcf7f1b59269d59545c8f1862'
            'e48c719be36cb266ac3ceae834739fb1faae6740524cd672fd367f9304894a39'
            '437332e1a7195dd0adcd505ecdeab5c3a7a793efdd7fea7bec55f97957bb8934'
            '3e6c115c9b8e044193c0524b94275dfb13c1adfba3d250131b065f632b6d5512'
            '9d0efb7e7e913e96b9f7953802311bbc2d046a7bfdcc3ffaf11fc509731b4d18'
            '5dfed4be015e93ab6f4e99a69fc5b3eece9c801ea504c8d0517c3cb6bb7dbe89'
            '85d9d042e0fa3493ec54b364e6b4fb6d2b12ba09641e01047bc84a09605ef76b'
            'b439ff4f0a1f13858f8fb596414b74ed2c14fc3103d90287bb8e461ee89288b9'
            '612bc5bcbe24c2f623713172d318c65a69ae601d72bcc5a170fe7c4eee458d18'
            'ee109d91a1c7ea84d278d9a8b0e1feb397e691b8868d79f77ea7bb6b1b3b1968')

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 \
    001-3.1-enable-new-dtags.patch \
    003-3.4-select-cygwin.patch \
    005-3.7-ctypes-cygwin.patch \
    007-3.8-dbm-cygwin.patch \
    008-3.8-distutils-cygwin.patch \
    009-3.8-export-PySignal_SetWakeupFd.patch \
    012-3.8-pep3149-cygwin.patch \
    014-3.8-xxsubinterpreters-cygwin.patch \
    015-rpath.patch \
    025-MINGW-compiler-customize-mingw-cygwin-compilers.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

  # 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}"

  export MSYSTEM=CYGWIN
  local CYGWIN_CHOST="${CHOST/-msys/-cygwin}"
  ./configure \
    --prefix=/usr \
    --build=${CYGWIN_CHOST} \
    --host=${CYGWIN_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 > >(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() {
  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

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

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