#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

include /usr/share/quilt/quilt.make

# 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)
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH) 
DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif


CC=/usr/bin/gcc
CXX=/usr/bin/g++
CFLAGS = -O3 -I/usr/include/mysql -I/usr/include/postgresql -I/usr/include/libxml2 -fPIC -DPIC
CXXFLAGS = -O3 -I/usr/include/mysql -I/usr/include/postgresql -I/usr/include/libxml2 -fPIC -DPIC

ifeq (amd64,$(DEB_HOST_ARCH)) 
CFLAGS += -O3 -msse3
CXXFLAGS += -O3 -msse3
endif

# Fix FTBFS on !linux builds, which require explicit pthread linkage
# Using CFLAGS/CXXFLAGS feels like an ugly hack, but no amount of coaxing with
# DEB_LDFLAGS_MAINT_{PRE,AP}PEND seems to get the flag in the right position
ifneq (linux,$(DEB_HOST_ARCH_OS))
CFLAGS += -pthread
CXXFLAGS += -pthread
endif

#CFLAGS = -Wall -g

#ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
#	CFLAGS += -O0
#else
#	CFLAGS += -O2
#endif

dpkg_buildflags = DEB_CFLAGS_MAINT_APPEND="$(CFLAGS)" DEB_CXXFLAGS_MAINT_APPEND="$(CXXFLAGS)" DEB_LDFLAGS_MAINT_APPEND="-Wl,-z,defs" dpkg-buildflags

config.status: configure
configure: configure-stamp
configure-stamp: $(QUILT_STAMPFN)
	dh_testdir
	dh_autotools-dev_updateconfig
	# Add here commands to configure the package.
	libtoolize
	./configure $(CROSS) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info CC="$(CC)" CXX="$(CXX)" --with-mysql=/usr --enable-new-allocator --with-postgresql=/usr --with-boost=/usr/include/boost $(shell $(dpkg_buildflags) --export=configure)
	touch configure-stamp


build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: config.status 
	dh_testdir
	[ -d $(CURDIR)/lib ] || mkdir $(CURDIR)/lib
	$(MAKE) $(shell $(dpkg_buildflags) --export=configure)
	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp 
	[ ! -f Makefile ] || $(MAKE) distclean
	-rmdir $(CURDIR)/lib
	dh_autotools-dev_restoreconfig
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
	$(MAKE) -C examples clean
	rm -f debian/tmp/usr/lib/*.la


# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i
	dh_installdocs -i -XChangeLog
	dh_installexamples -i
	dh_install -i --list-missing
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	# dh_fixperms doesn't quite handle everything
	find $(CURDIR)/debian/libquickfix-doc/usr/share/doc/libquickfix-doc/examples -type f |xargs -r chmod 644
	dh_makeshlibs -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a
	dh_installdocs -a -XChangeLog
	dh_installexamples -a
	dh_install -a --list-missing
	dh_installman -a
	dh_link -a
	dh_strip -a -Xexamples --dbg-package=libquickfix14-dbg
	dh_compress -a
	dh_fixperms -a
	# dh_fixperms doesn't quite handle everything
	find $(CURDIR)/debian/libquickfix-doc/usr/share/doc/libquickfix-doc/examples -type f |xargs -r chmod 644
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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