#!/usr/bin/make -f

#enable hardening
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

## use hardening flags
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

DATE_FMT = %Y/%m/%d at %H:%M:%S UTC
ifdef SOURCE_DATE_EPOCH
    BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "+$(DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "+$(DATE_FMT)" 2>/dev/null || date -u "+$(DATE_FMT)")
else
    BUILD_DATE ?= $(shell date "+$(DATE_FMT)")
endif

export PKGVERSION_CODENAME="Debian/$(DEB_VERSION)"

CPPFLAGS+=-DGEM_VERSION_CODENAME='"$(PKGVERSION_CODENAME)"'

archconfflags := \
	--libdir=\$${exec_prefix}/lib \
	--libexecdir=\$${exec_prefix}/lib \
	--with-pd=/usr/include/pd \
	--with-extension=pd_linux \
	--without-ALL \
	--with-glfw3 \
	--with-sdl \
	--with-ftgl \
	--with-fribidi \
	--with-ImageMagick \
	--with-jpeg \
	--with-tiff \
	--with-lqt \
	--with-libquicktime \
	--with-gmerlin_avdec \
	--with-libmpeg3 \
	--without-v4l \
	--without-libv4l1 \
	--with-v4l2 \
	--with-libv4l2 \
	--with-libdv \
	--with-libdc1394_2 \
	--with-libiec61883 \
	--with-libraw1394 \
	--with-unicap \
	--with-libunicap \
	--with-libvncclient \
	--with-libvlc \
	--with-DeckLink=local \
	--with-assimp \
	--with-build-date="on $(BUILD_DATE)" \
	--with-default-font=/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf

ifeq ($(DEB_HOST_ARCH_CPU), i386)
archconfflags += \
	--disable-mmx \
	$(empty)
endif

ifeq ($(DEB_HOST_ARCH_CPU), amd64)
archconfflags += \
	--enable-sse2 \
	$(empty)
endif

ifeq ($(DEB_HOST_ARCH_CPU), x32)
archconfflags += \
	--enable-sse2 \
	$(empty)
endif

ifeq ($(DEB_HOST_ARCH_CPU), ppc64el)
archconfflags += \
	--disable-altivec \
	$(empty)
CPPFLAGS+=-U__VEC__
endif



DEB_COPYRIGHT_CHECK_IGNORE_REGEX = \
	^\./\.git/.*|\.\/\.pc/.*|\./debian/|\./doc/manual/.*\.jpg|\./doc/manual/.*\.gif|\./examples/10\.glsl/.*\.jpg|\./examples/12\.multi_screen_projection\/.*\.jpg|\./examples/data/.*|.*\.pdf|\./build/osx-dmginstaller/DS_Store|.\/build/travis-ci/travisci.enc$

%:
	dh $@

execute_after_dh_clean:
	-rm -f gemdefaultwindow-*.pd

override_dh_auto_configure:
	CXXFLAGS="$(CXXFLAGS)" CFLAGS="$(CFLAGS)" dh_auto_configure -- $(archconfflags)

override_dh_auto_test:
	# no actual 'make test' target

override_dh_install:
	find src/Output/ -name "*.cpp" -exec \
		./debian/scripts/gemdefaultwindow.sh abstractions/gemdefaultwindow.pd.in  {} +
	-find debian/tmp/ -name gemdefaultwindow.pd -delete
	# remove libtool files, they are not needed
	-find debian/*/ -name '*.la' -print -delete
	# remove some assets
	-find debian/tmp/ -name '*.ttf' -delete
	#-find debian/tmp/ -name '*.avi' -delete
	#-find debian/tmp/ -name '*.mov' -delete
	#-find debian/tmp/ -name '*.mpg' -delete

	dh_install

	# the following COPYING only contains license information for vera.ttf
	# which is not included in the Debian package
	-rm debian/gem-doc/usr/share/gem/examples/data/COPYING.txt
	# remove extra LICENSE files for gem-extra
	-rm -f debian/gem-extra/usr/lib/pd/extra/pix_mano/LICENSE.txt
	-rm -f debian/gem-extra/usr/lib/pd/extra/pix_drum/LICENSE.txt

override_dh_installalternatives:
	echo "With dh>=13.1 there's a new way to handle alternatives" 1>&2
	echo "consider using it" 1>&2

override_dh_fixperms:
	dh_fixperms -Xpd-gem
	-chmod 0755 debian/gem/usr/bin/pd-gem
	-find debian -name '*.pd_linux' -exec \
		chmod 0644 {} +

override_dh_compress:
	dh_compress -X.pd -X.TIF -X.obj

licensecheck:
	licensecheck -i "$(DEB_COPYRIGHT_CHECK_IGNORE_REGEX)" --deb-machine -r . \
		> debian/copyright_newhints
	cmp debian/copyright_hints debian/copyright_newhints \
		&& rm debian/copyright_newhints
