#!/usr/bin/make -f
# -*- makefile -*-

include /usr/share/dpkg/default.mk

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

# 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)

%:
	dh $@

override_dh_auto_configure:
	# build nox version first
	CFLAGS="$(CFLAGS)" ./configure -C --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --without-x
	$(MAKE) CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)"
	cp src/cluster cluster.nox
	$(MAKE) clean
	# now configure X version
	CFLAGS="$(CFLAGS)" ./configure -C --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --with-x 

override_dh_auto_build-arch:
	# $(MAKE) CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)"
	dh_auto_build
	cp src/cluster cluster.x

override_dh_auto_build-indep:
	#error with prefix defined:  perl Makefile.PL prefix=$(CURDIR)/debian/$(DEB_SOURCE)/usr/
	perl Makefile.PL INSTALL_BASE=$(CURDIR)/debian/cluster3/usr/
	make
	make test
	make install

cluster3.1: debian/cluster3.sgml
	docbook-to-man $< > $@

xcluster3.1: debian/xcluster3.sgml
	docbook-to-man $< > $@

override_dh_clean:
	dh_clean src/*.o X11/*.o src/cluster X11/cluster src/Makefile src/Makefile.old X11/Makefile Makefile MANIFEST

override_dh_install:
	dh_install
	mkdir -p $(CURDIR)/debian/$(DEB_SOURCE)/usr/bin
	mv cluster.x $(CURDIR)/debian/$(DEB_SOURCE)/usr/bin/xcluster3
	mv cluster.nox $(CURDIR)/debian/$(DEB_SOURCE)/usr/bin/cluster3
	mkdir -p $(CURDIR)/debian/$(DEB_SOURCE)/usr/share/doc/$(DEB_SOURCE)
	cp doc/* $(CURDIR)/debian/$(DEB_SOURCE)/usr/share/doc/$(DEB_SOURCE)
	# upstream Makefile installs copy of html documentation to unusual place - remove it
	find debian/$(DEB_SOURCE)/usr -type d -name cluster | xargs rm -rf
	# upstream Makefile installs executable cluster in /usr/bin - conflicting name, we are using cluster3
	find debian/$(DEB_SOURCE)/usr/bin -type f -name cluster -delete

get-orig-source:
	mkdir -p ../tarballs
	uscan --verbose --force-download --destdir=../tarballs
