#!/usr/bin/make -f
# -*- makefile -*-

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

export DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
CFLAGS+=$(CPPFLAGS)
CXXFLAGS+=$(CPPFLAGS)

# out of tree build folder for vmime library
DEB_BUILD_DIR=debian/build
# default install folder
INSTDIR=$(CURDIR)/debian/tmp

DEB_LIBVMIME_CMAKE_OPTS := \
 -DCMAKE_C_COMPILER="$(CC)" \
 -DCMAKE_CXX_COMPILER="$(CXX)" \
 -DCMAKE_CXX_FLAGS="$(CXXFLAGS)" \
 -DCMAKE_SHARED_LINKER_FLAGS_RELEASE="$(LDFLAGS)" \
 -DCMAKE_VERBOSE_MAKEFILE=ON \
 -DCMAKE_INSTALL_PREFIX=/usr \
 -DVMIME_BUILD_SAMPLES=OFF \
 -DVMIME_INSTALL_LIBDIR="/usr/lib/$(DEB_HOST_MULTIARCH)" \
 -DVMIME_TLS_SUPPORT_LIB=gnutls \
 -G "Unix Makefiles" \
 $(CURDIR) \
 $(NULL)

PREPROCESS_FILES := $(wildcard debian/*.in)

$(PREPROCESS_FILES:.in=): %: %.in
	sed 's,/@DEB_HOST_MULTIARCH@,$(DEB_HOST_MULTIARCH:%=/%),g' $< > $@

%:
	dh $@

override_dh_clean:
	rm -rf $(DEB_BUILD_DIR)
	dh_clean

override_dh_auto_configure:
	@echo
	@echo "######################"
	@echo "# configure libvmime #"
	@echo "######################"
	@echo
	# libvmime doesn't have preconfigured cmake options for using dh_auto_configure
	# so we need to call the cmake setup manually.
	mkdir -p $(DEB_BUILD_DIR); \
	cd $(DEB_BUILD_DIR); \
	cmake $(DEB_LIBVMIME_CMAKE_OPTS)

override_dh_auto_build-arch:
	@echo
	@echo "#####################"
	@echo "# building libvmime #"
	@echo "#####################"
	@echo
	dh_auto_build  --sourcedirectory=$(CURDIR) --builddirectory=$(DEB_BUILD_DIR)

override_dh_auto_install-arch: $(PREPROCESS_FILES:.in=)
	@echo
	@echo "#######################"
	@echo "# installing libvmime #"
	@echo "#######################"
	@echo
	dh_auto_install --destdir=$(INSTDIR) --sourcedirectory=$(CURDIR) --builddirectory=$(DEB_BUILD_DIR)
	dh_install

override_dh_auto_build-indep:
	@echo
	@echo "##########################"
	@echo "# building documentation #"
	@echo "##########################"
	@echo
	dh_auto_build --sourcedirectory=$(CURDIR) --builddirectory=$(DEB_BUILD_DIR) -- doc
	# Removing *.md5 files generated by doxygen before dh_install will run
	# as we don't want to ship because they are useless and upstream hasn't a
	# usefull install target for the API documentation before.
	cd $(DEB_BUILD_DIR)/doc/html; \
		find  -type f -name "*.md5" -exec rm {} \;
