#!/usr/bin/make -f

#export DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all reproducible=-timeless
export DEB_CPPFLAGS_MAINT_APPEND = -DNDEBUG -Wno-error=alloc-zero -Wno-error=uninitialized

# workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81358
ifneq (,$(filter $(DEB_HOST_ARCH), armel m68k mipsel powerpc riscv64 sh4))
    export DEB_LDFLAGS_MAINT_APPEND = -Wl,--no-as-needed -latomic -Wl,--as-needed
endif

%:
	dh $@ -Scmake+ninja -Bdebian/build

override_dh_auto_configure:
	# Remove embedded code copies so we never use them
	rm -rf 3rdparty/miniupnpc
	# Remove generated files so we regenerate them
	rm -f doc/warzone2100.6
	dh_auto_configure -- -DCMAKE_BUILD_TYPE=RelWithDebInfo

override_dh_auto_build:
	(cd po ; ./update-po.sh ; cp -r custom ../debian/build/po )
	cmake --build debian/build
	# The HTML to PDF converter needs an X server
	# The default bit depth is too low for the images in the PDF
	xvfb-run --auto-servernum --server-args="-screen 0 1024x768x24" dh_auto_build
	icotool -x icons/warzone2100.ico
	rm -f warzone2100_*_*x*x*.png

override_dh_auto_install:
	cmake --install debian/build --prefix debian/tmp/usr
	mkdir debian/tmp/usr/share/games
	mv debian/tmp/usr/share/warzone2100 debian/tmp/usr/share/games/warzone2100
	mkdir -p debian/tmp/usr/games
	mv debian/tmp/usr/bin/warzone2100 debian/tmp/usr/games
	rm -rf debian/tmp/usr/bin
	mv debian/tmp/usr/share/metainfo debian/tmp/usr/share/appdata
	rm -f debian/*/usr/share/doc/warzone2100/COPYING*
	rm -f debian/*/usr/share/doc/warzone2100/ChangeLog*
	rm -f debian/*/usr/share/doc/warzone2100/Readme*
	rm -f debian/tmp/usr/share/doc/warzone2100/copyright

override_dh_compress:
	dh_compress -X.pdf

override_dh_auto_test:
	# Do nothing here at the moment to work around a FTBFS

override_dh_clean:
	rm -rf 3rdparty/discord-rpc/thirdparty/rapidjson
	rm -rf debian/build po/custom/fromJson.txt debian/warzone2100 debian/warzone2100-data debian/tmp debian/warzone2100-music debian/.debhelper
