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

pkgbase=python3
pkgname=python
pkgver=3.6.6
pkgrel=1
_pybasever=3.6
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.4-dbm-cygwin.patch
        002-3.1-enable-new-dtags.patch
        003-3.4-tkinter-cygwin.patch
        004-3.4-ctypes-cygwin.patch
        006-3.1-ncurses-abi6.patch
        007-3.2-export-PySignal_SetWakeupFd.patch
        008-3.4-distutils-soname.patch
        009-3.2-distutils-shlibext.patch
        010-3.6-pep3149-cygwin.patch
        011-3.6-thread-cygwin64.patch
        012-3.2-getpath-exe-extension.patch
        013-3.4-select-cygwin.patch
        014-3.4-signal-cygwin.patch
        015-3.4-struct-cygwin.patch
        016-3.6-ftm.patch
        025-MINGW-compiler-customize-mingw-cygwin-compilers.patch
        900-msysize.patch
        910-libffi-msys2.patch
        920-allow-win-drives-in-os-path-isabs.patch
        dont-make-libpython-readonly.patch
        00155-avoid-ctypes-thunks.patch
        00157-uid-gid-overflows.patch
        00170-gc-assertions.patch
        00178-dont-duplicate-flags-in-sysconfig.patch
        00188-fix-lib2to3-tests-when-hashlib-doesnt-compile-properly.patch
        00189-add-rewheel-module.patch
        00206-remove-hf-from-arm-triplet.patch
        00243-fix-mips64-triplet.patch
        00252-add-executable-option.patch
        00260-require-setuptools-dependencies.patch
        00264-skip-test-failing-on-aarch64.patch
        python3-powerppc-arch.patch
        016-3.6-mpdec-cygwin.patch)
sha256sums=('d79bc15d456e73a3173a2938f18a17e5149c850ebdedf84a78067f501ee6e16f'
            'de52e4722a6902e09dd6f343fdac0e7d45339f89386c8096b9c582c31aeb82e7'
            'b5a787f02811f46800f98bf242448770492643a4431771bb4283e6ae97016263'
            '4d7601b62e73c04553d22d480a873983a28b11df1f06be6898014453ce04afc3'
            'edf33a790c7c0903b8edbc8e68baf91cf2d34c13e607503d1cc7f270186ea76c'
            '8a138293fa5ad3a7522e00194994fecc8967c6a71822127391e4d756b2280e74'
            '1ab20d38926aa85e638317620c29c484fcbbc228cd7cdf9543c29b750556d23f'
            '7a7425b5fa28b7692bafff226981650748d7f40a42a711378b9a9c8b23a98060'
            '6ead37259110c28b16e35e05c93c82b46aa0fbb5152ff61bdaf1eea5f1ad01da'
            'f5b67408d39d3fda539ea90b12c48a05bd950b38460ec3e6a688e3377b071ed3'
            'dba84d6a24a54a66d14e969fbbf366666a7376b667802bdf98f06d8ee1c91b8c'
            'c09d3612d748852def0bcd1cb36dda7bd24366ed6b386743c800c7cb21eb0298'
            'e668b749e5f0bc584262add2cd7fffb2f6f32393a14918029d791012eda930f3'
            '30564663c1f2cd7d0d10a2b07eceb139534f17bc3a38dc8daafad0d5111e8bbc'
            '505c736e35772835b06597de552217e4838052b759d1350b65b4cb079b6403f3'
            '7c4774d40b18d5a4ef4f3e05d53cc2d4d0ca3d0f970b3af7f7164ac8be0a4c56'
            '5b1083e9b50e149d623d863dee38ac1fb8d142f1bb78c8a01dcb09bfd97f4118'
            'c196e50e029016947db4470a1339af6899722754fe8f026627fd671230852b51'
            '7e587d145db24fbee1c7c7b96a4d7f247d132315384d551005ffb39b51f41906'
            '387a2b7931fb4958e2526991760d85677f44fa13cff0aeb0f41a267f1f7fd214'
            'f8b15d7079bfa1707e5bea78f600a0fca2077c25428c2bac5793b19b408f276e'
            '7a3f8f43b9c9eecb65d80b60c875344950a8b55082401830599d091548a3a985'
            '13519ee9358cc5378b3fab60501b9db4d56e86d0b69da8b9841989d437492c7e'
            'e70369c7096b8204b0af968679d493bf272996df637e53ca6fad4a236f1a2b02'
            '4e7ce358f0650545ee9ae112b26cee4dce8f763eac5612f556e975ad38056fb8'
            'd04b7bec35418c699358c8140a7213efe1a133772972715800b7a6101e0d699a'
            '8c7ba55643635cc9dcfb57dec8cf4f50261bb2d5c169e82f991573e42400dd36'
            'd61273d74f734bcdef933531cbd3cc77cf79b5826837cbd40cd48e5b1bc76072'
            'f81a0a0907e65a00bff1a1ede4827a33984a6f2aee3aed2ce6a2423decdfd26d'
            '735257fc216be2300b35dab8a97ff76199cc98d69b0bce36f099d476fb0f30e5'
            '66d016758216565a4e69d81b44eb978518f572042a4afe1ee0a996158e59a983'
            '843c26c4325f1d42f30f6e1fac5e6ff06122903bd37aa67683838ae49609e276'
            '0de3e6e67d86f7e3593c504385399537b42920aca94329424cc8b1d65ae2ac4f'
            'e3ef181333d5c9d20297849a46a68271a2190b7fc611c40c68c1ae240fa7ec36')
