#!/usr/bin/make -f
#                                                       -*- makefile -*-
# debian/rules file for the Debian/GNU Linux quantlib package
# Copyright (C) 2001 - 2022 by Dirk Eddelbuettel <edd@debian.org>

source  := $(shell head -1 debian/changelog | awk '{print $$1}')
# 'quantlib-config --version' after configure is easier ...

# that currently gets 1.0 only
# reverted back to using libquantlib0
#sonum   := 1.3
#sonum	:= $(shell grep AC_INIT configure.ac | \
#			perl -ne 'm/([0-9\\.]+)/ and print $$1')
#libpkg  := lib$(source)-$(sonum)
#libpkg  := lib$(source)-1.1
libpkg  := lib$(source)0v5

devpkg  := lib$(source)0-dev
smppkg  := $(source)-examples
debtmp	:= $(CURDIR)/debian/$(libpkg)
libdoc	:= $(CURDIR)/debian/$(libpkg)/usr/share/doc/$(libpkg)
arch 	:= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
cpu	:= $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)

#export DH_VERBOSE=1

## edd 29 Nov 2001	Default to g++
## edd 18 Jun 2005 	g++-3.4 for testing
## edd 18 Jul 2005      back to g++
## edd 17 Nov 2005	g++-4.0
## edd 16 Mar 2006      back to g++
cxx	= g++

## edd 13 Apr 2014	use dpkg-buildflags as a base
cxxbuildflags   = 	$(shell dpkg-buildflags --get CXXFLAGS)
cppbuildflags   = 	$(shell dpkg-buildflags --get CPPFLAGS)
buildflags      =       $(cxxbuildflags) $(cppbuildflags)
ldflags  =	 	$(shell dpkg-buildflags --get LDFLAGS)

## edd 18 May 2002	standard flags for libraries
## edd 18 Jun 2005 	no optim., debugging
## edd 18 Jul 2005	back to normal
## edd 17 Nov 2005	added -fpermissive for 0.3.11-2
## edd 13 Apr 2014      use buildflags as base
compilerflags   = $(buildflags) -g0 -O2 -D_REENTRANT -fpermissive

# edd 14 Jan 2003  no longer override g++ as 3.2 is now default everywhere
## 			but use g++-3.0 on ia64
#ifneq "$(findstring $(arch), ia64-linux)" ""
#cxx	= g++-3.0
#endif

## edd 18 May 2002	no optimisation or debugging on baby systems
## edd 14 May 2005	don't do it on mipsel or mips either
## edd 26 Jun 2007 	use cpu test, not arch test -- thanks to Riku via #430709
## edd 13 Apr 2014      use buildflags as base
## edd 18 May 2017      add mipsel64
## bunk 25 Sep 2017     comment out, all buildds are now able to build with -O2
#ifneq "$(findstring $(cpu), m68k arm armeb mipsel mips mipsel64)" ""
#compilerflags   = $(buildflags) -O0 -g0 -D_REENTRANT -fpermissive
#endif

### edd 22 Dec 2002  g++-3.2 for s390
#ifneq "$(findstring $(arch), s390-linux)" ""
#cxx	= g++-3.2
#endif

## edd 19 June 2004	no optimisation on amd64 (#255100)
#ifneq "$(findstring $(arch), x86_64-linux)" ""
#compilerflags   = -O0 -D_REENTRANT
#endif

## edd 18 Jan 2022
makeflags = -j`nproc`

upstream: get-orig-source
get-orig-source:
        #lynx http://quantlib.org/download.html
        #lynx http://prdownloads.sf.net/quantlib
	links http://quantlib.org/download.shtml

prerelease:
#	links http://quantlib.org/gm/
#	links http://quantlib.org/prerelease
	links https://sourceforge.net/projects/quantlib/files/prerelease

configure: configure-stamp
configure-stamp:
	dh_testdir
	dh_autoreconf
        #./autogen.sh
	CXX=$(cxx) 						\
	CXXFLAGS="$(compilerflags)"				\
	CFLAGS="$(compilerflags)"				\
	LDFLAGS="$(ldflags)"                                    \
	MAKEFLAGS="$(makeflags)"                                \
	./configure 	--prefix=/usr 				\
			--enable-openmp   			\
			--enable-intraday			\
			--enable-examples			\
			--enable-benchmark			\
			--enable-shared 			\
			--enable-static 			\
			--with-gnu-ld 				\
			--mandir=$(debtmp)/usr/share/man 	\
			--build $(arch)
        # patch libtool re rpath (see lintian docu)
	sed < libtool > libtool-2 \
			-e 's/^hardcode_libdir_flag_spec.*$$/hardcode_libdir_flag_spec=" -D__LIBTOOL_IS_A_FOOL__ "/' \
			-e '/^archive_cmds="/s/"$$/ \\$$deplibs"/'
	mv libtool-2 libtool
	chmod 755 libtool
        # end libtool rpath patch
	touch configure-stamp

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: configure-stamp
	dh_testdir
	-$(MAKE) 	CFLAGS="$(compilerflags)"  	\
			CXXFLAGS="$(compilerflags)"  	\
			LDFLAGS="$(ldflags)"            \
			MAKEFLAGS="$(makeflags)"        \
			CXX=$(cxx)

        # edd 17 May 2005  create shlibs on the fly
        # edd 11 Feb 2007  libQuantLibFunctions is no more
	# edd 07 Mar 2012  need to tag this as so number 0
	# edd 01 Sep 2013  back to libquantlib0 and fixed file
	#@echo "libQuantLib ${sonum} libquantlib-${sonum}" \
	#	> debian/libquantlib-${sonum}.shlibs
	#@echo "libQuantLib 0 libquantlib-${sonum}" \
	#	> debian/shlibs.local
        #@echo "libQuantLibFunctions ${sonum} libquantlib-${sonum}" \
        #	>> debian/libquantlib-${sonum}.shlibs

	touch build-stamp

