#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

include /usr/share/dpkg/pkg-info.mk

ifeq ($(DEB_HOST_ARCH),arm64)
	ENABLE_GLES=ON
else
	ENABLE_GLES=OFF
endif

%:
	dh $@ --buildsystem=cmake --builddirectory=build-deb

override_dh_auto_configure:
	if echo "$(DEB_VERSION)" | grep -q '~git' ; then \
		_git_commit=$$(echo "$(DEB_VERSION)" | cut -d'+' -f2 | cut -d'-' -f1) ; \
	else \
		_git_commit="unknown" ; \
	fi ; \
	dh_auto_configure --parallel -- \
		-GNinja					\
		-DCMAKE_BUILD_TYPE=RelWithDebInfo	\
		-DENABLE_FFMPEG=ON			\
		-DENABLE_GLES=$(ENABLE_GLES)		\
		-DENABLE_LIBAVIF=ON			\
		-DENABLE_LTO=ON				\
		-DENABLE_MINIAUDIO=ON			\
		-DENABLE_QT6=ON				\
		-DENABLE_SDL=ON				\
		-DENABLE_SPICE=ON			\
		-DENABLE_TESTS=ON			\
		-DENABLE_TOOLS=ON			\
		-DGIT_COMMIT="$$_git_commit"		\
		-DUSE_ICU=ON -DUSE_MESHOPTIMIZER=ON


override_dh_auto_build:
	ninja -v -C build-deb


override_dh_auto_test:
	cd build-deb && ctest --output-on-failure --parallel 1 --timeout 60 --verbose


override_dh_install:
	install -m 0755 -t debian/tmp/usr/bin build-deb/src/tools/spice2xyzv/spice2xyzv
	find debian/tmp/usr/bin/ -type f ! -name *.pl -exec chrpath --delete {} ';'
	chrpath --delete debian/tmp/usr/lib/*/libcelestia.so.*
	dh_install
	dh_missing --fail-missing
	mv debian/celestia-common/usr/share/celestia/celestia.cfg \
	   debian/celestia-common/etc/celestia.cfg
	ln -s /etc/celestia.cfg debian/celestia-common/usr/share/celestia/
	rm debian/celestia-common/usr/share/celestia/fonts/*
	ln -s /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf \
	      debian/celestia-common/usr/share/celestia/fonts/
	ln -s /usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf \
	      debian/celestia-common/usr/share/celestia/fonts/


override_dh_auto_install:
	env DESTDIR=$(shell pwd)/debian/tmp ninja -v -C build-deb install
