#!/usr/bin/make -f
%:
	dh $@
ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
VENDOR ?= $(shell dpkg-vendor --query vendor)
export MSC_TEST_NOFONT = 1
export MSC_TEST_DUMP_DIFF = 3
# for dpkg-vendor, see https://bugs.debian.org/1076211#49
ifneq ($(VENDOR)$(ARCH),Debianamd64)
export MSC_TEST_ALLOWFAIL = 1
export PNGDIFF_NOFONT_MASK_RADIUS = 3
endif
export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow
export DEB_CPPFLAGS_MAINT_APPEND = -DNDEBUG
BUILD_DIR = $(CURDIR)/debian/build-gui
INST_DIR = $(CURDIR)/debian/tmp/gui
BUILD_NOX = $(CURDIR)/debian/build-nox
INST_NOX = $(CURDIR)/debian/tmp/nox
ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
export DEB_CXXFLAGS_MAINT_APPEND = -O0
endif
override_dh_auto_configure:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	dh_auto_configure -B$(BUILD_DIR)
else
	dh_auto_configure -B$(BUILD_DIR) -- --disable-doc
endif
	dh_auto_configure -B$(BUILD_NOX) -- --disable-doc --disable-gui --disable-font-checks
override_dh_auto_build:
	dh_auto_build -D$(BUILD_DIR)
	dh_auto_build -D$(BUILD_NOX)
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_test -D$(BUILD_DIR)
endif
override_dh_auto_install:
	dh_auto_install -D$(BUILD_DIR) --destdir=$(INST_DIR)
	dh_auto_install -D$(BUILD_NOX) --destdir=$(INST_NOX)
execute_after_dh_auto_install:
	jdupes -rl $(INST_DIR)
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	# Clear build path from embedded image meta
	find $(INST_DIR) -name msc-gen.pdf | xargs perl -p -i -e 's|(/PTEX.FileName \()([^\)]+/)([^/\)]+)|$$1 . " " x length($$2) . $$3|ge'
	# Remove '-gui' part from the no-X manpage
	{ o=`find $(INST_DIR) -name msc-gen.1 | head -1`; \
	  d=`dirname $(INST_NOX)$${o#$(INST_DIR)}`; \
	  mkdir -p $$d; \
	  sed '/-gui/,/^[.]TP/d' $$o >$$d/msc-gen.1; }
endif
override_dh_install \
override_dh_installman \
override_dh_installdocs \
override_dh_installexamples \
override_dh_installinfo \
override_dh_missing:
	$(@:override_%=%) --sourcedir=$(INST_DIR) -pmsc-generator -pmsc-generator-doc
	$(@:override_%=%) --sourcedir=$(INST_NOX) -pmsc-generator-nox
override_dh_installchangelogs:
	dh_installchangelogs $(BUILD_DIR)/NEWS
execute_after_dh_auto_clean:
	rm -rf $(BUILD_DIR) $(BUILD_NOX) doc/stamp-vti doc/version.texi
