#!/usr/bin/make -f

# This enables verbose mode.
export DH_VERBOSE=1
export V=1

# Determine the debian directory, which is the directory of this makefile.
# The path of this makefile is the first word in the MAKEFILE_LIST.
DEBIAN_DIR = $(dir $(firstword $(MAKEFILE_LIST)))

# Get the Debian version revision:
DEB_VERSION  := $(shell dpkg-parsechangelog -l$(DEBIAN_DIR)changelog -SVersion)
DEB_REVISION := $(word 2, $(subst -, ,$(DEB_VERSION)))

# Get the architecture triplet:
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Get the host architecture/OS:
DEB_HOST_ARCH_OS   ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_ARCH      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

# Ubuntu ld adds -Bsymbolic-functions by default, but that prevents FFmpeg from building.
LDFLAGS := $(filter-out %-Bsymbolic-functions,$(shell dpkg-buildflags --get LDFLAGS))

# Enable as many features as possible, as long as the result is still GPLv2+ (and not only GPLv3+).
# Therefore the following flags (and build-dependencies) are not added:
#   --enable-libopencore_amrnb      (libopencore-amrnb-dev)
#   --enable-libopencore_amrwb      (libopencore-amrwb-dev)
#   --enable-libvo_aacenc           (libvo-aacenc-dev)
#   --enable-libvo_amrwbenc         (libvo-amrwbenc-dev)
#   --enable-libsmbclient           (libsmbclient-dev)
# The following flags are not added, because the necessary libraries are not in Debian:
#   --enable-decklink
#   --enable-libcelt                (see #676592: removed from Debian as abandoned upstream, replaced by opus)
#   --enable-libilbc                (see #675959 for the RFP bug)
#   --enable-libnut
#   --enable-libstagefright-h264
#   --enable-libutvideo
#   --enable-libvidstab             (see #709193 for the RFP bug)
#   --enable-libxavs
# The following flags are not added for various reasons:
#   * --enable-libquvi: The wrapper wasn't updated for recent versions (due to license concerns) and thus might produce problems, e.g. for mpv.
#   * --enable-libv4l2 [!hurd-any]: This is only needed for very old devices and may cause problems for others.
#                                   Should anyone need it, using LD_PRELOAD pointing on libv4l2 has the same effect.
#   * --enable-opencl [!hurd-any]:  This is considered an experimental API.
# For the shared libraries there are several ways to build:
#  * Without further option: This will have no compatibility with Libav, but some sonames clash. Therefore no good choice.
#  * With --enable-incompatible-libav-abi, packages build against Libav would work with these libraries, but probably not the other way around.
#    This could be used together with Breaks:, Replaces: against the corresponding Libav libraries.
#  * With --enable-raise-major, the resulting binaries should be co-installable with the Libav ones and thus no conflicts would be necessary.
#    But this could break versioned configure checks.
#  * With --build-suffix="-ffmpeg" the library names are changed, so there can't be a conflict with Libav.
#    The patch pkg-config_file_without_build_suffix.patch ensures that packages using pkg-config still work with these FFmpeg library packages.
CONFIG := --prefix=/usr \
	--extra-version="$(DEB_REVISION)" \
	--build-suffix="-ffmpeg" \
	--toolchain=hardened \
	--libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
	--shlibdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
	--incdir=/usr/include/$(DEB_HOST_MULTIARCH) \
	--enable-gpl \
	--enable-shared \
	--disable-stripping \
	--enable-avresample \
	--enable-avisynth \
	--enable-ladspa \
	--enable-libass \
	--enable-libbluray \
	--enable-libbs2b \
	--enable-libcaca \
	--enable-libcdio \
	--enable-libflite \
	--enable-libfontconfig \
	--enable-libfreetype \
	--enable-libfribidi \
	--enable-libgme \
	--enable-libgsm \
	--enable-libmodplug \
	--enable-libmp3lame \
	--enable-libopenjpeg \
	--enable-libopus \
	--enable-libpulse \
	--enable-libschroedinger \
	--enable-libshine \
	--enable-libspeex \
	--enable-libssh \
	--enable-libtheora \
	--enable-libtwolame \
	--enable-libvorbis \
	--enable-libwavpack \
	--enable-libwebp \
	--enable-libxvid \
	--enable-opengl \
	--enable-x11grab

# Disable optimizations if requested.
ifneq (,$(filter $(DEB_BUILD_OPTIONS),noopt))
	CONFIG += --disable-optimizations
endif

# Some libraries are build only on linux.
ifeq ($(DEB_HOST_ARCH_OS),linux)
	CONFIG += --enable-libdc1394 \
	--enable-libiec61883
endif

# libzvbi is not available on m68k
ifneq ($(DEB_HOST_ARCH),m68k)
	CONFIG += --enable-libzvbi
endif

# Some libraries are not build on hurd.
ifneq ($(DEB_HOST_ARCH_OS),hurd)
	CONFIG += --enable-libzmq
endif

