#!/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)
DEB_HOST_ARCH  ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
CXXFLAGS += -D__STDC_CONSTANT_MACROS
ifeq ($(DEB_HOST_ARCH),armel)
  CFLAGS += -Os
  CXXFLAGS += -Os
endif

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)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="-Wl,-z,defs"

build: 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" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="-Wl,-z,defs"

	# Create mrpt documentation
	make documentation_html
	# Create man pages:
	make man_pages_all
	# Build all
	$(MAKE) VERBOSE=1
	# Run tests
	make test || echo "\n\n**************\n* WARNING: Some unit tests failed: this may be a real error or an artifact if this is building in some virtualization platform.\n*****************\n\n"

	# Install
	$(MAKE) install
	
	# Move all the ".so" files to the proper place: the -dev package:
	mkdir -p $(CURDIR)/debian/libmrpt-dev/usr/lib/
	# 1: copy (-P: don't dereference symbolic link)
	find $(CURDIR)/debian/ -name "*.so" | xargs -I FIL cp -P FIL $(CURDIR)/debian/libmrpt-dev/usr/lib/
	# 2: remove from the old location (this is to avoid "mv" to complaing if moving to the same location)
	find $(CURDIR)/debian/ -name "*.so" | grep -v 'libmrpt-dev' | xargs rm 
	
# 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 --dbg-package=libmrpt-core-dbg
	dh_strip
	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 
