#!/usr/bin/make -f

export QT_SELECT=qt5
DEB_TARGET_GNU_TYPE := $(shell dpkg-architecture -qDEB_TARGET_GNU_TYPE)
DEB_HOST_GNU_TYPE := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# DEB_BUILD_GNU_TYPE is the system running the build (this system)
# DEB_HOST_GNU_TYPE is the system jamulus is being built for (the target)
# DEB_TARGET_GNU_TYPE is only relevant when building cross-compilers
$(info DEB_BUILD_GNU_TYPE=$(DEB_BUILD_GNU_TYPE) DEB_HOST_GNU_TYPE=$(DEB_HOST_GNU_TYPE) DEB_TARGET_GNU_TYPE=$(DEB_TARGET_GNU_TYPE))

ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	QMAKE := qmake
else
	# For an unknown reason, when called from this makefile in Ubuntu 20.04,
	# qmake needs an option between qmake and -qtconf, else it gives an error.
	# We use -makefile, which is the default mode anyway, and this avoids the issue.
	QMAKE := qmake -makefile -qtconf "/usr/lib/$(DEB_HOST_GNU_TYPE)/qt5/qt.conf" -spec "/usr/lib/$(DEB_HOST_GNU_TYPE)/qt5/mkspecs/$(DEB_HOST_GNU_TYPE)-g++" LIBS+="-lstdc++ -lm"
endif

%:
	dh $@

override_dh_update_autotools_config:
	# dh_update_autotools_config replaces libs/opus/config.{sub,guess}.
	# This is unnecessary as we don't build opus via autotools at all
	# (we use qmake). In addition, this would cause our -dev version generation
	# logic to mark Debian builds as -dirty by default.
	# Therefore, disable this behavior:
	:

override_dh_auto_configure:
	mkdir -p build-gui && cd build-gui && $(QMAKE) "CONFIG+=noupcasename" PREFIX=/usr ../Jamulus.pro
	mkdir -p build-nox && cd build-nox && $(QMAKE) "CONFIG+=headless serveronly" TARGET=jamulus-headless PREFIX=/usr ../Jamulus.pro

override_dh_auto_build:
	cd build-gui && make -j "$$(nproc)"
	cd build-nox && make -j "$$(nproc)"

override_dh_auto_install:
	cd build-nox && make install INSTALL_ROOT=../debian/tmp
	cd build-gui && make install INSTALL_ROOT=../debian/tmp

override_dh_auto_clean:
	rm -rf build-gui
	rm -rf build-nox
	dh_clean
