### this file is sourced not run
PKGVER=3.6.5
PKGBUILD=1
PKGARCH=x86_64

# source: BLFS 8.2 / OpenSuse
# patches: https://build.opensuse.org/package/show/devel:languages:python:Factory/python
TARBALL=Python-$PKGVER.tar.xz
MD5SUM=9f49654a4d6f733ff3284ab9d227e9fd
SRC_URL=http://www.python.org/ftp/python/$PKGVER/$TARBALL
BUNDLE=

SLACKREQ='libffi expat openssl sqlite3'
SLACKDESC="python3: python3 $PKGVER (Python programming language)  
python3:  
python3: The Python3 package contains the Python development environment. It 
python3: is useful for object-oriented programming, writing scripts, 
python3: prototyping large programs or developing entire applications. 
python3: 
python3: This is version 3 of the Python language.
python3: 
python3: 
python3: 
python3:
"

### default pkg_download
### prepare
pkg_prepare() {
	pkg_prepare_default &&
	cd tmp/Python* &&
	
	# OpenSUSE patches
	### COMMON-PATCH-BEGIN ###
	# implement "--record-rpm" option for distutils installations
	#patch -Np1 -i ../pkg/Python-3.0b1-record-rpm.patch &&
	# support lib-vs-lib64 distinction
	patch -Np1 -i ../pkg/python-3.6.0-multilib-new.patch &&
	# support finding packages in /usr/local, install to /usr/local by default
	#patch -Np1 -i ../pkg/python-3.3.0b1-localpath.patch &&
	# replace DATE, TIME and COMPILER by fixed definitions to aid reproducible builds
	patch -Np1 -i ../pkg/python-3.3.0b1-fix_date_time_compiler.patch &&
	# fix wrong include path in curses-panel module
	patch -Np0 -i ../pkg/python-3.3.0b1-curses-panel.patch &&
	# POSIX_FADV_WILLNEED throws EINVAL. Use a different constant in test
	#patch -Np1 -i ../pkg/python-3.3.0b1-test-posix_fadvise.patch &&
	# Disable global and distutils sysconfig comparison test, we deviate from the default depending on optflags
	#patch -Np1 -i ../pkg/python-3.3.3-skip-distutils-test_sysconfig_module.patch &&
	# Raise timeout value for test_subprocess
	#patch -Np1 -i ../pkg/subprocess-raise-timeout.patch &&
	# PATCH-FIX-UPSTREAM bmwiedemann@suse.de -- https://github.com/python/cpython/pull/296
	patch -Np1 -i ../pkg/0001-allow-for-reproducible-builds-of-python-packages.patch &&
	# PATCH-FEATURE-OPENSUSE order files for compilation until the underlying cause of bsc#1049186 is resolved
	patch -Np1 -i ../pkg/distutils-reproducible-compile.patch &&
	# skip some tests only for PowerPC
	#patch -Np1 -i ../pkg/skip_random_failing_tests.patch &&
	# PATCH-FIX-UPSTREAM sorted tar https://github.com/python/cpython/pull/2263
	patch -Np1 -i ../pkg/python3-sorted_tar.patch &&
	### COMMON-PATCH-END ###
	
	# drop Autoconf version requirement
	sed -i 's/^AC_PREREQ/dnl AC_PREREQ/' configure.ac &&

	# fix shebangs - convert /usr/local/bin/python and /usr/bin/env/python to /usr/bin/python3
	for dir in Lib Tools; do
		# find *.py, filter to files that contain bad shebangs
		# break up "/""usr" like this to prevent replacing with %{_prefix}
		find $dir -name '*.py' -type f -print0 \
			| xargs -0 grep -lE '^#! *(/''usr/.*bin/(env +)?)?python' \
			| xargs sed -r -i -e '1s@^#![[:space:]]*(/''usr/(local/)?bin/(env +)?)?python([0-9]+(\.[0-9]+)?)?@#!%{_bindir}/python3@'
	done &&
	### COMMON-PREP-END ###
	
	
	true
}

### default pkg_package
### build
pkg_build() {
	cd /tmp/Python*
	autoreconf -fi &&

	./configure $CONFFLAGS \
		--enable-shared \
		--with-system-expat \
		--with-system-ffi   \
		--with-ensurepip=yes \
		--enable-ipv6        \
		--enable-unicode=ucs4
		
	# prevent make from trying to rebuild PYTHON_FOR_GEN stuff
	make -t Python/Python-ast.c \
			Include/Python-ast.h \
			Objects/typeslots.inc \
			Python/opcode_targets.h \
			Include/opcode.h

	make $MAKEFLAGS && make install &&
	chmod -v 755 /usr/$LIBDIR/libpython3.6m.so &&
	chmod -v 755 /usr/$LIBDIR/libpython3.so	&&
	rm -r /usr/$LIBDIR/python3.6/test && # delete rubbish

	# remove duplicate files with python2
	rm /usr/bin/2to3
	
	pkg_build_slackdesc &&

	# multilib (CBLFS)
	mv -v /usr/bin/python3.6{,-64} &&	
	mv -v /usr/bin/python3.6m{,-64} &&
	mv -v /usr/bin/python3.6m-config{,-64} &&

	ln -sfv python3.6-64 /usr/bin/python3-64 &&
	ln -sfv python3.6m-config-64 /usr/bin/python3.6-config-64 &&
	ln -sfv python3.6-config-64 /usr/bin/python3-config-64 &&

	ln -sfv multiarch_wrapper /usr/bin/python3 &&
	ln -sfv multiarch_wrapper /usr/bin/python3.6 &&
	ln -sfv multiarch_wrapper /usr/bin/python3.6-config &&
	ln -sfv multiarch_wrapper /usr/bin/python3.6m &&
	ln -sfv multiarch_wrapper /usr/bin/python3.6m-config &&
	ln -sfv multiarch_wrapper /usr/bin/python3-config &&
	
	mv -v /usr/include/python3.6m/pyconfig{,-64}.h &&
	install -m644 /tmp/pkg/pyconfig.h /usr/include/python3.6m &&

	true
}


