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

pkgbase=python3
pkgname=python
pkgver=3.8.3
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/"
depends=('libbz2' 'libexpat' 'libffi' 'liblzma' 'ncurses' 'libopenssl' 'libreadline' 'mpdecimal' 'libsqlite' 'zlib')
makedepends=('libbz2-devel' 'libexpat-devel'  'mpdecimal-devel' 'libsqlite-devel' 'libffi-devel' 'ncurses-devel' 'libreadline-devel' 'liblzma-devel' 'openssl-devel' 'zlib-devel')
#optdepends=('tk: for tkinter' 'sqlite')
provides=('python3')
replaces=('python3')
options=('!makeflags')
source=(https://www.python.org/ftp/python/${pkgver%rc*}/Python-${pkgver}.tar.xz
        001-3.1-enable-new-dtags.patch
        002-3.2-getpath-exe-extension.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
        016-no-static-lib.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)
sha256sums=('dfab5ec723c218082fe3d5d7ae17ecbdebffa9a1aea4d64aa3a2ecdd2e795864'
            'be96ddaca58a39ddaf1e3e5bb7900b34c0e6d00e9b64c4e0f8a3565a74a44e84'
            '4044827af3cf0e91a8ddd44cd588f17ea4e17619512edd299b5688ec6e112ad4'
            '82cfafc5b31ad4c9bb4c9786044c39c75762dbc2656abdfdc433c23fee69c02f'
            'f0bb75ca69c63894fc43e0f8218c9dbcc746935bf5ea095a724e6fb2f5dcc566'
            '098a756dfdb69b43bece4efe9d53a4f6ffc05a9a19a3d48854de787e69fc453c'
            'dc044b0651a67f7c76e4119d65085e42277c0513b8e9653f0972d955c3dde2c5'
            '8c460bf9522e50e06aa2c21a921eb42543d7a4cac6c598b13be43940a96966bf'
            '70f854f00de71372e49f2ebbff7491e09e9e036e8e3f3646fe2984e30fb4a9da'
            '62588723eb4051dccd0c8ab4183455e7ab99a3dca6c3c07e7403e1919b934ef0'
            'd6b15b6624183d64fc77e5a4e5d100f05266ac8a83fc4e21262c7969e2abf4e5'
            '89ebefa1acbdbde73d084a93241674d596e61e608a55301784e29903dba4f289'
            '4c6d9d786da1673208b0ef2bc3340e1acf08ce861b49193eb1592c53c3a4bd26'
            'e48c719be36cb266ac3ceae834739fb1faae6740524cd672fd367f9304894a39'
            '437332e1a7195dd0adcd505ecdeab5c3a7a793efdd7fea7bec55f97957bb8934'
            '43aac098b3a6e2b7806653e51f923b25b7afbf93e0e01c94846c5f749a81f5de'
            '5b1083e9b50e149d623d863dee38ac1fb8d142f1bb78c8a01dcb09bfd97f4118'
            'fc6164a90f0ca2a9341aaf4448b4334c3393459ca5cbad6d7149f8bcf70da5fe'
            'b4042475c5c75e0b4c7c08ccad2891eb8098c066c5ba524a988a0519102e8e5d'
            'e833cb6ab8a3d35296f52ad327df7eb4cc4eab94413085bded2943a290cba16d'
            '387a2b7931fb4958e2526991760d85677f44fa13cff0aeb0f41a267f1f7fd214')

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

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  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 \
    002-3.2-getpath-exe-extension.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 \
    016-no-static-lib.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
  
  # Incomplete patch from Ray Donnelly
  # patch -p1 -i ${srcdir}/3.3.2-allow-windows-paths-for-executable.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"
}

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

package() {
  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

  # Fix FS#22552
  # cp -f "${pkgdir}"/usr/lib/python${_pybasever}/config-${_pybasever}-*/libpython${_pybasever}.dll.a "${pkgdir}"/usr/lib/libpython${_pybasever}.dll.a

  # Clean-up reference to build directory
  # sed -i "s|${srcdir}/Python-${pkgver}:||" "${pkgdir}/usr/lib/python${_pybasever}/config-${_pybasever}-*/Makefile"

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