#!/usr/bin/make -f
#export DH_VERBOSE=1

DESTDIR=$(CURDIR)/debian/php5-mongo

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1

# This Makefile snippet defines the following variables:
#
# CFLAGS: flags for the C compiler
# CPPFLAGS: flags for the C preprocessor
# CXXFLAGS: flags for the C++ compiler
# FFLAGS: flags for the Fortran compiler
# LDFLAGS: flags for the linker
#
# You can also export them in the environment by setting
# DPKG_EXPORT_BUILDFLAGS to a non-empty value.
#
# This list is kept in sync with the default set of flags returned
# by dpkg-buildflags.

dpkg_late_eval ?= $(or $(value DPKG_CACHE_$(1)),$(eval DPKG_CACHE_$(1) := $(shell $(2)))$(value DPKG_CACHE_$(1)))

DPKG_BUILDFLAGS_LIST = CFLAGS CPPFLAGS CXXFLAGS FFLAGS LDFLAGS

define dpkg_buildflags_export_envvar
ifdef $(1)
DPKG_BUILDFLAGS_EXPORT_ENVVAR += $(1)="$(value $(1))"
endif
endef

$(eval $(call dpkg_buildflags_export_envvar,DEB_BUILD_MAINT_OPTIONS))
$(foreach flag,$(DPKG_BUILDFLAGS_LIST),\
  $(foreach operation,SET STRIP APPEND PREPEND,\
    $(eval $(call dpkg_buildflags_export_envvar,DEB_$(flag)_MAINT_$(operation)))))

dpkg_buildflags_setvar = $(1) = $(call dpkg_late_eval,$(1),$(DPKG_BUILDFLAGS_EXPORT_ENVVAR) dpkg-buildflags --get $(1))

$(foreach flag,$(DPKG_BUILDFLAGS_LIST),\
  $(eval $(call dpkg_buildflags_setvar,$(flag))))

ifdef DPKG_EXPORT_BUILDFLAGS
  export $(DPKG_BUILDFLAGS_LIST)
endif

CFLAGS          ?= $(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS        ?= $(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS         ?= $(shell dpkg-buildflags --get LDFLAGS)
CFLAGS          += $(HARDENING_CFLAGS)
CPPFLAGS        += $(HARDENING_CPPLAGS)
LDFLAGS         += $(HARDENING_LDFLAGS)

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

# Enable IEEE-conformant floating point math on alphas (not the default)
ifeq (alpha-linux-gnu,$(DEB_HOST_GNU_TYPE))
	CFLAGS += -mieee
endif

ifeq ($(DEB_HOST_GNU_TYPE), $(findstring $(DEB_HOST_GNU_TYPE), ia64-linux-gnu powerpc64-linux-gnu))
	CFLAGS += -g
else
	CFLAGS += -gstabs
endif

configure: configure-stamp
configure-stamp:
	phpize5
	./configure --prefix=/usr --with-php-config=/usr/bin/php-config5
	touch $@

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

build-stamp: configure
	$(MAKE) DESTDIR=$(DESTDIR)
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -rf configure-stamp build-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
	phpize --clean
	find $(CURDIR)/tests \( -name \*.diff -o -name \*.exp -o -name \*.log -o -name \*.out -o -name \*.sh \) -exec rm {} \;
	rm -f $(CURDIR)/tests/mongo-test-cfg.inc
	dh_clean

install: check
	dh_testdir
	dh_testroot
	dh_prep
	$(MAKE) install INSTALL_ROOT=$(DESTDIR)
	install -D -m 644 debian/mongo.ini \
		debian/php5-mongo/etc/php5/mods-available/mongo.ini

check: build
	cp $(CURDIR)/tests/mongo-test-cfg.inc.template \
		$(CURDIR)/tests/mongo-test-cfg.inc
#	$(MAKE) test

binary-indep: install

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installdirs -a
	dh_installchangelogs -a
	dh_installdocs -a
	dh_installexamples -a
	dh_install -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	echo "php:Depends=phpapi-`php-config5 --phpapi`" >> debian/php5-mongo.substvars
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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