#!/usr/bin/make -f

# BEGIN ENVIRONMENT SETUP
# https://www.debian.org/doc/debian-policy/ch-source.html
# nocheck: This tag says to not run any build-time test suite provided by the package.
export DEB_BUILD_OPTIONS=nocheck

# Setup of $(arch), $(rockruby_libdir) and $(rockruby_archdir)
arch=$(shell gcc -print-multiarch)
ruby_ver=$(shell ruby --version)
ruby_arch_dir=$(shell ruby -r rbconfig -e "print RbConfig::CONFIG['archdir']")
ruby_libdir=$(shell ruby -r rbconfig -e "print RbConfig::CONFIG['rubylibdir']")
rockruby_archdir=$(subst /usr,,$(ruby_arch_dir))
rockruby_libdir=$(subst /usr,,$(ruby_libdir))
# Setup of paths: PATH,PKG_CONFIG_PATH,RUBYLIB,...
master1906_install_dir = /opt/rock/master-19.06
env_setup =  PATH=$(master1906_install_dir)/bin:$(PATH)
env_setup += RUBYLIB=$(master1906_install_dir)/$(rockruby_libdir):$(master1906_install_dir)/$(rockruby_archdir):$(master1906_install_dir)/lib/ruby/vendor_ruby/standard:$(master1906_install_dir)/lib/ruby/vendor_ruby/core:$(master1906_install_dir)/lib/ruby/vendor_ruby:$(RUBYLIB)
env_setup += PKG_CONFIG_PATH=$(master1906_install_dir)/lib/pkgconfig:$(master1906_install_dir)/lib/$(arch)/pkgconfig:/usr/share/pkgconfig:/usr/lib/$(arch)/pkgconfig:$(PKG_CONFIG_PATH)
env_setup += Rock_DIR=$(master1906_install_dir)/share/rock/cmake:$(Rock_DIR)
env_setup += LD_LIBRARY_PATH=$(master1906_install_dir)/lib:$(master1906_install_dir)/lib/$(arch):$(LD_LIBRARY_PATH)
env_setup += CMAKE_PREFIX_PATH=$(master1906_install_dir):$(CMAKE_PREFIX_PATH)
env_setup += OROGEN_PLUGIN_PATH=$(master1906_install_dir)/share/orogen/plugins:$(OROGEN_PLUGIN_PATH)
export TYPELIB_CXX_LOADER=castxml
export DEB_CPPFLAGS_APPEND=-std=c++11
export npm_config_cache=/tmp/npm
rock_library_dirs=$(master1906_install_dir)/lib:$(master1906_install_dir)/lib/$(arch):
rock_install_dir=/opt/rock/master-19.06
# Check if a python command is available and identify the version
# and identify the setup.py file
python_cmd=$(shell command -v python 2> /dev/null)
ifneq ("$(python_cmd)","")
python_version=$(shell python -c 'import sys; print "{0}.{1}".format(sys.version_info[0],sys.version_info[1])')
# Get dirname a setup.py match
python_setup_dir=$(shell find $(DEB_SRCDIR) -name setup.py | tail -n 1 | xargs dirname)
endif

DEB_VERBOSE_ALL=1
include /usr/share/cdbs/1/rules/debhelper.mk
extra_lib_dir=$(shell pwd)/debian/rock-master-19.06-tools-configmaps$(rock_install_dir)/lib
# This argument list is forwarded to dh_shlibdeps which takes the -l option and a colon separated list of libraries
# (not that this is different from the linker arguments)
DEB_DH_SHLIBDEPS_ARGS_ALL=-l$(extra_lib_dir):$(extra_lib_dir)/$(arch):$(rock_library_dirs)

rock_doc_install_dir=$(rock_install_dir)/share/doc/rock-master-19.06-tools-configmaps

pre-build::
	echo "Detected:"
	echo "   ruby version: $(ruby_ver)"
	echo "   ruby libdir: $(rockruby_libdir)"
	echo "   ruby archdir: $(rockruby_archdir)"

	#echo "Current directory:"
	#find .

# END ENVIRONMENT SETUP
######################################
# BEGIN OROGEN -- This block needs to be executed before CMAKE


# END OROGEN
######################################
# BEGIN CMAKE
#
# Defining the cmake command and setting the required enviroment variables
# PKG_CONFIG_PATH Resolve Rock package via pkg-config
# Rock_DIR Set to make sure find_package(Rock) does work
# ROCK_CMAKE_MACROS Used in some patches to set the CMAKE_MODULE_PATH, allowing to find Rock.cmake
CMAKE = $(env_setup) cmake
DEB_CMAKE_INSTALL_PREFIX = $(rock_install_dir)

