#!/usr/bin/make -f

DESTDIR = $(shell pwd)/debian/tcl-tclreadline

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

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

CFLAGS += -DUSE_TCL_STUBS
export CFLAGS

LIBS += -ltclstub
export LIBS

v = 2.3.1

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- \
			  --with-tcl=/usr/lib \
			  --with-tcl-includes=/usr/include/tcl

override_dh_clean:
	dh_clean tclreadline.h tclreadline.n

override_dh_auto_install:
	dh_auto_install
	#
	# Fix up the architecture dependent include
	install -m 755 -d $(DESTDIR)/usr/include/$(DEB_HOST_MULTIARCH)
	mv $(DESTDIR)/usr/include/*.h $(DESTDIR)/usr/include/$(DEB_HOST_MULTIARCH)
	#
	# Fix up the manpage
	mv $(DESTDIR)/usr/share/man/mann/tclreadline.n \
	   $(DESTDIR)/usr/share/man/man3/tclreadline.3tcl
	sed -i -e 's/^\.TH tclreadline n/.TH tclreadline 3tcl/' \
	       -e 's/string ".*$(DEB_HOST_MULTIARCH).*"/directory string/g' \
	   $(DESTDIR)/usr/share/man/man3/tclreadline.3tcl
	#
	# Remove .la file
	rm $(DESTDIR)/usr/lib/*/*.la
	#
	# Remove empty directories
	find $(DESTDIR) -empty -prune -exec rmdir \{\} \;

override_dh_gencontrol:
	tcltk-depends
	dh_gencontrol

get-orig-source:
	wget -O tclreadline_$(v).orig.tar.gz \
		https://github.com/flightaware/tclreadline/archive/v$(v).tar.gz

.PHONY: override_dh_auto_configure override_dh_clean override_dh_auto_install \
	override_dh_gencontrol get-orig-source
