#!/usr/bin/make -f
export DH_VERBOSE = 1
NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
BITS = $(shell dpkg-architecture -q DEB_TARGET_ARCH_BITS)

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

# The denoise module depends on OIDN which is x86_64 only (in the
# vendored version).
# Godot's own logic to disable it on other arches is a bit brittle
# when it comes to cross-compiling currently.
# See https://github.com/godotengine/godot/issues/47344
ifneq ($(DEB_HOST_ARCH),$(filter $(DEB_HOST_ARCH),amd64 i386 armel armhf))
       DISABLE_MODULES = module_denoise_enabled=no
endif

override_dh_clean:
	dh_clean
	scons -c
	find . -iname "*.gen.cpp" -delete
	find . -iname "*.gen.h" -delete
	find . -iname "*.pyc" -delete
	find . -iname ".a" -delete
	find . -iname ".o" -delete
	find . -iname ".scons_env.json" -delete
	find . -iname ".sconsign.dblite" -delete
	find . -name '*.o' -delete
	rm -rf .sconf_temp/
	rm -rf main/libmain.x11.opt.tools.64.a main/tests/libtests.x11.opt.tools.64.a
	rm -rf config.log .sconsign?.dblite
	rm -rf bin/
	rm -rf enet
	rm -rf freetype
	rm -rf libogg
	rm -rf libpng
	rm -rf libtheora
	rm -rf libvorbis
	rm -rf libvpx
	rm -rf libwebp
	rm -rf openssl
	rm -rf opus
	rm -rf pcre2
	rm -rf squish
	rm -rf zlib

include /usr/share/dpkg/architecture.mk  # provides DEB_HOST_ARCH_BITS

# bullet requires 2.88

SCONS_OPTIONS = bits=$(DEB_HOST_ARCH_BITS) \
	system_certs_path=/etc/ssl/certs/ca-certificates.crt \
	builtin_bullet=yes \
	builtin_enet=no \
	builtin_freetype=no \
	builtin_libogg=no \
	builtin_libpng=no \
	builtin_libtheora=no \
	builtin_libvorbis=no \
	builtin_libvpx=no \
	builtin_libwebp=no \
	builtin_openssl=no \
	builtin_opus=no \
	builtin_pcre2=no \
	builtin_squish=no \
	builtin_zlib=no \
	builtin_zstd=yes

%:
	dh $@

override_dh_auto_build:
	dh_auto_build
	scons platform=server tools=yes target=release_debug $(SCONS_OPTIONS) -j $(NUMJOBS) CCFLAGS="$(CXXFLAGS)" LINKFLAGS="$(LDFLAGS)" CFLAGS="$(CFLAGS)" $(DISABLE_MODULES)
	scons platform=x11    tools=no  target=release       $(SCONS_OPTIONS) -j $(NUMJOBS) CCFLAGS="$(CXXFLAGS)" LINKFLAGS="$(LDFLAGS)" CFLAGS="$(CFLAGS)" $(DISABLE_MODULES)
	scons platform=x11    tools=yes target=release_debug $(SCONS_OPTIONS) -j $(NUMJOBS) CCFLAGS="$(CXXFLAGS)" LINKFLAGS="$(LDFLAGS)" CFLAGS="$(CFLAGS)" $(DISABLE_MODULES)

override_dh_auto_install:
	echo "Installing binaries for $(BITS) bits architecture"
	install -D -m 400 $(CURDIR)/bin/godot.x11.opt.tools.$(BITS)           $(CURDIR)/debian/godot3/usr/bin/godot3
	install -D -m 400 $(CURDIR)/bin/godot.x11.opt.$(BITS)                 $(CURDIR)/debian/godot3-runner/usr/bin/godot3-runner
	install -D -m 400 $(CURDIR)/bin/godot_server.x11.opt.tools.$(BITS) $(CURDIR)/debian/godot3-server/usr/bin/godot3-server
	install -D -m 444 $(CURDIR)/misc/dist/linux/org.godotengine.Godot.desktop  $(CURDIR)/debian/godot3/usr/share/applications/org.godotengine.Godot.desktop
	install -D -m 444 $(CURDIR)/misc/dist/linux/org.godotengine.Godot.appdata.xml $(CURDIR)/debian/godot3/usr/share/metainfo/org.godotengine.Godot.xml
	install -D -m 444 $(CURDIR)/misc/dist/linux/org.godotengine.Godot.xml $(CURDIR)/debian/godot3/usr/share/mime/packages/org.godotengine.Godot.xml
	install -D -m 644 $(CURDIR)/icon.svg                                       $(CURDIR)/debian/godot3/usr/share/pixmaps/godot.svg
	sed -i 's/^Exec=.*/Exec=godot3 -p %f/' $(CURDIR)/debian/godot3/usr/share/applications/org.godotengine.Godot.desktop
	dh_auto_install
