#!/usr/bin/make -f

EPOCH=6:
DEB_SOURCE := $(shell dpkg-parsechangelog | sed -n 's/^Source: //p')
DEB_VERSION := $(shell dpkg-parsechangelog | sed -n 's/^Version: //p')
UPSTREAM_VERSION := $(shell echo $(DEB_VERSION) | sed -r 's/[^:]+://; s/-[^-]+$$//')
SHLIBS_VERSION := $(EPOCH)9.1-1

# these package do not build -extra variants
LIB_PKGS := $(shell sed -nr 's/^Package:[[:space:]]*(lib(avutil|avdevice|avformat|avfilter|avresample|swscale)[0-9]+)[[:space:]]*$$/\1/p' debian/control)
# these packages do build -extra variants
LIB_PKGS2 := $(shell sed -nr 's/^Package:[[:space:]]*(libavcodec[0-9]+)[[:space:]]*$$/\1/p' debian/control)
# these packages are the -extra- variants
LIB_EXTRA_PKGS := $(shell sed -nr 's/^Package:[[:space:]]*(libavcodec-extra-[0-9]+)[[:space:]]*$$/\1/p' debian/control)

# these are the -dev packages
DEV_PKGS := $(shell echo $(LIB_PKGS) $(LIB_PKGS2) | sed 's/[0-9]\+\>/-dev/g')

# Support multiple makes at once
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NUMJOBS = -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
else
# on i386 and amd64, we query the system unless overriden by DEB_BUILD_OPTIONS
ifeq      ($(DEB_HOST_ARCH),i386)
NUMJOBS := -j$(shell getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)
else ifeq ($(DEB_HOST_ARCH),amd64)
NUMJOBS := -j$(shell getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)
endif
endif

include debian/confflags
OPT_FLAVORS := $(filter-out static shared, $(FLAVORS))

info:
	@echo Packages that do not build -extra variants: $(LIB_PKGS)
	@echo Packages that do build -extra variatnts: $(LIB_PKGS2)
	@echo Packages that are the -extra- variants: $(LIB_EXTRA_PKGS)

$(info FLAVORS = $(FLAVORS))
$(info DEB_BUILD_OPTIONS = $(DEB_BUILD_OPTIONS))
$(info CFLAGS = $(CFLAGS))
$(info CPPFLAGS = $(CPPFLAGS))
$(info LDFLAGS = $(LDFLAGS))

define dh_install_file_opt_flavor
	  grep @DEB_HOST_MULTIARCH_OPT@ < $(1).in | \
	  sed 's:@DEB_HOST_MULTIARCH_OPT@:$(DEB_HOST_MULTIARCH)/$($(2)_shlibdir):g' >> $(1)

endef

DH_INSTALL_FILES := $(foreach pkg, $(LIB_PKGS) $(LIB_PKGS2) $(LIB_EXTRA_PKGS) $(DEV_PKGS), debian/$(pkg).install)
$(DH_INSTALL_FILES):
	sed 's/@DEB_HOST_MULTIARCH\(_OPT\)\?@/$(DEB_HOST_MULTIARCH)/g' $@.in > $@
	$(foreach opt_flavor,$(OPT_FLAVORS),$(call dh_install_file_opt_flavor,$@,$(opt_flavor)))

configure-%: configure-stamp-%
configure-stamp-%: configure
	dh_testdir
	mkdir -p debian-$*
	cd debian-$* && CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" $(CURDIR)/configure \
		$($*_build_confflags) $(extra_$*_build_confflags)
	touch $@
configure-extra-stamp-%:
	dh_testdir
	mkdir -p debian-extra-$*
	cd debian-extra-$* && CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" $(CURDIR)/configure \
		$($*_build_confflags) $(v3_confflags) $(extra_$*_build_confflags)
	touch $@

build-%: build-stamp-%
build-stamp-%: configure-stamp-%
	dh_testdir
	$(MAKE) -C debian-$* $(NUMJOBS) V=1
	touch $@
build-extra-stamp-%: configure-extra-stamp-%
	dh_testdir
	$(MAKE) -C debian-extra-$* $(NUMJOBS) V=1
	touch $@

debian-shared/tools/qt-faststart: build-stamp-shared
	$(MAKE) -C debian-shared tools/qt-faststart V=1

build: build-stamp build-extra
build-stamp: $(addprefix build-stamp-, $(FLAVORS)) debian-shared/tools/qt-faststart
	touch $@
build-extra: $(addprefix build-extra-stamp-, $(filter-out static,$(FLAVORS)))
	touch $@

clean:
	dh_testdir
	rm -f build-stamp build-extra $(addprefix build-stamp-, $(FLAVORS)) \
	    $(addprefix build-extra-stamp-, $(FLAVORS)) \
	    $(addprefix configure-stamp-, $(FLAVORS)) \
	    $(addprefix configure-extra-stamp-, $(FLAVORS)) \
	    build-doxy-stamp
	rm -rf libav/ # created by the libav-extra variant
	rm -rf $(addprefix debian-, $(FLAVORS)) \
		$(addprefix debian-extra-, $(FLAVORS)) doxy
	dh_clean $(DH_INSTALL_FILES) $(wildcard formats*.txt) $(wildcard codecs*.txt)

