#!/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

%:
	dh $@

override_dh_auto_configure:
	cp -b /usr/share/misc/config.guess aux/
	cp -b /usr/share/misc/config.sub aux/
	# A bit ugly way to prevent autoconfing after ./configure
	touch aclocal.m4 && \
	touch stamp-h.in && \
	touch config.h.in && \
	touch Makefile.in && \
	touch configure && \
	dh_auto_configure -- \
			  --with-tcl=/usr/lib \
			  --with-tcl-includes=/usr/include/tcl

override_dh_clean:
	-mv -f aux/config.guess~ aux/config.guess
	-mv -f aux/config.sub~ aux/config.sub
	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/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_2.1.0.orig.tar.gz \
		http://sourceforge.net/projects/tclreadline/files/tclreadline/tclreadline-2.1.0/tclreadline-2.1.0.tar.gz

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