#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

dpkg_buildflags = DEB_LDFLAGS_MAINT_APPEND="-Wl,-z,defs" \
	DEB_BUILD_MAINT_OPTIONS="hardening=+all" dpkg-buildflags

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

PKGNAME  = $(shell head -1 debian/changelog | sed 's/ .*//')
VERSION	 = $(shell head -1 debian/changelog | sed 's/.*(//;s/).*//')
PKGFILES = $(PKGNAME)_$(VERSION)_$(shell dpkg --print-architecture).deb \
	   $(PKGNAME)-dev_$(VERSION)_$(shell dpkg --print-architecture).deb \
	   $(PKGNAME)-doc_$(VERSION)_all.deb
CHANGES	 = $(PKGNAME)_$(VERSION)_$(shell dpkg --print-architecture).changes

TCLX_VERSION   = $(shell head -1 debian/changelog | sed 's/ .*//;s/tclx//')
TCL_THREADS    = $(shell . /usr/lib/tclConfig.sh && echo $$TCL_THREADS)
ENABLE_THREADS = $(shell test "$(TCL_THREADS)" = 1 && echo --enable-threads)
DESTDIR	       = $(CURDIR)/debian/tmp

configure: tclconfig/tcl.m4
	# Rebuild configure with new tcl.m4
	dh_autoreconf
	rm -rf autom4te.cache

config.status: configure
	dh_testdir
	# Not done here since configure must be invoked
	# separately for static and shared libraries.
	#./configure \
	#    --host=$(DEB_HOST_GNU_TYPE) \
	#    --build=$(DEB_BUILD_GNU_TYPE) \
	#    --prefix=/usr \
	#    --mandir=\$${prefix}/share/man \
	#    --infodir=\$${prefix}/share/info \
	#    --with-tcl=/usr/lib \
	#    --enable-shared \
	#    $(ENABLE_THREADS) \
	#    CFLAGS="$(CFLAGS)" \
	#    LDFLAGS="-Wl,-z,defs"

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp

build-stamp: build-stamp-static build-stamp-shared
	touch build-stamp-static
	touch build-stamp-shared
	touch $@

build-stamp-static: config.status
	dh_testdir
	# Compile the static library
	rm -f config.cache config.log config.status *.o pkgIndex.tcl
	./configure \
	    --host=$(DEB_HOST_GNU_TYPE) \
	    --build=$(DEB_BUILD_GNU_TYPE) \
	    --prefix=/usr \
	    --mandir=\$${prefix}/share/man \
	    --infodir=\$${prefix}/share/info \
	    --with-tcl=/usr/lib \
	    --disable-shared \
	    $(ENABLE_THREADS) \
	    $(shell $(dpkg_buildflags) --export=configure)
	$(MAKE)
	touch $@

build-stamp-shared: config.status
	dh_testdir
	# Compile the shared library
	rm -f config.cache config.log config.status *.o pkgIndex.tcl
	./configure \
	    --host=$(DEB_HOST_GNU_TYPE) \
	    --build=$(DEB_BUILD_GNU_TYPE) \
	    --prefix=/usr \
	    --mandir=\$${prefix}/share/man \
	    --infodir=\$${prefix}/share/info \
	    --with-tcl=/usr/lib \
	    --enable-shared \
	    $(ENABLE_THREADS) \
	    $(shell $(dpkg_buildflags) --export=configure)
	$(MAKE)
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp*

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	dh_autoreconf_clean
	dh_clean
	-rm -rf pkgIndex.tcl libtclx*.* config.* $$(find . -name CVS)

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/tmp.
	$(MAKE) install DESTDIR=$(DESTDIR)
	cp -fp libtclx*.a $(DESTDIR)/usr/lib/

	# Fix the installation
	mv $(DESTDIR)/usr/lib/tclx$(TCLX_VERSION)/lib* $(DESTDIR)/usr/lib/
	ln -nfs libtclx$(TCLX_VERSION).so.0 \
	    $(DESTDIR)/usr/lib/libtclx$(TCLX_VERSION).so
	mkdir -p $(DESTDIR)/usr/include/tclx$(TCLX_VERSION)
	mv $(DESTDIR)/usr/include/*.h \
	    $(DESTDIR)/usr/include/tclx$(TCLX_VERSION)/
	sh ./debian/install_manpages $(DESTDIR)
	ln -nfs TclX.3tclx.gz $(DESTDIR)/usr/share/man/man3/tclx.3tclx.gz
	# Load only on tcl8.4
	echo 'if {[package vcompare [info tclversion] 8.4] < 0} return' \
	    > $(DESTDIR)/usr/lib/tclx$(TCLX_VERSION)/pkgIndex.tcl
	sed 's|\$$dir|/usr/lib|' < pkgIndex.tcl \
	    >>$(DESTDIR)/usr/lib/tclx$(TCLX_VERSION)/pkgIndex.tcl

	# Copy files in packages directories
	dh_install -v --sourcedir=debian/tmp

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs ChangeLog -i
	dh_installdocs -i
	# dh_installexamples -i
	# dh_install -i
	# dh_installmenu -i
	# dh_installdebconf -i
	# dh_installlogrotate -i
	# dh_installemacsen -i
	# dh_installpam -i
	# dh_installmime -i
	# dh_python -i
	# dh_installinit -i
	# dh_installcron -i
	# dh_installinfo -i
	dh_installman -i
	dh_link -i
	# dh_strip -i
	dh_compress -i
	dh_fixperms -i
	# dh_perl -i
	# dh_makeshlibs -i
	dh_installdeb -i
	# dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs ChangeLog -a
	dh_installdocs -a
	# dh_installexamples -a
	# dh_install -a
	# dh_installmenu -a
	# dh_installdebconf -a
	# dh_installlogrotate -a
	# dh_installemacsen -a
	# dh_installpam -a
	# dh_installmime -a
	# dh_python -a
	# dh_installinit -a
	# dh_installcron -a
	# dh_installinfo -a
	dh_installman -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	# dh_perl -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install 

package:
	dh_testdir
	dpkg-buildpackage -rfakeroot

dist:
	dh_testdir
	dpkg-buildpackage -rfakeroot

deb:
	dh_testdir
	dpkg-buildpackage -rfakeroot -us -uc

debclean:
	dh_testdir
	fakeroot ./debian/rules clean

debinst:
	dh_testdir
	cd .. && sudo dpkg -i $(PKGFILES)

lintian:
	dh_testdir
	cd .. && lintian -i $(PKGFILES)

upload:
	dh_testdir
	cd .. && grep -q 'PGP SIGNED MESSAGE' $(CHANGES) && dupload $(CHANGES)