# The trailing newline is important!
define install_flavor
	$(MAKE) -C debian-$(1) install DESTDIR=$(CURDIR)/debian/tmp \
	    mandir=$(CURDIR)/debian/tmp/usr/share/man

endef

# The trailing newline is important!
define install_flavor_extra
	$(MAKE) -C debian-extra-$(1) install DESTDIR=$(CURDIR)/debian/tmp/extra \
	    mandir=$(CURDIR)/debian/tmp/usr/share/man

endef

# usage $(call call_and_install_avconv_dump,$package,$parameter), paramter like "codecs" or "formats"
define call_and_install_avconv_dump
	mkdir -p debian/$(1)/usr/share/doc/$(1)
	env LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)" \
	debian/tmp/usr/bin/avconv -$(2) > debian/$(1)/usr/share/doc/$(1)/$(2)-$(DEB_HOST_ARCH).txt
	cat debian/$(1)/usr/share/doc/$(1)/$(2)-$(DEB_HOST_ARCH).txt

endef

# installs arch-dependent files only
install: install-common install-extra
install-common: build $(DH_INSTALL_FILES)
	dh_testdir
	dh_testroot
	dh_prep -a
	$(foreach flavor,$(FLAVORS),$(call install_flavor,$(flavor)))
	mkdir -p debian/tmp/etc
	install -m 644 -D doc/avserver.conf debian/tmp/etc/
	install -m 755 -D debian-shared/tools/qt-faststart debian/tmp/usr/bin/qt-faststart
	dh_install $(addprefix -N,$(LIB_EXTRA_PKGS)) \
		 --fail-missing --sourcedir=debian/tmp
	$(call call_and_install_avconv_dump,libavformat54,formats)
	$(call call_and_install_avconv_dump,libavcodec54,codecs)
install-extra: build $(DH_INSTALL_FILES)
	dh_testdir
	dh_testroot
	$(foreach flavor,$(filter-out static,$(FLAVORS)),$(call install_flavor_extra,$(flavor)))
	dh_install $(addprefix -p,$(LIB_EXTRA_PKGS)) --fail-missing \
		--sourcedir=debian/tmp/extra
	$(call call_and_install_avconv_dump,libavcodec-extra-54,codecs)
ifeq ($(DEB_SOURCE), libav)
	dh_installdocs -a doc/APIchanges
	install -m 644 -D debian-shared/doc/*.html doc/t2h.init debian/libav-tools/usr/share/doc/libav-tools
	dh_installexamples -p libavcodec-dev libavcodec/api-example.c
	dh_installchangelogs -a Changelog
else
	dh_installchangelogs -a libav/Changelog
endif

binary-indep:
	dh_testdir
	dh_testroot
	dh_prep -i
	doxygen doc/Doxyfile
	mkdir -p debian/libav-doc/usr/share/doc/libav-doc
	mv -v doc/doxy/html debian/libav-doc/usr/share/doc/libav-doc
	dh_installdocs -i
	dh_installchangelogs -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i -- -Zxz

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installman -plibav-tools debian/qt-faststart.1
	dh_strip -a --dbg-package=libav-dbg
	dh_lintian -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a

	# operate on non -extra package producing packages
	for pkg in $(LIB_PKGS); do \
	    dh_makeshlibs -p"$$pkg" -V"$$pkg (>= $(SHLIBS_VERSION))"; \
	done
	# operate e.g. the -extra- packages, produce shlibs that have alternative dependencies
	for pkg in $(LIB_EXTRA_PKGS); do \
	    npkg=$$(echo "$$pkg" | sed -r 's/-extra-//'); \
	    dh_makeshlibs -p"$$pkg" -V"$$npkg (>= $(SHLIBS_VERSION)) | $$pkg (>= $(EPOCH)$(UPSTREAM_VERSION))"; \
	done
	# operate on packages with -extra- variants such as libavcodec54 - produce shlibs that have alternative dependencies
	for pkg in $(LIB_PKGS2); do \
	    upkg=$$(echo "$$pkg" | sed -r 's/([0-9]+)$$/-extra-\1/'); \
	    dh_makeshlibs -p"$$pkg" -V"$$pkg (>= $(SHLIBS_VERSION)) | $$upkg (>= $(EPOCH)$(UPSTREAM_VERSION))"; \
	done
	dh_shlibdeps
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a -- -Zxz

binary: binary-indep binary-arch

.PHONY: build $(addprefix build-, $(FLAVORS)) \
	clean \
	configure $(addprefix configure-, $(FLAVORS)) \
	binary binary-indep binary-arch \
	install install-common install-extra \
	get-orig-source