# Some libraries are not available on x32.
ifneq ($(DEB_HOST_ARCH),x32)
	CONFIG += --enable-frei0r \
	--enable-libvpx \
	--enable-libx264
endif

# Some libraries are not available on sparc64.
ifneq ($(DEB_HOST_ARCH),sparc64)
	CONFIG += --enable-libsoxr \
	--enable-gnutls \
	--enable-openal
endif

# OpenCV is not available on some architectures.
ifeq (,$(filter $(DEB_HOST_ARCH),m68k powerpcspe sh4 sparc64 x32))
	CONFIG += --enable-libopencv
endif

# librtmp-dev is uninstallable on sparc64.
ifeq (,$(filter $(DEB_HOST_ARCH),sparc64))
	CONFIG += --enable-librtmp
endif

# X265 is not (yet) available on some architectures.
ifeq (,$(filter $(DEB_HOST_ARCH),sparc m68k sh4 sparc64 x32))
	CONFIG += --enable-libx265
endif

# Disable altivec optimizations on powerpc, because they are not always available on this architecture.
ifeq ($(DEB_HOST_ARCH),powerpc)
	CONFIG += --disable-altivec
endif

# Disable assembly optimizations on ppc64el and x32, because they don't work (yet).
ifneq (,$(filter $(DEB_HOST_ARCH),ppc64el x32))
	CONFIG += --disable-asm
endif

# Disable optimizations on mips(el), because they are not always available on these architecture
# and on mips64(el), because they don't work (yet).
ifneq (,$(filter $(DEB_HOST_ARCH),mips mipsel mips64 mips64el))
	CONFIG += --disable-mips32r2 \
	--disable-mipsdspr1 \
	--disable-mipsdspr2 \
	--disable-mipsfpu
endif

# Use the default debhelper scripts, where possible.
# Enable parallel building.
%:
	dh $@ --parallel

# Add configuration options:
override_dh_auto_configure:
	$(info DEB_BUILD_OPTIONS = $(DEB_BUILD_OPTIONS))
	./configure $(CONFIG) || (cat config.log && exit 1)

# Building the source package fails without this override, because config.mak does not exist in the source.
# It (and related files) are created by configure and removed by distclean.
# The folder doc/examples/pc-uninstalled is not removed by distclean, so remove it manually.
override_dh_auto_clean:
	[ ! -f config.mak ] || make distclean
	[ ! -d doc/examples/pc-uninstalled ] || rm -r doc/examples/pc-uninstalled
	# Restore backed up CSS files
	[ ! -f debian/bootstrap.min.css.bak ] || mv debian/bootstrap.min.css.bak doc/bootstrap.min.css
	[ ! -f debian/style.min.css.bak ] || mv debian/style.min.css.bak doc/style.min.css

# Create doxygen documentation:
override_dh_auto_build-indep:
	dh_auto_build -i -- apidoc
	# Backup the minified CSS files distributed by upstream.
	mv doc/bootstrap.min.css debian/bootstrap.min.css.bak
	mv doc/style.min.css debian/style.min.css.bak
	# Create the minified CSS files.
	lessc debian/missing-sources/bootstrap/less/bootstrap.less | cleancss | cat debian/missing-sources/bootstrap_copyright_notice.txt - > doc/bootstrap.min.css
	lessc debian/missing-sources/ffmpeg-web/src/less/style.less | cleancss > doc/style.min.css

override_dh_auto_build-arch:
	# Build qt-faststart here, to make it possible to build with 'nocheck'.
	make tools/qt-faststart
	dh_auto_build -a || (cat config.log && exit 1)

# Set the library path for the dynamic linker, because the test otherwise don't find the libraries.
override_dh_auto_test:
	export LD_LIBRARY_PATH="libavcodec:libavdevice:libavfilter:libavformat:libavresample:libavutil:libpostproc:libswresample:libswscale"; dh_auto_test -- -k

# Move the debug symbols to the debug package:
override_dh_strip:
	# dh_strip would add timestamps to static libraries (*.a), so handle them manually.
	dh_strip --dbg-package=ffmpeg-dbg -X.a
	for lib in debian/*/usr/lib/*/*.a; do strip --strip-debug --enable-deterministic-archives "$$lib"; done

override_dh_auto_install:
	dh_auto_install
	# Create symbolic links from the standard library lib*.so symlinks, pkg-config files and static libraries to the suffixed ones.
	for lib in `find debian/ -name lib*-ffmpeg.so; find debian/ -name lib*.pc; find debian/ -name lib*.a`; do \
		dir=`dirname $$lib`; \
		base=`basename $$lib`; \
		link=`echo $$base | sed 's/-ffmpeg//'`; \
		ln -sf $$base $$link; \
		mv $$link $$dir; \
	done

# Don't compress the example source code files.
override_dh_compress:
	dh_compress -Xexamples

# Download the latest upstream tarball.
get-orig-source:
	cd $(DEBIAN_DIR)../ && \
	uscan --rename --force-download --destdir $(CURDIR)
