#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

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

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

CFLAGS += -Wall -g -fPIC

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

#Architecture 

configure:	configure-stamp
configure-stamp:
	cp -f /usr/share/misc/config.sub config.sub
	cp -f /usr/share/misc/config.guess config.guess
	touch configure-stamp

build-arch: build-arch-stamp-nox build-arch-clean build-arch-stamp-x cluster3.1 xcluster3.1
build-arch-stamp-nox:  configure
	# Add here commands to compile the arch part of the package.
	CFLAGS="$(CFLAGS)" ./configure -C --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --without-x
	$(MAKE) CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)"
	cp src/cluster cluster.nox
	touch build-arch-stamp-nox

build-arch-stamp-x:  configure
	CFLAGS="$(CFLAGS)" ./configure -C --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --with-x 
	$(MAKE) CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)"
	cp src/cluster cluster.x
	touch build-arch-stamp-x

# in order to build build-arch-stamp-x after build-arch-stamp-nox
# any object file needs to be cleaned
build-arch-clean:  configure
	$(MAKE) clean

install-arch-perl: install-arch-perl-stamp
install-arch-perl-stamp:
	#error with prefix defined:  perl Makefile.PL prefix=$(CURDIR)/debian/cluster3/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 $< > $@

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

	# Add here commands to compile the indep part of the package.
	touch build-indep-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp* build-indep-stamp #CONFIGURE-STAMP#

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) clean

	-find . -name .deps -a -type d | xargs rm -rf

	dh_clean config.status config.cache config.sub config.guess src/*.o X11/*.o src/cluster X11/cluster cluster3.1 xcluster3.1 src/Makefile src/Makefile.old X11/Makefile Makefile

install: install-indep install-arch
install-indep:
	dh_testdir -i
	dh_testroot -i
	dh_prep -i 
	dh_installdirs -i

	# Add here commands to install the indep part of the package into
	# debian/<package>-doc.
	#INSTALLDOC#

	dh_install -i

install-arch: build-arch install-arch-perl
	dh_testdir -a
	dh_testroot -a
	dh_prep -a 
	dh_installdirs -a

	# Add here commands to install the arch part of the package into 
	# debian/tmp.
	cp cluster.x $(CURDIR)/debian/cluster3/usr/bin/xcluster3
	cp cluster.nox $(CURDIR)/debian/cluster3/usr/bin/cluster3
	cp doc/* $(CURDIR)/debian/cluster3/usr/share/doc/cluster3
	cp -r example $(CURDIR)/debian/cluster3/usr/share/cluster3
	dh_install -s

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installmenu
	dh_installman cluster3.1 xcluster3.1
	dh_link
	dh_strip
	dh_compress 
	dh_fixperms
	dh_perl
#	dh_python
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb
# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

binary: binary-arch 

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

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