prepare() {
  cd "${srcdir}/Python-${pkgver}"

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

  patch -p1 -i ${srcdir}/001-3.4-dbm-cygwin.patch
  patch -p1 -i ${srcdir}/002-3.1-enable-new-dtags.patch
  patch -p1 -i ${srcdir}/003-3.4-tkinter-cygwin.patch
  patch -p1 -i ${srcdir}/004-3.4-ctypes-cygwin.patch
  patch -p1 -i ${srcdir}/006-3.1-ncurses-abi6.patch
  patch -p1 -i ${srcdir}/007-3.2-export-PySignal_SetWakeupFd.patch
  patch -p1 -i ${srcdir}/008-3.4-distutils-soname.patch
  patch -p1 -i ${srcdir}/009-3.2-distutils-shlibext.patch
  patch -p1 -i ${srcdir}/010-3.6-pep3149-cygwin.patch
  patch -p1 -i ${srcdir}/011-3.6-thread-cygwin64.patch
  patch -p1 -i ${srcdir}/012-3.2-getpath-exe-extension.patch
  patch -p1 -i ${srcdir}/013-3.4-select-cygwin.patch
  patch -p1 -i ${srcdir}/014-3.4-signal-cygwin.patch
  patch -p1 -i ${srcdir}/015-3.4-struct-cygwin.patch
  patch -p2 -i ${srcdir}/016-3.6-ftm.patch
  patch -p1 -i ${srcdir}/025-MINGW-compiler-customize-mingw-cygwin-compilers.patch
  patch -p1 -i ${srcdir}/900-msysize.patch
  patch -p1 -i ${srcdir}/910-libffi-msys2.patch
  patch -p1 -i ${srcdir}/920-allow-win-drives-in-os-path-isabs.patch

  #archlinux
  patch -p1 -i ${srcdir}/dont-make-libpython-readonly.patch
  #fedora
  patch -p1 -i ${srcdir}/00155-avoid-ctypes-thunks.patch
  patch -p1 -i ${srcdir}/00157-uid-gid-overflows.patch
  patch -p1 -i ${srcdir}/00170-gc-assertions.patch
  patch -p1 -i ${srcdir}/00178-dont-duplicate-flags-in-sysconfig.patch
  patch -p1 -i ${srcdir}/00188-fix-lib2to3-tests-when-hashlib-doesnt-compile-properly.patch
  patch -p1 -i ${srcdir}/00189-add-rewheel-module.patch
  patch -p1 -i ${srcdir}/00206-remove-hf-from-arm-triplet.patch
  patch -p1 -i ${srcdir}/00243-fix-mips64-triplet.patch
  patch -p1 -i ${srcdir}/00252-add-executable-option.patch
  patch -p1 -i ${srcdir}/00260-require-setuptools-dependencies.patch
  patch -p1 -i ${srcdir}/00264-skip-test-failing-on-aarch64.patch
  patch -p1 -i ${srcdir}/python3-powerppc-arch.patch
  #PiotrVV
  patch -p1 -i ${srcdir}/016-3.6-mpdec-cygwin.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/zlib
  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-threads \
	--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 maninstall

  # 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 pyvenv-${_pybasever}  "${pkgdir}"/usr/bin/pyvenv3
  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}m/libpython${_pybasever}m.dll.a "${pkgdir}"/usr/lib/libpython${_pybasever}m.dll.a

  # Clean-up reference to build directory
  sed -i "s|$srcdir/Python-${pkgver}:||" "${pkgdir}/usr/lib/python${_pybasever}/config-${_pybasever}m/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
}
