#!/usr/bin/make -f
# -*- makefile -*-
# Debian rules for building ros-<distro>-edgefirst-msgs packages.
# Supports multiple ROS2 distributions via ROS_DISTRO variable.

# Extract version from package.xml
VERSION := $(shell grep '<version>' package.xml | sed 's/.*<version>\(.*\)<\/version>.*/\1/')
BUILD_NUMBER ?= 0
ROS_DISTRO ?= humble

# Repository root (for changelog generation)
REPO_ROOT := $(shell cd .. && pwd)

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1
# TODO: remove the LDFLAGS override.  It's here to avoid esoteric problems
# of this sort:
#  https://code.ros.org/trac/ros/ticket/2977
#  https://code.ros.org/trac/ros/ticket/3842
export LDFLAGS=
export PKG_CONFIG_PATH=/opt/ros/$(ROS_DISTRO)/lib/pkgconfig
# Explicitly enable -DNDEBUG, see:
# 	https://github.com/ros-infrastructure/bloom/issues/327
export DEB_CXXFLAGS_MAINT_APPEND=-DNDEBUG
ifneq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
	BUILD_TESTING_ARG=-DBUILD_TESTING=OFF
endif

DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

%:
	dh $@ -v --buildsystem=cmake --builddirectory=.obj-$(DEB_HOST_GNU_TYPE)

override_dh_auto_configure:
	# In case we're installing to a non-standard location, look for a setup.sh
	# in the install tree and source it.  It will set things like
	# CMAKE_PREFIX_PATH, PKG_CONFIG_PATH, and PYTHONPATH.
	if [ -f "/opt/ros/$(ROS_DISTRO)/setup.sh" ]; then . "/opt/ros/$(ROS_DISTRO)/setup.sh"; fi && \
	dh_auto_configure -- \
		-DCMAKE_INSTALL_PREFIX="/opt/ros/$(ROS_DISTRO)" \
		-DAMENT_PREFIX_PATH="/opt/ros/$(ROS_DISTRO)" \
		-DCMAKE_PREFIX_PATH="/opt/ros/$(ROS_DISTRO)" \
		$(BUILD_TESTING_ARG)

override_dh_auto_build:
	# In case we're installing to a non-standard location, look for a setup.sh
	# in the install tree and source it.  It will set things like
	# CMAKE_PREFIX_PATH, PKG_CONFIG_PATH, and PYTHONPATH.
	if [ -f "/opt/ros/$(ROS_DISTRO)/setup.sh" ]; then . "/opt/ros/$(ROS_DISTRO)/setup.sh"; fi && \
	dh_auto_build

override_dh_auto_test:
	# In case we're installing to a non-standard location, look for a setup.sh
	# in the install tree and source it.  It will set things like
	# CMAKE_PREFIX_PATH, PKG_CONFIG_PATH, and PYTHONPATH.
	# echo -- Running tests. Even if one of them fails the build is not canceled.
	# if [ -f "/opt/ros/$(ROS_DISTRO)/setup.sh" ]; then . "/opt/ros/$(ROS_DISTRO)/setup.sh"; fi && \
	# dh_auto_test || true

override_dh_shlibdeps:
	# In case we're installing to a non-standard location, look for a setup.sh
	# in the install tree and source it.  It will set things like
	# CMAKE_PREFIX_PATH, PKG_CONFIG_PATH, and PYTHONPATH.
	if [ -f "/opt/ros/$(ROS_DISTRO)/setup.sh" ]; then . "/opt/ros/$(ROS_DISTRO)/setup.sh"; fi && \
	dh_shlibdeps -l$(CURDIR)/debian/ros-$(ROS_DISTRO)-edgefirst-msgs//opt/ros/$(ROS_DISTRO)/lib/:$(CURDIR)/debian/ros-$(ROS_DISTRO)-edgefirst-msgs//opt/ros/$(ROS_DISTRO)/opt/edgefirst_msgs/lib/

override_dh_auto_install:
	# In case we're installing to a non-standard location, look for a setup.sh
	# in the install tree and source it.  It will set things like
	# CMAKE_PREFIX_PATH, PKG_CONFIG_PATH, and PYTHONPATH.
	if [ -f "/opt/ros/$(ROS_DISTRO)/setup.sh" ]; then . "/opt/ros/$(ROS_DISTRO)/setup.sh"; fi && \
	dh_auto_install

# Generate debian/control from template and debian/changelog from CHANGELOG.md
version:
	@echo "Generating debian/control for ROS_DISTRO=$(ROS_DISTRO)"
	sed 's/@ROS_DISTRO@/$(ROS_DISTRO)/g' debian/control.in > debian/control
	@echo "Generating debian/changelog for version $(VERSION)-$(BUILD_NUMBER)"
	$(REPO_ROOT)/scripts/generate-debian-changelog.sh $(ROS_DISTRO) $(VERSION) $(BUILD_NUMBER) > debian/changelog
