#!/usr/bin/make -f

mgck_tar   = ImageMagick-7.0.8-6.tar.xz
mgck_dir   = $(CURDIR)/ImageMagick-7.0.8-6
cython_tar = cython-0.29.tar.xz
cython_dir = $(CURDIR)/Cython-0.29
VSVERSION  = $(shell dpkg-parsechangelog -SVersion | cut -d- -f1)

export V=1
export PKG_CONFIG_PATH=$(mgck_dir)/libs/lib/pkgconfig
export TESSERACT_CFLAGS=-I/usr/include
export TESSERACT_LIBS=-ltesseract -lm

CFLAGS   += -fvisibility=hidden -fno-strict-aliasing
CXXFLAGS += -fvisibility=hidden -fno-strict-aliasing

# work around some linking issues
imagemagick_libs = \
	-Wl,--whole-archive \
	  $(mgck_dir)/libs/lib/libMagick++-7.Q16HDRI.a \
	  $(mgck_dir)/libs/lib/libMagickWand-7.Q16HDRI.a \
	  $(mgck_dir)/libs/lib/libMagickCore-7.Q16HDRI.a \
	-Wl,--no-whole-archive \
	$$(pkg-config --static --libs ImageMagick)


%:
	dh ${@} --parallel --with autoreconf

override_dh_auto_clean:
	dh_auto_clean -Sautoconf
	rm -rf $(mgck_dir) $(cython_dir) build doc/_build

override_dh_auto_configure:

override_dh_auto_build:
	tar xf $(mgck_tar)
	cd $(mgck_dir) && ./configure \
		--prefix=$(mgck_dir)/libs \
		--enable-static \
		--disable-shared \
		--with-pic \
		--disable-docs \
		--enable-hdri \
		--with-quantum-depth=16
	dh_auto_build -D$(mgck_dir)
	$(MAKE) -C $(mgck_dir) install

	IMAGEMAGICK_LIBS="$(imagemagick_libs)" \
	LIBS="-pthread" \
	dh_auto_configure -- \
		--enable-static=yes \
		--enable-shared=yes \
		--with-pic \
		--enable-plugins \
		--enable-subtext
	tar xf $(cython_tar)
	python3 $(cython_dir)/cython.py $(CURDIR)/src/cython/vapoursynth.pyx
	dh_auto_build

	CFLAGS="$(CFLAGS) -fvisibility=default" \
	LDFLAGS="$(LDFLAGS) -L .libs" \
	python3 setup.py build
	sed -e 's|@VERSION@|$(VSVERSION)|' debian/VapourSynth-VERSION.egg-info > build/VapourSynth-$(VSVERSION).egg-info

	$(MAKE) -C doc html man

override_dh_auto_install:
	dh_auto_install
	$(foreach VS,vapoursynth vapoursynth-script,\
	  sed -i 's|Requires.private|#Requires.private|' $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/$(VS).pc;)

override_dh_installdocs:
	dh_installdocs -pvapoursynth-docs -plibvapoursynth-dev
	dh_installdocs --remaining-packages --link-doc=libvapoursynth

override_dh_installchangelogs:
	dh_installchangelogs -A ChangeLog

override_dh_shlibdeps:
	dh_shlibdeps -Llibvapoursynth -l/usr/lib/$(DEB_HOST_MULTIARCH)

override_dh_strip:
	dh_strip --dbg-package=vapoursynth-dbg

