#!/usr/bin/make -f
# Rules for mkvtoolnix based on Joey Hess's sample

####################################################################
# Build flags
####################################################################
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

CONFIG_FLAGS = --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info

#######################################################################
# BUILD : Two versions: without and with GUI.
#	  This impacts mmg which only exists in -GUI
#	  For mkvinfo, we use an alternative
#######################################################################
build: build-stamp

build-stamp:
	dh_testdir

	CFLAGS="$(CFLAGS)" ./configure $(CONFIG_FLAGS) --enable-gui
	$(MAKE)
	mv src/mkvinfo src/mkvinfo-gui
	mv src/mmg/mmg src/mmg/mmg-gui

	$(MAKE) distclean

	CFLAGS="$(CFLAGS)" ./configure $(CONFIG_FLAGS) --disable-gui
	$(MAKE)

	touch build-stamp

########################################################################
# Other rules
########################################################################
clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	rm -f src/mkvinfo-gui
	rm -f src/mmg/mmg-gui

	-$(MAKE) clean
	rm -f Makefile config.h
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
	rm -f config.log
	rm -f config.status

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k

	$(MAKE) install prefix=$(CURDIR)/debian/tmp


binary-indep: build install
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs -a
	dh_installexamples

	dh_install -a
	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_installmenu -pmkvtoolnix-gui
	dh_installman
	dh_desktop -pmkvtoolnix-gui

	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

	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
