#! /usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS := hardening=+all,-fPIC

# To enable parallel building:
# You can either set DEB_BUILD_OPTIONS=parallel=<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)
	MESONFLAGS += -j$(NUMJOBS)
	NINJAFLAGS += -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 $@

ffmpeg_config: libplacebo_build
	scripts/ffmpeg-config \
		--enable-gnutls \
		--enable-libaom \
		--enable-libass \
		--enable-libdav1d \
		--enable-libgme \
		--enable-libgsm \
		--enable-libmodplug \
		--enable-libmp3lame \
		--enable-libopus \
		--enable-libpulse \
		--enable-libsoxr \
		--enable-libspeex \
		--enable-libssh \
		--enable-libsvtav1 \
		--enable-libvorbis \
		--enable-libvpx \
		--enable-libwebp \
		--enable-ladspa \
		--enable-libbs2b \
		--enable-gpl --enable-libxvid --enable-libx264 \
		--enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvo-amrwbenc

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

libplacebo_config:
	scripts/libplacebo-config

libplacebo_build: libplacebo_config
	scripts/libplacebo-build $(NINJAFLAGS)

# drop the local/ since it's package managed now
override_dh_auto_configure: ffmpeg_build libplacebo_build
	scripts/mpv-config --prefix=/usr \
		-Dopenal=enabled \
		-Ddvdnav=enabled \
		-Dcdda=enabled

override_dh_auto_build:
	scripts/mpv-build $(MESONFLAGS)

# install mpv to the debian packaging dir
# --destdir is relative to mpv/build
override_dh_auto_install:
	scripts/mpv-install --destdir=../../debian/mpv

# for manually installed dependencies
override_dh_shlibdeps:
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info

# call all the cleans
override_dh_auto_clean:
	./clean

# No dbgsym packages:
override_dh_strip:
	@echo "NO--THANKS ANYWAY--but not building dbgsym packages for MX Linux"
	dh_strip --no-automatic-dbgsym
