#!/usr/bin/make -f
# -*- makefile -*-
# Makefile for MRPT Debian package.

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

include /usr/share/dpkg/architecture.mk
ifneq (,$(filter armhf powerpc ppc64el,$(DEB_HOST_ARCH)))
DPKG_EXPORT_BUILDFLAGS    = yes
DEB_CFLAGS_MAINT_APPEND   = -O1
DEB_CXXFLAGS_MAINT_APPEND = -O1
DEB_CFLAGS_MAINT_STRIP    = -O2 -O3
DEB_CXXFLAGS_MAINT_STRIP  = -O2 -O3
CMAKE_BUILD_TYPE=None
else
CMAKE_BUILD_TYPE=Release
endif
include /usr/share/dpkg/default.mk

# 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)

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

config.status: CMakeLists.txt
	dh_testdir
	# Add here commands to configure the package and install it into debian/mrpt-*
	cmake . \
		-DCMAKE_HOST=$(DEB_HOST_GNU_TYPE) \
		-DCMAKE_BUILD=$(DEB_BUILD_GNU_TYPE) \
		-DCMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE)  \
		-DCMAKE_INSTALL_PREFIX=$(CURDIR)/debian/ \
		-DCMAKE_CONFIGURE_LDFLAGS="$(LDFLAGS)" \
		-DCMAKE_MRPT_USE_DEB_POSTFIXS="1" \
		-DCMAKE_MRPT_IS_RPM_PACKAGE="0" \
		-DCMAKE_C_FLAGS="$(CFLAGS) $(CPPFLAGS)" \
		-DCMAKE_CXX_FLAGS="$(CXXFLAGS) $(CPPFLAGS)" \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		 

build: build-arch build-indep

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

build-stamp:  config.status
	dh_testdir

	touch $@

distclean:
    # Nothing to do (do not remove this line!)

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	# Add here commands to clean up after the build process.
	rm -f config.sub config.guess

	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	# Create mrpt documentation
	VERBOSE=1 make documentation_html
	VERBOSE=1 make documentation_performance_html
	# Create man pages:
	make man_pages_all
	# Compile manual(s):
	make documentation_psgz_guides || echo "*** Ignoring failed manual build! ***"
	# Build all
	$(MAKE) VERBOSE=1
	# Run tests
	make test || echo "*** Ignoring failed tests! ***"

	# Install
	$(MAKE) install
	
	# Move all the ".so" files to the proper place: the -dev package:
	mkdir -p $(CURDIR)/debian/libmrpt-dev/usr/lib/
	find $(CURDIR)/debian/ -name "*.so" | grep -v 'libmrpt-dev' | xargs -I FIL mv -f FIL $(CURDIR)/debian/libmrpt-dev/usr/lib/

	# DEBUG PACKAGES -------------	
	#./configure MRPT_IS_DEB_PACKAGE=1 --prefix=$(CURDIR)/debian/ CMAKE_MRPT_USE_DEB_POSTFIXS="1" --buildtype=Debug
	# Build libs only
	cmake . -DCMAKE_BUILD_TYPE=Debug -DBUILD_APPLICATIONS=OFF
	$(MAKE) VERBOSE=1 

	# Install
	$(MAKE) install

	# Move all the "-dbg.so" files to the proper place: the -dev package:
	mkdir -p $(CURDIR)/debian/libmrpt-dbg/usr/lib/
	find $(CURDIR)/debian/ -name "*-dbg.so*" | grep -v 'libmrpt-dev' | grep -v 'libmrpt-dbg' | xargs -I FIL mv -f FIL $(CURDIR)/debian/libmrpt-dbg/usr/lib/

	# Create Lintian overrides:
	mkdir -p $(CURDIR)/debian/libmrpt-dbg/usr/share/lintian/overrides/
	echo "libmrpt-dbg: non-dev-pkg-with-shlib-symlink" > $(CURDIR)/debian/libmrpt-dbg/usr/share/lintian/overrides/libmrpt-dbg
	echo "libmrpt-dbg: package-name-doesnt-match-sonames" >> $(CURDIR)/debian/libmrpt-dbg/usr/share/lintian/overrides/libmrpt-dbg

	
# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installmenu
	dh_installmime -p mrpt-core
	dh_installman -p mrpt-apps $(CURDIR)/man-pages/*.1*
	# Register .desktop files with a MIME type:
#	dh_desktop -p mrpt-apps
	dh_link
	#dh_strip -plibmrpt-dbg --dbg-package=libmrpt-dbg
	dh_strip --exclude=-dbg
	dh_compress
	dh_fixperms
	dh_makeshlibs -a
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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