#!/usr/bin/make -f

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


DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# Disabled because this FTBFS with multiple jobs (bug 620973)
#ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
#         NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
#         MAKEFLAGS += -j$(NUMJOBS)
#endif

# Add here any variable or target overrides you need.
# where sources are
DEB_SRCDIR = $(CURDIR)/src

## The following was injected to enable hardening but those flags are overriden
## in debian/Makefile*.inc files - so this code is rather injected there
#CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
#CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
#CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
#LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

CCP=mpicc
CFLAGS=$(CFLAGS) -I. -fPIC -Drestrict=__restrict -DCOMMON_FILE_COMPRESS_GZ -DCOMMON_PTHREAD -DCOMMON_RANDOM_FIXED_SEED -DSCOTCH_RENAME
LDFLAGS=$(LDFLAGS) -lz -lm -lrt

DEB_DBG_PACKAGE_scotch = scotch-dbg
DEB_DBG_PACKAGE_ptscotch = ptscotch-dbg
DEB_DBG_PACKAGE_libscotch-5.1 = libscotch-dbg
DEB_DBG_PACKAGE_libptscotch-5.1 = libptscotch-dbg

DEB_STRIPPED_UPSTREAM_VERSION = $(shell echo $(DEB_UPSTREAM_VERSION) | sed -n -e 's/\.dfsg.*$$//p')
SCOTCH_VERSION=$(DEB_STRIPPED_UPSTREAM_VERSION)

INTSIZE_VERS := $(shell echo "int int32 int64 long")

get-orig-source:
	mkdir tmp; \
	cd tmp; \
	wget http://gforge.inria.fr/frs/download.php/5218/scotch_$(SCOTCH_VERSION)_esmumps.tar.gz; \
	tar xzf scotch_$(SCOTCH_VERSION)_esmumps.tar.gz; \
	mv scotch_5.1_esmumps scotch-$(SCOTCH_VERSION).dfsg;\
	cd scotch-$(SCOTCH_VERSION).dfsg;\
	rm -f doc/ptscotch_user5.1.pdf doc/scotch_user5.1.ps.gz doc/ptscotch_user5.1.ps.gz  doc/scotch_user5.1.pdf; \
	cd .. ; \
	tar czf ../scotch_$(SCOTCH_VERSION).dfsg.orig.tar.gz scotch-$(SCOTCH_VERSION).dfsg; \
	cd ..; rm -rf tmp

patch-stamp:
	QUILT_PATCHES=debian/patches quilt push -a || test $$? = 2
	touch $@

build: build-arch
build-arch: build-arch-stamp
build-arch-stamp: patch-stamp
	dh_testdir
	dh_prep

	for v in $(INTSIZE_VERS); do \
	   mkdir -p $(CURDIR)/$$v; \
	   mkdir -p $(CURDIR)/$$v/bin; \
	   mkdir -p $(CURDIR)/$$v/lib; \
	   mkdir -p $(CURDIR)/$$v/include; \
	   mkdir -p $(CURDIR)/$$v/share/man/man1; \
	done

	for v in $(INTSIZE_VERS); do \
	   cp debian/Makefile_$$v.inc src/Makefile.inc; \
	   (cd src && $(MAKE) realclean prefix=$(CURDIR)/$$v); \
	   (cd src && $(MAKE) scotch ptscotch prefix=$(CURDIR)/$$v); \
	   (cd src && $(MAKE) install prefix=$(CURDIR)/$$v); \
	   mv $$v/bin/gbase $$v/bin/scotch_gbase; \
	   mv $$v/bin/gmap $$v/bin/scotch_gmap; \
	   rm -rf lib; \
	   rm -f src/Makefile.inc; \
	done

	touch $@

build-indep:

install: build
#	Install [par]metis-compatibility symlinks
	for v in $(INTSIZE_VERS); do \
	   (cd $$v/lib && ln -s libscotchmetis.a libmetis.a); \
	   (cd $$v/lib && ln -s libscotchmetis.so libmetis.so); \
	   (cd $$v/lib && ln -s libptscotchparmetis.a libparmetis.a); \
	   (cd $$v/lib && ln -s libptscotchparmetis.so libparmetis.so); \
	done

#	add suffix to {int32,int64,long}/bin/*
	for file in $(shell ls int32/bin); do \
	   (cd int32/bin && mv $$file $$file-int32); \
	done
	for file in $(shell ls int64/bin); do \
	   (cd int64/bin && mv $$file $$file-int64); \
	done
	for file in $(shell ls long/bin); do \
	   (cd long/bin && mv $$file $$file-long); \
	done

#	Fix lintian macro `brol2-0'' not defined
	cp -f debian/dgscat.1 int/share/man/man1
#	Fix lintian  macro `-':' not defined
	cp -f debian/gout.1 int/share/man/man1
#	Fix lintian warnings for undefined macro in man pages
	perl -pi -e 's|\.\"|\.\\\"|' int/share/man/man1/*.1
	mv int/share/man/man1/gbase.1 int/share/man/man1/scotch_gbase.1
#       Rename gmap to avoid clash with package gmap
	mv int/share/man/man1/gmap.1 int/share/man/man1/scotch_gmap.1
#	Fix lintian missing man pages
	cp debian/scotch_esmumps.1 int/share/man/man1	
	cp debian/ptscotch_esmumps.1 int/share/man/man1	
#	Fix lintian warnings
	for file in $(shell ls int/bin); do \
	   (cd int/share/man/man1 && gzip -9n $$file.1); \
	done

	for file in $(shell ls int/bin); do \
	   (cd int/share/man/man1 && ln -sf $$file.1.gz $$file-int32.1.gz); \
	   (cd int/share/man/man1 && ln -sf $$file.1.gz $$file-int64.1.gz); \
	   (cd int/share/man/man1 && ln -sf $$file.1.gz $$file-long.1.gz); \
	done

clean:
	dh_testdir
	dh_clean
	for v in $(INTSIZE_VERS); do \
	   (if [ -d $$v ]; then rm -rf $$v; fi); \
	done
	cp debian/Makefile.inc src/
	make -C src clean
	rm -f src/Makefile.inc
	QUILT_PATCHES=debian/patches quilt pop -a -R || test $$? = 2
	rm -f patch-stamp
	rm -f build-arch-stamp
	rm -rf .pc lib bin include
	find src -name \*.so | xargs -r rm
	find src -name \*.a | xargs -r rm

binary-indep: install
	dh_testdir -i
	dh_testroot -i
	dh_installdirs -i
	dh_install -i
	dh_installdocs -i
	dh_installchangelogs -i
	dh_link -i
	dh_compress -i -XMakefile -Xcc -Xinp
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: install
	dh_testdir -a
	dh_testroot -a
	dh_installdirs -a
	dh_install -a
	dh_installdocs -a
	dh_installchangelogs -a
	dh_link -a
	dh_strip -pscotch --dbg-package=scotch-dbg
	dh_strip -pptscotch --dbg-package=ptscotch-dbg
	dh_strip -plibscotch-5.1 --dbg-package=libscotch-dbg
	dh_strip -plibptscotch-5.1 --dbg-package=libptscotch-dbg
	dh_makeshlibs -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

.PHONY: binary binary-arch binary-indep clean
