#!/usr/bin/make -f

v = 8.6
DIR = $(shell pwd)/debian/tmp

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/architecture.mk

LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)
export LDFLAGS

ifeq ($(origin CC),default)
CC = $(DEB_HOST_GNU_TYPE)-gcc
endif

ifeq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
# See bug #446335 for -fno-unit-at-a-time
ifeq ($(DEB_HOST_ARCH), hppa)
# Disable stack checking for hppa (it doesn't work properly yet)
CFLAGS += -fno-unit-at-a-time -DTCL_NO_STACK_CHECK=1
else
CFLAGS += -fno-unit-at-a-time
endif
endif

%:
	dh $@

override_dh_auto_clean:
	[ ! -f unix/Makefile ] || $(MAKE) -C unix distclean

override_dh_autoreconf:
	dh_autoreconf autoreconf -- -f -i unix

override_dh_auto_configure:
	# So so ugly but it works...
	touch generic/tclStubInit.c
	TCL_LIBRARY="/usr/share/tcltk/tcl$(v)" \
	TCL_PACKAGE_PATH="/usr/local/lib/tcltk:/usr/local/share/tcltk:/usr/lib/tcltk/$(DEB_HOST_MULTIARCH):/usr/lib/tcltk:/usr/share/tcltk:/usr/lib/tcltk/tcl$(v):/usr/lib" \
	CC="$(CC)" \
	CFLAGS="$(CFLAGS)" \
	CPPFLAGS="$(CPPFLAGS)" \
	TCL_SHLIB_LD_EXTRAS="-Wl,-soname,\$${TCL_LIB_FILE}" \
	dh_auto_configure -Dunix -- \
		      --includedir=/usr/include/tcl$(v) \
		      --enable-shared \
		      --enable-threads \
		      --enable-dll-unloading \
		      --disable-rpath \
		      --without-tzdata \
		      --enable-man-symlinks

override_dh_auto_build:
	touch generic/tclStubInit.c && \
	$(MAKE) -C unix
	# Build the static library
	cd unix && \
	  LC_ALL=C \
	  ar cr libtcl$(v).a *.o && \
	  ar d libtcl$(v).a tclAppInit.o && \
	  ranlib libtcl$(v).a

override_dh_auto_install:
	$(MAKE) -C unix INSTALL_ROOT=$(DIR) \
			MAN_INSTALL_DIR=$(DIR)/usr/share/man \
			MANN_INSTALL_DIR=$(DIR)/usr/share/man/man3 \
			TCL_MODULE_PATH="/usr/lib/tcltk /usr/share/tcltk" \
		install
	# Fix up the modules
	sed -i -e's:variable paths {}:variable paths {/usr/share/tcltk/tcl$(v)/tcl8}:' \
	    $(DIR)/usr/share/tcltk/tcl$(v)/tm.tcl
	install -d -m 755 $(DIR)/usr/share/tcltk/tcl$(v)/tcl8
	mv $(DIR)/usr/share/tcltk/tcl8/*/* $(DIR)/usr/share/tcltk/tcl$(v)/tcl8
	# Fix up the libraries
	cp unix/libtcl$(v).a $(DIR)/usr/lib/$(DEB_HOST_MULTIARCH)
	install -d -m 755 $(DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/tcl$(v)
	mv $(DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/*.sh $(DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/tcl$(v)
	mv $(DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/tcl.pc $(DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/tcl$(v).pc
	install -d -m 755 $(DIR)/usr/share/aclocal
	cp unix/tcl.m4 $(DIR)/usr/share/aclocal/tcl$(v).m4
	rm -f $(DIR)/usr/share/tcltk/tcl$(v)/ldAix
	# Fix up the include files
	install -d $(DIR)/usr/include/tcl$(v)/tcl-private/generic
	cp generic/*.h $(DIR)/usr/include/tcl$(v)/tcl-private/generic
	install -d $(DIR)/usr/include/tcl$(v)/tcl-private/unix
	cp unix/*.h $(DIR)/usr/include/tcl$(v)/tcl-private/unix
	install -d $(DIR)/usr/include/tcl$(v)/tcl-private/compat
	cp compat/*.h $(DIR)/usr/include/tcl$(v)/tcl-private/compat
	install -d $(DIR)/usr/include/tcl$(v)/tcl-private/libtommath
	cp libtommath/*.h $(DIR)/usr/include/tcl$(v)/tcl-private/libtommath
	#
	# Temporary HACK until all reverse dependencies will be rebuilt
	ln -sf libtcl$(v).so $(DIR)/usr/lib/$(DEB_HOST_MULTIARCH)/libtcl$(v).so.0
	#
	# Fix up the manpages.
	cd $(DIR)/usr/share/man/man1 && \
	  cat tclsh.1 | sed -e 's/(n)/(3tcl)/g' > tclsh$(v).1 && \
	  rm tclsh.1
	cd $(DIR)/usr/share/man/man3 && \
	  for f in *.[3n] ; do \
	     f2=$$(echo $$f | sed -e 's/\.[3n]/.3tcl/') ; \
	     if [ -L $$f ]; then \
		l=$$(readlink -n $$f | sed -e 's/\.[3n]/.3tcl/') ; \
		rm $$f ; \
		ln -sf $$l $$f2 ; \
	     else \
		cat $$f | sed -e 's/^\.TH \([^ ]\+\|"[^"]\+"\) [3n]/.TH \1 3tcl/' \
			       -e 's/\(Tk_[0-9A-Za-z]*\)(3)/\1(3tk)/g' \
			       -e 's/\([A-Z][0-9A-Za-z_]*\)(3)/\1(3tcl)/g' \
			       -e 's/send(n)/send(3tk)/g' \
			       -e 's/text(n)/text(3tk)/g' \
			       -e 's/tk(n)/tk(3tk)/g' \
			       -e 's/winfo(n)/winfo(3tk)/g' \
			       -e 's/(n)/(3tcl)/g' \
			       -e "s/\\N'244'/\\[^o]/g" \
			 >$$f2 ; \
		rm $$f ; \
	     fi ; \
	  done

override_dh_makeshlibs:
	dh_makeshlibs -a -Xtcltest

override_dh_shlibdeps:
	dh_shlibdeps -a -ldebian/libtcl$(v)/usr/lib/$(DEB_HOST_MULTIARCH)

get-orig-source:
	CURDIR=`pwd` && \
	TMPDIR=`mktemp -d /tmp/tcl$(v).XXXXXX` && \
	cd $$TMPDIR && \
	wget -O - \
	    http://prdownloads.sourceforge.net/tcl/tcl-core$(v).16-src.tar.gz | tar -zx && \
	for f in `find . -name *.chm` ; do \
	    echo "REMOVING NON-FREE FILE $$f" ; rm -f $$f ; \
	done && \
	tar -zcf $$CURDIR/tcl$(v)_$(v).16+dfsg.orig.tar.gz tcl$(v).16 && \
	rm -rf $$TMPDIR

.PHONY: override_dh_auto_clean override_dh_auto_configure override_dh_auto_install \
	override_dh_makeshlibs override_dh_shlibdeps get-orig-source override_dh_autoreconf
