#!/usr/bin/make -f
export DH_VERBOSE = 1
NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))

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

# Link with libatomic on archs where it is needed.
# See <URL: https://bugs.debian.org/1019868 >
ifeq ($(DEB_HOST_ARCH),$(filter $(DEB_HOST_ARCH),armel riscv64))
       export DEB_LDFLAGS_MAINT_APPEND += -Wl,--no-as-needed -latomic -Wl,--as-needed
endif

# replace "custom_build" with more useful Debian version info
include /usr/share/dpkg/pkg-info.mk  # provides DEB_VERSION
export BUILD_NAME = debian${DEB_VERSION}

# clipper2, msdfgen, rvo2, and xatlas are not available within Debian.
# glslang-dev only ships static libraries. (See #909951)
# openxr currently requires a vendored libjsoncpp.
SCONS_OPTIONS = platform=linuxbsd \
	--jobs=$(NUMJOBS) \
	import_env_vars=CCACHE_DIR,CCACHE_MAXSIZE \
	CC="$(CC)" \
	CXX="$(CXX)" \
	ccflags="$(CPPFLAGS)" \
	cflags="$(CFLAGS)" \
	cxxflags="$(CXXFLAGS)" \
	linkflags="$(LDFLAGS)" \
	builtin_brotli=no \
	builtin_certs=no \
	system_certs_path=/etc/ssl/certs/ca-certificates.crt \
	builtin_clipper2=yes \
	builtin_embree=no \
	builtin_enet=no \
	builtin_freetype=no \
	builtin_msdfgen=yes \
	builtin_glslang=yes \
	builtin_graphite=no \
	builtin_harfbuzz=no \
	builtin_icu4c=no \
	static_icu_data=no \
	builtin_libogg=no \
	builtin_libpng=no \
	builtin_libtheora=no \
	builtin_libvorbis=no \
	builtin_libwebp=no \
	builtin_wslay=no \
	builtin_mbedtls=no \
	builtin_miniupnpc=no \
	openxr=no \
	builtin_pcre2=no \
	builtin_recastnavigation=no \
	builtin_rvo2_2d=yes \
	builtin_rvo2_3d=yes \
	builtin_xatlas=yes \
	builtin_zlib=no \
	builtin_zstd=no \
	use_static_cpp=no \
	tests=yes \
	engine_update_check=no \
	verbose=yes

# disable debug symbols if noautodbgsym in DEB_BUILD_OPTIONS
ifneq (,$(findstring noautodbgsym, $(DEB_BUILD_OPTIONS)))
	SCONS_OPTIONS += debug_symbols=no
else
	SCONS_OPTIONS += debug_symbols=yes
endif

%:
	dh $@

override_dh_clean:
	scons $(SCONS_OPTIONS) -c target=editor output_path=bin/godot.editor
	scons $(SCONS_OPTIONS) -c target=template_debug output_path=bin/godot.template-debug
	scons $(SCONS_OPTIONS) -c target=template_release production=yes output_path=bin/godot.template-release
	dh_clean

override_dh_auto_build:
	dh_auto_build
	# build requires thirdparty certs path to be present even if disabled
	install -D /dev/null thirdparty/certs/ca-certificates.crt
	scons $(SCONS_OPTIONS) target=editor output_path=bin/godot.editor
	scons $(SCONS_OPTIONS) target=template_debug output_path=bin/godot.template-debug
	scons $(SCONS_OPTIONS) target=template_release production=yes output_path=bin/godot.template-release

override_dh_auto_install:
	install -D -m 400 $(CURDIR)/bin/godot.editor $(CURDIR)/debian/godot/usr/bin/godot
	install -D -m 400 $(CURDIR)/bin/godot.template-debug $(CURDIR)/debian/godot-template-debug/usr/bin/godot-template-debug
	install -D -m 400 $(CURDIR)/bin/godot.template-release $(CURDIR)/debian/godot-template-release/usr/bin/godot-template-release
	install -D -m 444 $(CURDIR)/misc/dist/linux/org.godotengine.Godot.desktop $(CURDIR)/debian/godot/usr/share/applications/org.godotengine.Godot.desktop
	install -D -m 444 $(CURDIR)/misc/dist/linux/org.godotengine.Godot.appdata.xml $(CURDIR)/debian/godot/usr/share/metainfo/org.godotengine.Godot.metainfo.xml
	install -D -m 444 $(CURDIR)/misc/dist/linux/org.godotengine.Godot.xml $(CURDIR)/debian/godot/usr/share/mime/packages/org.godotengine.Godot.xml
	install -D -m 644 $(CURDIR)/icon.svg $(CURDIR)/debian/godot/usr/share/icons/hicolor/scalable/apps/godot.svg
	install -D -m 444 $(CURDIR)/misc/dist/linux/godot.6 $(CURDIR)/debian/godot/usr/share/man/man6/godot.6
	install -D -m 444 $(CURDIR)/misc/dist/linux/godot.6 $(CURDIR)/debian/godot-template-debug/usr/share/man/man6/godot-template-debug.6
	install -D -m 444 $(CURDIR)/misc/dist/linux/godot.6 $(CURDIR)/debian/godot-template-release/usr/share/man/man6/godot-template-release.6
	dh_auto_install

override_dh_dwz:
	# dwz fails with standard DIE limit on godot editor
	dh_dwz -- --max-die-limit none
