#!/usr/bin/make -f

.PHONY: override_dh_clean override_dh_auto_configure override_dh_auto_build override_dh_install override_dh_strip override_dh_installchangelogs override_dh_builddeb

LDFLAGS += -Wl,-z,defs -Wl,--as-needed

# uncomment line to turn verbose mode on
#export V=1


%:
	dh $@ --parallel

override_dh_clean:
	[ ! -f build-config ] || rake clean:dist

	dh_clean config.status config.log \
	rake.d/dependecy.d/*.rb src/input/*.d config.h build-config

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	rm -rf lib/gtest
endif

override_dh_auto_configure:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	mkdir -p lib/gtest
	cp -R /usr/include/gtest lib/gtest/
	mv lib/gtest/gtest lib/gtest/include
	cp -R /usr/src/gtest/src lib/gtest/
endif

# gcc 10 in Debian 11 Bullseye contains a bug that causes an
# "internal compiler error" segfault. Workaround: compile with
# clang.
	if grep -qr bullseye /etc/apt/sources.list*; then \
	  dh_auto_configure -- CC=/usr/bin/clang CXX=/usr/bin/clang++ \
	    --docdir='$${datarootdir}/doc/mkvtoolnix-gui' \
	    --enable-debug --enable-optimization ; \
	else \
	  dh_auto_configure -- --docdir='$${datarootdir}/doc/mkvtoolnix-gui' \
	    --enable-debug --enable-optimization ; \
	fi

override_dh_auto_build:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	LC_ALL=C rake tests:run_unit
endif

	rake

override_dh_install:
	rake install DESTDIR=$(CURDIR)/debian/tmp

	dh_install -Xorg.bunkus.mkvtoolnix-gui.xml
	dh_missing --fail-missing -Xorg.bunkus.mkvtoolnix-gui.xml

override_dh_strip:
	dh_strip -pmkvtoolnix     --dbg-package=mkvtoolnix-dbg
	dh_strip -pmkvtoolnix-gui --dbg-package=mkvtoolnix-gui-dbg

override_dh_installchangelogs:
	dh_installchangelogs NEWS.md

override_dh_builddeb:
	case $(shell uname -m) in \
		i*86) dh_builddeb -- -Zxz     ;; \
		*)    dh_builddeb -- -Zxz -z9 ;; \
	esac