test:	test-stamp
test-stamp: build-stamp
ifeq "$(findstring $(cpu), m68k arm aarch64 armeb armel mipsel mips mips64el hppa)" ""
	-LD_LIBRARY_PATH=$(CURDIR)/ql/.libs $(MAKE) $(makeflags) check
        #-LD_LIBRARY_PATH=$(CURDIR)/ql/.libs $(MAKE) benchmark
endif
	touch test-stamp

clean:
	dh_testdir
	dh_testroot
	dh_autoreconf_clean
	rm -f configure-stamp build-stamp install-stamp test-stamp
	rm -rf debian/static/
	dh_clean lib/*so* build/*.so*
	[ ! -f Makefile ] || $(MAKE) distclean
	-rm -rf config.log config.status
	#-rm -rf debian/libquantlib-${sonum}.shlibs

install: install-stamp
install-stamp: test-stamp
	$(MAKE) -f debian/rules DH_OPTIONS= install-work
install-work:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	-$(MAKE) prefix=$(debtmp)/usr	 	  install
	-$(MAKE) prefix=$(debtmp)/usr	 	  install-am
        # clean usr/aclocal
	rm -rvf 	$(debtmp)/usr/aclocal
	dh_installexamples	-p$(libpkg)	Examples/ 	\
						test-suite/	\
						quantlib.el
	(cd $(libdoc)/examples/Examples; 			\
		rm -vf  */*.o */*.dsp */*.mak *.dsw *.mak;	\
		rm -rvf */.deps/ */.libs/;			\
		find . -type f -perm 0755 | xargs rm -vf ) || true
	(cd $(libdoc)/examples/test-suite; 			\
		rm -rvf  *.o .deps/ .libs/;			\
		find . -type f -perm 0755 | xargs rm -vf ) || true
#	dh_installman	-p$(devpkg)	debian/quantlib-test-suite.1
        # now part of upstream: debian/DiscreteHedging.1 \
        #                       debian/quantlib-config.1 \
        # defunct		debian/Parities.1
#	dh_installdirs 	-p$(smppkg) 	usr/share/man/man1
#	dh_installman	-p$(smppkg)	debian/EquityOption.1 \
#					debian/ConvertibleBonds.1
#	dh_installman	-p$(smppkg)	debian/FittedBondCurve.1
#	dh_installman	-p$(smppkg)	debian/CDS.1 debian/CallableBonds.1
        # install -d debian/libquantlib0/usr/bin
        # install -m 0755 -c quantlib-config debian/libquantlib0/usr/bin
        # move static libs into dev-package
	dh_movefiles 	-p$(devpkg) --sourcedir=debian/$(libpkg)
        # move example files into smp-package
	dh_movefiles 	-p$(smppkg) --sourcedir=debian/$(libpkg)
        # some cleanup after movefiles
	-rm -vf  	$(debtmp)/usr/lib/lib*.la
	-rm -rvf 	$(debtmp)/usr/include
	-rm -rvf	$(debtmp)/usr/share/doc/$(libpkg)/examples/
        # install lintian "silencer"
        # edd 15 May 2005  no longer needed	#
        #dh_installdirs -p$(libpkg) usr/share/lintian/overrides
        #install -p -m 0644  debian/$(libpkg).lintian \
        #	$(debtmp)/usr/share/lintian/overrides/$(libpkg)
        # remove redundant LGPL license
	rm -vf 	$(CURDIR)/debian/$(devpkg)/usr/share/doc/$(libpkg)/examples/test-suite/CPPUNIT-COPYING
        # try this here
	dh_installchangelogs	-p$(libpkg) -N$(devpkg) -N$(smppkg) ChangeLog.txt
        # remove empty examples/ dir
#	rm -vf 	$(CURDIR)/debian/$(devpkg)/usr/share/doc/$(libpkg)/examples/test-suite/CPPUNIT-COPYING
	touch install-stamp

# This single target is used to build all the packages, all at once, or
# one at a time. So keep in mind: any options passed to commands here will
# affect _all_ packages. Anything you want to only affect one package
# should be put in another target, such as the install target.
binary-common: build test install
	dh_testdir
	dh_testroot
#	dh_installdebconf
	dh_link			-p$(smppkg) -p$(devpkg)
	dh_installdocs		-p$(libpkg) -N$(devpkg) -N$(smppkg)	\
						README.md  		\
						Contributors.txt 	\
						News.md
#	dh_installmenu
#	dh_installemacsen
#	dh_installpam
#	dh_installinit
#	dh_installcron
#	dh_installinfo
#	dh_undocumented
	dh_strip
#	dh_link
	dh_compress
	dh_lintian
	dh_fixperms
#	dh_suidregister
	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps
	dh_gencontrol
#	dh_md5sums

        # because of how we install and move examples, copyright files are
        # spuriously installed -- remove'em here as both are gotten via
        # via symlink to the one in the main package
	rm -vf	$(CURDIR)/debian/$(smppkg)/usr/share/doc/$(libpkg)/copyright \
		$(CURDIR)/debian/$(devpkg)/usr/share/doc/$(libpkg)/copyright

	dh_builddeb || true


# Build architecture independant packages using the common target.
binary-indep: build install
# (Uncomment this next line if you have such packages.)
#	 $(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

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

# Any other binary targets build just one binary package at a time.
binary-%: build install
	make -f debian/rules binary-common DH_OPTIONS=-p$*

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