#!/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)

# Only use '-O0' if given (via noopt).
# Hardening is enabled via the configure option --toolchain=hardened.
CFLAGS   := $(filter -O0,$(shell dpkg-buildflags --get CFLAGS))
CXXFLAGS := $(filter -O0,$(shell dpkg-buildflags --get CXXFLAGS))

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

# Faketime on hurd currently (faketime 0.9.6-3) doesn't work.
# Thus don't use it there.
# TODO: Use faketime on hurd again, once this is fixed.
ifeq ($(DEB_HOST_ARCH_OS),hurd)
  FT=
else
  # Get the date of the latest changelog entry.
  # This is used to make the build more binary reproducible.
  DAT  = $(shell dpkg-parsechangelog -l$(DEBIAN_DIR)changelog | grep Date: | sed s/Date:\ //)
  # Change the format to the only one that stops faketime from incresing the time.
  DATE = $(shell faketime "$(DAT)" date -u +"%Y-%m-%d %H:%M:%S")
  FT = faketime -f -m "$(DATE)"
endif

# 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-libx265                (see #750817 for the ITP 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 \
	--extra-cflags="$(CFLAGS)" \
	--extra-cxxflags="$(CXXFLAGS)" \
	--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-fontconfig \
	--enable-frei0r \
	--enable-gnutls \
	--enable-ladspa \
	--enable-libass \
	--enable-libbluray \
	--enable-libcaca \
	--enable-libcdio \
	--enable-libflite \
	--enable-libfreetype \
	--enable-libfribidi \
	--enable-libgme \
	--enable-libgsm \
	--enable-libmodplug \
	--enable-libmp3lame \
	--enable-libopencv \
	--enable-libopenjpeg \
	--enable-libopus \
	--enable-libpulse \
	--enable-librtmp \
	--enable-libschroedinger \
	--enable-libshine \
	--enable-libsoxr \
	--enable-libspeex \
	--enable-libssh \
	--enable-libtheora \
	--enable-libtwolame \
	--enable-libvorbis \
	--enable-libvpx \
	--enable-libwavpack \
	--enable-libwebp \
	--enable-libx264 \
	--enable-openal \
	--enable-opengl \
	--enable-x11grab \
	--enable-libxvid

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

# Some libraries are not build on hurd.
ifneq ($(DEB_HOST_ARCH_OS),hurd)
	CONFIG += --enable-libzmq
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 armel and ppc64el, because they don't work (yet).
ifneq (,$(filter $(DEB_HOST_ARCH),armel ppc64el))
	CONFIG += --disable-asm
endif


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

# libbs2b currently fails to build on arm64
ifneq ($(DEB_HOST_ARCH),arm64)
	CONFIG += --enable-libbs2b
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))
	$(info CFLAGS = $(CFLAGS))
	$(info CXXFLAGS = $(CXXFLAGS))
	./configure $(CONFIG) || cat config.log

# 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:
	# Use faketime, because doxygen embeds the creation date.
	$(FT) 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
	# Use faketime to make the build more binary reproducible.
	$(FT) dh_auto_build -a

# 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

# Move the debug symbols to the debug package:
# Faketime is necessary here, because dh_strip adds timestamps to static libraries (*.a).
override_dh_strip:
	$(FT) dh_strip --dbg-package=ffmpeg-dbg

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)
