#! /usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS:=hardening=+all

# To enable parallel building:
# You can either set DEB_BUILD_OPTIONS=paralell=<num-procs> in your build environment
# or provide the -j<numprocs> option to debuild or dpkg-buildpackage, which
# ammounts to the same thing.
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
# use MFLAGS, rather than MAKEFLAGS as the latter is used by make internally
	MFLAGS += -j$(NUMJOBS)
	WAFFLAGS += -j$(NUMJOBS)
endif

# make .PHONY all the targets that have name collisions with the scripts
# see http://www.debian.org/doc/manuals/maint-guide/dreq.en.html#rules
.PHONY: clean build install
# Apparently, the above isn't enough because of the "%" target. Make the problematic targets explicit
clean:
	exec dh $@
build:
	exec dh $@
install:
	exec dh $@
# Handle all other targets in the usual way.
# The --parallel flag to dh doesn't seem to have the intended effect
# so leave it out.
%:
	exec dh $@

# this should really create multiple shared object debian packages but
# this will do for now
# the dependencies must be built before mpv is configured
#x264_config:
#	scripts/x264-config

#x264_build: x264_config
#	scripts/x264-build $(MFLAGS)

mujs_build:
	scripts/mujs-build

libass_config:
	scripts/libass-config

libass_build: libass_config
	scripts/libass-build $(MFLAGS)

ffmpeg_config:
	scripts/ffmpeg-config

ffmpeg_build: ffmpeg_config
	scripts/ffmpeg-build $(MFLAGS)

# put the config in the right place and drop the local/ since it's package managed now
override_dh_auto_configure: mujs_build libass_build ffmpeg_build
	scripts/mpv-config --prefix=/usr --confdir=/etc/mpv

override_dh_auto_build:
	scripts/mpv-build $(WAFLAGS)

DOCDEST=debian/mpv/usr/share/doc/mpv
DOCSOURCE=mpv/DOCS
TOOLDEST=debian/mpv/usr/share/doc/mpv/tools
TOOLSOURCE=mpv/TOOLS
# call waf to install to the debian packageing dir

override_dh_auto_install:
	cd mpv && python waf -v install --destdir=../debian/mpv
#	cp -f debian/mpv.conf.ubuntu debian/mpv/etc/mpv/mpv.conf

	find "$(DOCSOURCE)" -mindepth 1 -type d \
	-not -regex '.*man.*' \
	-not -regex '.*client_api_examples.*' \
	-printf "%P\0" | \
	xargs -0i /usr/bin/install -d "$(DOCDEST)/{}"

	find "$(DOCSOURCE)" -mindepth 1 -type f -not -regex '.*manu.*' \
	-not -regex '.*man.*' \
	-not -regex '.*client_api_examples.*' \
	-not -name 'tech-overview.txt' \
	-not -name 'waf-buildsystem.rst' \
	-not -name 'crosscompile-mingw.md' \
	-not -name 'coding-style.md' \
	-printf "%P\0" | \
	xargs -0i /usr/bin/install -m644 "$(DOCSOURCE)/{}" "$(DOCDEST)/{}"

	find "$(TOOLSOURCE)" -mindepth 1 -type d \
	-not -regex '.*osxbundle.*' \
	-printf "%P\0" | \
	xargs -0i /usr/bin/install -d "$(TOOLDEST)/{}"

	find "$(TOOLSOURCE)" -mindepth 1 -type f \
	-not -regex '.*osxbundle.*' \
	-not -name 'gen-x11-icon.sh' \
	-not -name 'file2string.pl' \
	-not -name 'uncrustify.cfg' \
	-printf "%P\0" | \
	xargs -0i /usr/bin/install -m644 "$(TOOLSOURCE)/{}" "$(TOOLDEST)/{}"

override_dh_compress:
	dh_compress --exclude=.md

# call all the cleans
override_dh_auto_clean:
	./clean
	find . -name "*.pyc" -delete
