#!/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


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


config.status: configure
	dh_testdir
	# Add here commands to configure the package.
	./configure MRPT_IS_DEB_PACKAGE=1 --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info CFLAGS="$(CFLAGS)" LDFLAGS="-Wl,-z,defs"

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

	# Add here commands to install the package into debian/mrpt.
	./configure MRPT_IS_DEB_PACKAGE=1 --prefix=$(CURDIR)/debian/ CMAKE_MRPT_USE_DEB_POSTFIXS="1"

	# Create mrpt documentation
	make documentation_html
	# Create man pages:
	make man_pages_all
	# 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 . -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
	#export LD_LIBRARY_PATH=$(CURDIR)/debian/libmrpt-base0.X/usr/lib
	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 
