# Maintainer: Natanael Copa <ncopa@alpinelinux.org>

# This package must be synchronized with python2. It is splitted to avoid
# a cyclic dependency: python -> tk -> libx11 -> libxcb -> xcb-proto -> python

pkgname=python2-tkinter
pkgver=2.7.14
_verbase=${pkgver%.*}
pkgrel=0
pkgdesc="A graphical user interface for the Python"
url="https://wiki.python.org/moin/TkInter"
arch="all"
license="custom"
subpackages="$pkgname-tests python2-idle:_idle:noarch"
depends=""
makedepends="expat-dev libressl-dev zlib-dev ncurses-dev bzip2-dev
	gdbm-dev sqlite-dev libffi-dev readline-dev linux-headers paxmark
	tk tk-dev python2"
replaces="python2"
replaces_priority=1
source="http://www.python.org/ftp/python/$pkgver/Python-$pkgver.tar.xz"
builddir="$srcdir/Python-$pkgver"

prepare() {
	local _pyapkbuild="$startdir"/../main/python2/APKBUILD
	if [ -e "$_pyapkbuild" ]; then
		_pver=$(. "$_pyapkbuild" ; echo $pkgver)
		if [ "$_pver" != "$pkgver" ]; then
			error "python version mismatch ($_pver). Set pkgver=$_pver"
			return 1
		fi
	fi
	default_prepare
	cd "$builddir"

	# Make sure we use system libs
	rm -rf Modules/expat Modules/_ctypes/libffi* Modules/zlib
}

build() {
	cd "$builddir"
	export OPT="$CFLAGS"
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--enable-shared \
		--with-threads \
		--enable-ipv6 \
		--with-system-ffi \
		--with-system-expat \
		--with-system-zlib \
		--enable-unicode=ucs4
	make
}

package() {
	cd "$builddir"
	# we only care about idle, and tkinter
	make DESTDIR="$builddir"/tmpinstall sharedinstall libinstall \
		|| return 1

	mkdir -p "$pkgdir"/usr/lib/python${_verbase}/lib-dynload \
		"$pkgdir"/usr/bin

	for lib in idlelib lib-tk; do
		mv "$builddir"/tmpinstall/usr/lib/python${_verbase}/$lib \
			"$pkgdir"/usr/lib/python${_verbase}/
	done

	mv "$builddir"/tmpinstall/usr/lib/python${_verbase}/lib-dynload/_tkinter.so \
		"$pkgdir"/usr/lib/python${_verbase}/lib-dynload/

	mv "$builddir"/tmpinstall/usr/bin/idle \
		"$pkgdir"/usr/bin/idle${_verbase}
}

_mv_files() {
	local i
	for i in "$@"; do
		mkdir -p "$subpkgdir"/${i%/*}
		mv "$pkgdir"/$i "$subpkgdir"/$i
	done
}

tests() {
	pkgdesc="The test modules from the main python package for tkinter"
	replaces="python-tests python2-tests"
	cd "$pkgdir"
	_mv_files usr/lib/python*/*/test
}

_idle() {
	pkgdesc="IDE for Python2 using Tkinter"
	depends="$pkgname=$pkgver-r$pkgrel"
	cd "$pkgdir"
	_mv_files usr/bin
}

sha512sums="78310b0be6388ffa15f29a80afb9ab3c03a572cb094e9da00cfe391afadb51696e41f592eb658d6a31a2f422fdac8a55214a382cbb8cfb43d4a127d5b35ea7f9  Python-2.7.14.tar.xz"