# Debian packaging discourages the use of rpath, but
# cdbs actively sets CMAKE_SKIP_RPATH=ON, so reinforcing OFF here
DEB_CMAKE_EXTRA_FLAGS = -DCMAKE_SKIP_RPATH=OFF
# Telling FindGEM.cmake to search for os packages using pkg-config and not for gems
DEB_CMAKE_EXTRA_FLAGS += -DGEM_OS_PKG=TRUE
# Leaving RUBY_EXECUTABLE unset since it maps to a path of the user executing the upload script
DEB_CMAKE_EXTRA_FLAGS +=  -DCMAKE_EXPORT_COMPILE_COMMANDS="ON"  -DROCK_USE_CXX11="ON"
# Disable ROCK Testing -- since running test on the OBS would require proper setting of path, etc. (see. data_processing/type_to_vector)
DEB_CMAKE_EXTRA_FLAGS += -DROCK_TEST_ENABLED=OFF -UROCK_TEST_LOG_DIR
include /usr/share/cdbs/1/class/cmake.mk

# END CMAKE
##########################################
# BEGIN AUTOTOOLS
#


# http://cdbs-doc.duckcorp.org/en/cdbs-doc.xhtml see post-configure actions
# replace all entries of -I with -I and -isystem in order to 
#   (a) guarantee that pkgconfig can still knowns the include directory (-I)
#   (b) the include directory is actually accounted for as system include directory (-isystem)
# Account for the fact, that configure is called multiple times e.g. revert changes first, then reapply
configure/rock-master-19.06-tools-configmaps::
	echo "Adapt -I to -isystem in pkgconfig files:"
	find . -name '[a-zA-Z]*.pc' -exec sh -c "echo Adapting {}; sed -i 's#\(\"\?\)-isystem[^ ]\+\1##g; s#\(\"\?\)-I\([^ ]\+\)\1#\1-I\2\1 \1-isystem\2\1#g; s#[ ]\+# #g' {}; cat {};" \;

###########################################
# DOCS


# Build docs
# This checks for Doxyfile and runs doxygen with that.
# Otherwise, it tries the "doc" target.
# Since docs are not critical, ignore failures('-' on recipe line start).
# The make call is supposed to be invoked even if -t or -n is selected,
# with the appropriate flags('+' on recipe line start)

common-build-arch common-build-indep:: debian/stamp-rock-doc
debian/stamp-rock-doc:
	-+$(DEB_MAKE_INVOKE) doc
	touch $@

common-install-arch common-install-indep:: common-install-rock-doc install-extensions
common-install-rock-doc::
	mkdir -p $(DEB_DESTDIR)$(rock_doc_install_dir)
	$(if $(wildcard $(DEB_SRCDIR)/doc/html),-cp -r "$(DEB_SRCDIR)/doc/html" "$(DEB_DESTDIR)$(rock_doc_install_dir)",$(if $(wildcard $(DEB_SRCDIR)/doc),-cp -r "$(DEB_SRCDIR)/doc" "$(DEB_DESTDIR)$(rock_doc_install_dir)"))
	$(if $(wildcard $(DEB_BUILDDIR)/doc/html),-cp -r "$(DEB_BUILDDIR)/doc/html" "$(DEB_DESTDIR)$(rock_doc_install_dir)",$(if $(wildcard $(DEB_BUILDDIR)/doc),-cp -r "$(DEB_BUILDDIR)/doc" "$(DEB_DESTDIR)$(rock_doc_install_dir)"))

install-extensions:: install-python
install-python::
ifneq ("$(python_setup_dir)","")
	echo "Installing python extension in $(python_setup_dir)"
	mkdir -p $(DEB_DESTDIR)$(rock_install_dir)/lib/python$(python_version)/site-packages
	cd $(python_setup_dir); $(python_cmd) setup.py build; PYTHONPATH=$(DEB_DESTDIR)$(rock_install_dir)/lib/python$(python_version)/site-packages/ $(python_cmd) setup.py install --prefix=$(DEB_DESTDIR)$(rock_install_dir); cd -
endif

clean::
	rm -f debian/stamp-rock-doc
	rm -rf $(DEB_SRCDIR)/doc/html


# END DOCS
###########################################
