#!/usr/bin/make -f

# mozjs' build process does not seem to be compatible with other shells
# like zsh
export SHELL = /bin/sh

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

include /usr/share/dpkg/architecture.mk

PARALLEL = $(or $(subst parallel=,-j,$(filter parallel=%,${DEB_BUILD_OPTIONS})), -j$(shell getconf _NPROCESSORS_ONLN))

%:
	dh $@

override_dh_auto_clean:
	# TODO: building twice in a row will fail, this is insufficient to delete all cruft
	# cleanup cruft from unpacking embedded libraries
	rm -rf libraries/source/cxxtest-4.4/cxxtest-4.4/
	rm -rf libraries/source/fcollada/fcollada-28209/
	rm -rf libraries/source/nvtt/nvtt-28209/
	rm -rf libraries/source/premake-core/premake-core-5.0.0-beta3/
	rm -rf libraries/source/spidermonkey/mozjs-115.16.1/
	rm -rf libraries/source/spirv-reflect/SPIRV-Reflect-vulkan-sdk-1.3.290.0/
	# cleanup all other cruft
	find binaries/system/ -type f ! -name readme.txt -delete
	rm -f build/premake/.*.tmp
	rm -f libraries/source/cxxtest-4.4/python/cxxtest/*.pyc
	rm -rf libraries/source/cxxtest-4.4/python/cxxtest/__pycache__
	rm -f libraries/source/cxxtest-4.4/python/python3/cxxtest/*.pyc
	rm -rf libraries/source/cxxtest-4.4/python/python3/cxxtest/__pycache__
	rm -f source/ps/tests/stub_impl_hack.cpp
	dh_auto_clean

override_dh_auto_build:
	libraries/build-source-libs.sh $(PARALLEL)
	build/workspaces/update-workspaces.sh \
		--bindir=/usr/games \
		--libdir=/usr/lib/games/0ad \
		--datadir=/usr/share/games/0ad \
		$(PARALLEL)
	
	$(MAKE) config=release verbose=1 -C build/workspaces/gcc \
		$(PARALLEL)

override_dh_auto_test:
	# Note: Avoid running tests from root dir of build, otherwise certain
	# tests (i.e. in testsuite MeshManager) may not work as intended and
	# create spurious directories above root dir (../data/mods).
	# Also avoid running tests on non-amd64 due to testsuite flakiness.
ifeq (amd64, $(DEB_HOST_ARCH))
	cd binaries/system/ && LD_LIBRARY_PATH=. ./test
endif

override_dh_auto_install:
	install -Dm 0755 build/resources/0ad.sh $(CURDIR)/debian/tmp/usr/games/0ad
	dh_auto_install

override_dh_makeshlibs:
	dh_makeshlibs -Xusr/lib/games/0ad

override_dh_dwz:
	dh_dwz -Xlibmozjs115-release.so -Xlibmozjs115-debug.so
