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

# 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='libffi32 expat32 openssl32 sqlite3_32'
SLACKDESC="python3_32: python3_32 $PKGVER (Python programming language)  
python3_32:  
python3_32: The Python 2 package contains the Python development environment. It 
python3_32: is useful for object-oriented programming, writing scripts, 
python3_32: prototyping large programs or developing entire applications. 
python3_32: 
python3_32: This is version 3 of the Python language.
python3_32: 
python3_32: This contains 32-bit version of Python3.
python3_32: 
python3_32:
"

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

### package - only collect the libraries
pkg_package() {
	# kill other duplicate stuff
	rm -r ./usr/$LIBDIR/python3.6/site-packages &&
	true
}

### 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
	
	pkg_build_slackdesc &&

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

	ln -sfv python3.6-32 /usr/bin/python3-32 &&
	ln -sfv python3.6m-config-32 /usr/bin/python3.6-config-32 &&
	ln -sfv python3.6-config-32 /usr/bin/python3-config-32 &&
	
	# kill duplicate binaries and include
	rm -rv /usr/bin/{2to3*,easy_install*,idle3*,pip3*,pydoc3*,pyvenv*} &&
	mv -v /usr/include/python3.6m/pyconfig{,-32}.h &&
	mv -v /usr/include/python3.6m/pyconfig-32.h /tmp &&
	rm -rf /usr/include/python3.6m && mkdir -p /usr/include/python3.6m &&
	mv -v /tmp/pyconfig* /usr/include/python3.6m &&

	true
}


