#!/usr/bin/make -f

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

NCPUS := $(shell getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)

ifeq ($(NCPUS),-1)
	NCPUS:=1
endif

ifeq ($(NCPUS),0)
	NCPUS:=1
endif

%:
	dh $@

override_dh_auto_configure:
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif
	dh_auto_configure -- --enable-gui --docdir='$${datarootdir}/doc/mkvtoolnix-gui'

	./drake -j $(NCPUS) apps:mkvinfo apps:mmg

	mv src/mkvinfo src/mkvinfo-gui
	mv src/mmg/mmg src/mmg/mmg-gui

	./drake clean

	dh_auto_configure -- --disable-gui

	./drake -j $(NCPUS) default 'translations:guides'

override_dh_clean:
	dh_clean src/mkvinfo-gui src/mmg/mmg-gui config.status config.log
	[ ! -f build-config ] || ./drake clean

override_dh_auto_build:


override_dh_install:
	./drake install DESTDIR=$(CURDIR)/debian/tmp
	./drake 'install:translations:guides' DESTDIR=$(CURDIR)/debian/tmp docdir='$${datarootdir}/doc/mkvtoolnix-gui'
	dh_install --fail-missing

	mv debian/mkvtoolnix/usr/bin/mkvinfo debian/mkvtoolnix/usr/bin/mkvinfo-text
	mv debian/mkvtoolnix-gui/usr/bin/mmg-gui debian/mkvtoolnix-gui/usr/bin/mmg

	dh_link -pmkvtoolnix usr/share/man/man1/mkvinfo.1.gz usr/share/man/man1/mkvinfo-text.1.gz
	dh_link -pmkvtoolnix-gui usr/share/man/man1/mkvinfo.1.gz usr/share/man/man1/mkvinfo-gui.1.gz

override_dh_builddeb:
	dh_builddeb -- -Zbzip2
