SUMMARY="A windowing toolkit for use with Tcl"
DESCRIPTION="
Tk is a graphical user interface toolkit that takes developing desktop \
applications to a higher level than conventional approaches. Tk is the \
standard GUI not only for Tcl, but for many other dynamic languages, and \
can produce rich, native applications that run unchanged across Windows, \
Mac OS X, Linux and more.

This package is based on UndroidWish, a SDL2-based Tk port."
HOMEPAGE="https://www.androwish.org"
COPYRIGHT="Regents of the University of California, Sun Microsystems, Inc., Scriptics Corporation, and other parties"
LICENSE="BSD (2-clause)"
REVISION="1"
SOURCE_URI="http://androwish.org/download/androwish-c48f047f5b.tar.gz"
CHECKSUM_SHA256="4ecbc23193cb00b9fc34c0942e2327f0b6badbe587ab574b492780994707683d"
SOURCE_DIR="androwish-c48f047f5b/jni/sdl2tk/"
PATCHES="tk-$portVersion.patchset"

ARCHITECTURES="all"

PROVIDES="
	tk = $portVersion compat >= 8.6
	cmd:wish = $portVersion compat >= 8.6
	cmd:wish8.6 = $portVersion compat >= 8.6
	lib:libtk8.6 = $portVersion compat >= 8.6
	"
REQUIRES="
	haiku
	tcl
	lib:libz
	lib:libfreetype
	lib:libSDL2_2.0
	"

PROVIDES_devel="
	tk_devel = $portVersion
	devel:libtk8.6 = $portVersion
	devel:libtkstub8.6 = $portVersion
	"
REQUIRES_devel="
	tk == $portVersion
	"

BUILD_REQUIRES="
	tcl_devel
	devel:libz
	devel:libfreetype
	devel:libSDL2
	"
BUILD_PREREQUIRES="
	haiku_devel
	cmd:awk
	cmd:gcc
	cmd:ld
	cmd:make
	"

BUILD()
{
	cd sdl

	LDFLAGS="-Wl,--no-undefined" \
	CFLAGS=-DTclpGetMonotonicTime=Tcl_GetTime \
	runConfigure ./configure \
		--with-tcl=/system/develop/lib/ \
		--enable-man-symlinks

	# patch Makefile for Haiku compatibility
	sed -i s/-lsocket// Makefile
	if [ "$effectiveTargetArchitecture" = x86_gcc2 ]; then
		sed -i s/-lstdc++/-lstdc++.r4/ Makefile
	fi

	# patch Makefile for shared Tcl compatibility
	sed -i 's/-ltcl8.6/-ltcl8.6 -ltclstub8.6/' Makefile

	# patch Makefile to be "regular Tk"
	sed -i s/tkZipMain/tkMain/ Makefile
	sed -i s/libsdl2tk/libtk/ Makefile
	sed -i s/sdl2wish/wish/ Makefile
	sed -i s/sdl2tk8/tk8/ Makefile

	make
}

INSTALL()
{
	cd sdl
	make install

	# rearrange to be "regular Tk"
	mv $libDir/sdl2tkConfig.sh $libDir/tkConfig.sh
	mv $libDir/pkgconfig/sdl2tk.pc $libDir/pkgconfig/tk.pc

	# add standard symlinks
	ln -s $binDir/wish8.6 $binDir/wish

	prepareInstalledDevelLibs libtkstub8.6
	fixPkgconfig
	mv $libDir/tkConfig.sh $developLibDir
	packageEntries devel $developDir
}
