#!/usr/bin/make -f

export DEB_PYTHON_INSTALL_LAYOUT=deb_system

include /usr/share/dpkg/architecture.mk
BUILDDIR = $(CURDIR)/debian/build
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
%:
	dh $@ --builddirectory=$(BUILDDIR) --with python3

no_debug_info = mips64el mipsel

ifneq (,$(filter $(DEB_HOST_ARCH),$(no_debug_info)))
  # no debug info to avoid running
  # out of address space when linking
	export DEB_CXXFLAGS_MAINT_APPEND += -g0
endif

# If SALSABUILD is set we want to drop debug info as well
ifneq ($(SALSABUILD),)
  export DEB_CXXFLAGS_MAINT_APPEND += -g0
endif

# Salsa CI's shared runners don't have enough disk space to build this
# package with full debug info (see pipeline 1155026: "No space left on
# device" while installing sumo-gui). Salsa CI sets noautodbgsym in
# DEB_BUILD_OPTIONS by default (SALSA_CI_DISABLE_BUILD_DBGSYM: 1) and,
# unlike a fresh custom variable, that's confirmed to actually reach
# dpkg-buildpackage inside the sbuild chroot, so piggyback on it: if we
# are not producing a dbgsym package anyway, there is no point compiling
# with -g in the first place. Real uploads (buildd, local sbuild/
# dpkg-buildpackage without noautodbgsym) keep full debug info.
ifneq (,$(findstring noautodbgsym,$(DEB_BUILD_OPTIONS)))
  export DEB_CXXFLAGS_MAINT_APPEND += -g0
endif

DPKG_EXPORT_BUILDFLAGS = 1

export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic

include /usr/share/dpkg/buildflags.mk

execute_after_dh_auto_clean:
	# The build system writes binaries and generated Python/SWIG bindings
	# straight into the source tree (CMAKE_RUNTIME_OUTPUT_DIRECTORY and
	# friends point at bin/, tools/libsumo/ and tools/libtraci/ instead of
	# the out-of-tree build directory). dh_auto_clean only cleans
	# $(BUILDDIR), so remove those generated files here too, otherwise a
	# second build/source-package generation sees the source tree as
	# modified (Closes: #1045554).
	find $(CURDIR)/bin -mindepth 1 ! -name start-command-line.bat -delete
	find $(CURDIR)/tools/libsumo -mindepth 1 ! -name __init__.py -delete
	find $(CURDIR)/tools/libtraci -mindepth 1 ! -name __init__.py -delete
	# same story for the compiled gettext catalogs (data/locale/*/LC_MESSAGES/sumo.mo):
	# the whole data/locale tree is generated in-source from data/po/*.po by
	# the i18n cmake target and does not exist at all in a pristine checkout
	rm -rf $(CURDIR)/data/locale
	# and for the doxygen/man targets, which themselves do rm -rf docs/X &&
	# mkdir && regenerate, overwriting the pre-built copies shipped upstream
	rm -rf $(CURDIR)/docs/doxygen
	rm -rf $(CURDIR)/docs/man
	$(RM) $(CURDIR)/doxygen.log

execute_after_dh_auto_build-arch:
	cd $(BUILDDIR) && $(MAKE) man

execute_after_dh_auto_build-indep:
	cd $(BUILDDIR) && $(MAKE) doxygen

execute_after_dh_auto_install:
	find . -type d -name "__pycache__" -exec rm -r {} +
	find $(CURDIR)/debian/tmp/usr/share -type f -name "*.so" -exec rm -r {} +
	rm -rf $(CURDIR)/debian/tmp/usr/bin/start-command-line.bat

	# Remove extra license file
	$(RM) $(CURDIR)/debian/tmp/usr/share/sumo/tools/game/sounds/license.txt

	# Remove embedded copy of leaflet.css, webWizard/index.html is patched
	# to use the system-installed libjs-leaflet package instead
	$(RM) $(CURDIR)/debian/tmp/usr/share/sumo/tools/webWizard/leaflet.css

	# Move JNI libraries
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/jni
	mv $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/liblibsumojni.so \
	   $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/jni/
	mv $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/liblibtracijni.so \
	   $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/jni/

override_dh_auto_test-arch:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	mkdir $(CURDIR)/tests_auto
	cd $(CURDIR)/tests_auto; cp $(CURDIR)/debian/tests/hokkaido/* ./; SUMO_HOME=$(CURDIR) ../bin/sumo -b 0 -e 10000 -n net.net.xml -r input_routes.rou.xml --summary-output sum.out
	cat $(CURDIR)/tests_auto/sum.out
	rm -rf $(CURDIR)/tests_auto
endif

override_dh_auto_test-indep:

execute_after_dh_installman:
	# Remove spare manual page
	$(RM) $(CURDIR)/debian/*/usr/share/man/man1/TraCITestClient.*

execute_after_dh_python3:
	dh_python3 -p sumo-tools --shebang=/usr/bin/python3 /usr/share/sumo/tools
