#!/usr/bin/make -f

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

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

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
        MAKEFLAGS += -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

CXXFLAGS += -Wall

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

# with pattern rules multiple targets means those are generated
# by one invocation, so this is a pattern rule...
include/NTL/con%ig.h src/src/m%ileout src/make%ile src/c%ileout: src/configure src/m%ile src/c%ile src/DoConfig
	cd src && ./configure \
		WIZARD=off SHARED=on NATIVE=off \
		CPPFLAGS='$(CPPFLAGS)' CXXFLAGS='$(CXXFLAGS)' LDFLAGS='$(LDFLAGS)' \
		DEF_PREFIX='/usr' \
		NTL_GMP_LIP=on SHARED=on \
		NTL_GF2X_LIB=off

include/NTL/mach_desc.h: src/makefile include/NTL/config.h src/cfileout
	# TODO: better make that file static and check it is correct than
	# generating it this way and perhaps miss something (extended doubles)
	$(MAKE) -C src setup1

src/GetPID.c: src/makefile
	$(MAKE) -C src setup2

src/GetTime.c:
	# This should be the correct one:
	cp src/GetTime2.c src/GetTime.c

include/NTL/gmp_aux.h: src/makefile include/NTL/config.h src/cfileout \
			include/NTL/mach_desc.h
	# this also generates src/lip_gmp_aux_impl.h, which is not used
	# but src/makefile requires it anyway, so it cannot be removed here
	$(MAKE) -C src setup3

build-arch-stamp: src/makefile include/NTL/config.h src/cfileout \
		include/NTL/mach_desc.h src/GetTime.c include/NTL/gmp_aux.h src/GetPID.c
	# ntl.a is quite a misnomer, this is the actual idempotent build target
	$(MAKE) -C src ntl.a
	touch $@

clean:
	if test -e src/makefile ; then $(MAKE) -C src clobber ;fi
	rm -f build-arch-stamp
	rm -f src/makefile include/NTL/config.h
	dh_clean

binary: binary-arch binary-indep
binary-indep:

binary-arch: build-arch-stamp
	dh_testdir
	dh_testroot
	dh_prep
	$(MAKE) -C src install PREFIX='$(CURDIR)/debian/tmp/usr'
	dh_install
	dh_installchangelogs
	mv debian/tmp/usr/share/doc/NTL debian/libntl*-dev/usr/share/doc/libntl*-dev/
	dh_installexamples
	dh_installdocs
	dh_installman
	dh_lintian
	dh_link
	dh_strip
	dh_makeshlibs -V
	dh_shlibdeps
	dh_compress -X/NTL/
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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