#!/usr/bin/make -f

ifneq (,$(findstring parallel=,$(DEB_BUILD_OPTIONS)))
	NCPUS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
else
	NCPUS := $(shell getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)
	ifeq ($(NCPUS),-1)
		NCPUS:=1
	endif
	ifeq ($(NCPUS),0)
		NCPUS:=1
	endif
endif

export DEB_CFLAGS_MAINT_APPEND = -fPIC

%:
	dh $@ -Dtoonz/sources -Bbuild -Scmake

override_dh_auto_configure:
	cd thirdparty/tiff-4.0.3 && \
	./configure --with-pic --disable-jbig

	$(MAKE) -C thirdparty/tiff-4.0.3 -j $(NCPUS)

	dh_auto_configure -- \
	-DCMAKE_BUILD_WITH_INSTALL_RPATH=FALSE -DCMAKE_SKIP_RPATH-ADVANCED:INTERNAL=FALSE

#	exit 1

# Doesn't link against system libtiff
#	-DTIFF_LIBRARY:FILEPATH=/usr/lib/$(DEB_HOST_MULTIARCH)/libtiff.so

execute_after_dh_auto_install:
	patchelf --remove-rpath debian/tmp/usr/lib/opentoonz/*.so
	patchelf --remove-rpath debian/tmp/usr/bin/t*
	patchelf --remove-rpath debian/tmp/usr/bin/l*
	patchelf --remove-rpath debian/tmp/usr/bin/OpenToonz

override_dh_auto_clean:
	[ ! -f thirdparty/tiff-4.0.3/Makefile ] || $(MAKE) -C thirdparty/tiff-4.0.3 distclean

	dh_auto_clean

override_dh_clean:

	-rm -rf thirdparty/boost
	-rm -rf thirdparty/glew
	-rm -rf thirdparty/glut
	-rm -rf thirdparty/LibJPEG
	-rm -rf thirdparty/libmypaint
	-rm -rf thirdparty/libpng-*
	-rm -rf thirdparty/libusb
	-rm -rf thirdparty/Lz4
	-rm -rf thirdparty/openblas
	-rm -rf thirdparty/quicktime
	-rm -rf thirdparty/superlu
	-rm -rf thirdparty/zlib-*

	find -name *.exe -delete

	dh_clean

# Don't delete these directories
# thirdparty/kiss_fft130
# thirdparty/lzo
# thirdparty/tiff-4.0.3
