#!/usr/bin/make -f

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

DPKG_EXPORT_BUILDFLAGS = 1

export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic

include /usr/share/dpkg/buildflags.mk

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 executable bit
	chmod 644 $(CURDIR)/debian/tmp/usr/share/sumo/tools/contributed/sumopy/coremodules/demand/demandbase.py \
	          $(CURDIR)/debian/tmp/usr/share/sumo/tools/contributed/sumopy/coremodules/network/networktools.py \
	          $(CURDIR)/debian/tmp/usr/share/sumo/tools/contributed/sumopy/plugins/mapmatching/mapmatching.py

	# 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
