#! /bin/env bash

NAME=tcl
VERSION=8.6.4
RELEASE=1
CATEGORY="Interpreters"
SUMMARY="Tool Command Language"
DESCRIPTION="Tcl provides a powerful platform for creating integration
applications that tie together diverse applications, protocols, devices,
and frameworks. When paired with the Tk toolkit, Tcl provides the fastest
and most powerful way to create GUI applications that run on PCs, Unix, and
the Macintosh. Tcl can also be used for a variety of web-related tasks and
for creating powerful command languages for applications."
HOMEPAGE=http://tcl.tk/
SRC_URI="mirror://sourceforge/tcl/tcl${VERSION}-src.tar.gz
         http://tcl.cvs.sourceforge.net/viewvc/tcl/tclconfig/tcl.m4"
SRC_DIR="tcl${VERSION}"
PATCH_URI="
	tcl-8.6.4-cygwin_unix.patch
	tcl-8.6.4-cygwin_version.patch
	tcl-8.6.4-cygwin_library_tm.patch
	tcl-8.6.4-cygwin_library_configure_in.patch
	tcl-8.6.4-cygwin_makefile_tm.patch
	tcl-8.6.4-cygwin_platform.patch
"
if cross_compiling
then
	:
	# PATCH_URI+=" 8.5-cross-install.patch"
fi

PKG_NAMES="tcl86 tcl86-doc tcl86-devel"
tcl86_CONTENTS="
	$(__host_prefix | tail -c +2)/bin/
	$(__host_prefix | tail -c +2)/lib/
"
tcl86_devel_CONTENTS="
	$(__host_prefix | tail -c +2)/include/
	$(__host_prefix | tail -c +2)/share/aclocal/*.m4
"

tcl86_doc_CONTENTS="
	$(__host_prefix | tail -c +2)/share/doc/
	$(__host_prefix | tail -c +2)/share/man/
"

REQUIRES=tzcode

DIFF_EXCLUDES='aclocal.m4 configure pkgs'

src_compile() {
	cd "${S}/unix"
	cygautoreconf

	cd "${S}/win"
	cygautoreconf
	cd "${S}/unix"


	if [ -e "${S}/pkgs" ]; then
		mv "${S}/pkgs" "${S}/pkgs_cygport"
	fi

	mkdir -p "${B}/unix"
	cd "${B}/unix"
	mkdir -p ../win
	cygconf --enable-man-symlinks tcl_cv_strtod_buggy=no \
		tcl_cv_sys_version=CYGWIN_NT \
		--includedir="$(__host_prefix)/include/tcl$VERSION" \
		--libdir="$(__host_prefix)/lib/tcl$VERSION"
	cygmake -j1

	# This was an attempt to not make pkgs, but the Makefile thwarted that.
	# Instead, just move pkgs to pkgs_cygport
	#cygmake -j1 binaries libraries doc

	cygmake libtcl$VERSION.a STUB_LIB_FILE=libtcl$VERSION.a STUB_LIB_OBJS=\${OBJS}
}

src_install() {
	cd ${B}/unix
	cyginstall install-headers install-private-headers 

	#mv ${D}/usr/lib/libtcl${VERSION%.*}.dll ${D}/usr/bin/
	__doinstall 0644 libtcl$VERSION.dll.a "$(__host_prefix)/lib/tcl$VERSION"

	#sed -i.bak \
	#	-e "s|^\(TCL_BUILD_LIB_SPEC\)='.*|\1='-Wl,/usr/lib/libtcl${VERSION%.*}.dll.a'|" \
	#	-e "s|^\(TCL_SRC_DIR\)='.*'|\1='/usr/include/tcl${VERSION%.*}'|" \
	#	-e "s|^\(TCL_BUILD_STUB_LIB_SPEC\)='.*|\1='-Wl,/usr/lib/libtclstub${VERSION%.*}.a'|" \
	#	-e "s|^\(TCL_BUILD_STUB_LIB_PATH\)='.*/unix|\1='/usr/lib|" \
	#	-e "s|^\(TCL_STUB_LIB_SPEC\)='.*|\1='-Wl,-ltclstub${VERSION%.*}'|" \
	#	${D}/usr/lib/tclConfig.sh || error

	## install private headers
	#includeinto tcl${VERSION%.*}/unix
	#doinclude ${S}/unix/*.h
	#includeinto tcl${VERSION%.*}/generic
	#doinclude ${S}/generic/*.h

	# install build system macros
	insinto $(__host_prefix)/share/aclocal
	newins ${S}/unix/tcl.m4 tcl-sc.m4
	newins ${S}/tcl.m4 tcl-tea.m4

	## make the default tcl version
	dosym tclsh$VERSION.exe $(__host_prefix)/bin/tclsh${VERSION%.*}
	dosym tclsh${VERSION%.*} $(__host_prefix)/bin/tclsh${VERSION%.*.*}
	dosym tclsh${VERSION%.*.*} $(__host_prefix)/bin/tclsh
	dosym libtcl$VERSION.dll $(__host_prefix)/bin/libtcl${VERSION%.*}.dll
	dosym libtcl${VERSION%.*}.dll $(__host_prefix)/bin/libtcl.dll

	dosym tcl$VERSION $(__host_prefix)/include/tcl${VERSION%.*}

	dosym tcl$VERSION $(__host_prefix)/lib/tcl${VERSION%.*}
	dosym tcl${VERSION%.*} $(__host_prefix)/lib/tcl

	dosym tcl${VERSION}/tclConfig.sh $(__host_prefix)/lib/tclConfig.sh

	dosym libtcl$VERSION.dll.a \
		$(__host_prefix)/lib/tcl$VERSION/libtcl${VERSION%.*}.dll.a
	dosym libtcl${VERSION%.*}.dll.a \
		$(__host_prefix)/lib/tcl$VERSION/libtcl.dll.a

	dosym libtclstub$VERSION.a \
		"$(__host_prefix)/lib/tcl$VERSION/libtclstub${VERSION%.*}.a"
	dosym libtclstub${VERSION%.*}.a \
		"$(__host_prefix)/lib/tcl$VERSION/libtclstub.a"

	dosym tcl${VERSION}/libtcl$VERSION.dll.a \
		"$(__host_prefix)/lib/libtcl$VERSION.dll.a"
	dosym libtcl$VERSION.dll.a \
		"$(__host_prefix)/lib/libtcl${VERSION%.*}.dll.a"
	dosym libtcl${VERSION%.*}.dll.a \
		$(__host_prefix)/lib/libtcl.dll.a

	dosym tcl${VERSION}/libtclstub$VERSION.a \
		$(__host_prefix)/lib/libtclstub$VERSION.a
	dosym libtclstub$VERSION.a $(__host_prefix)/lib/libtclstub${VERSION%.*}.a
	dosym libtclstub${VERSION%.*}.a $(__host_prefix)/lib/libtclstub.a

}
