#!/usr/bin/make -f
# output every command that modifies files on the build system.
#export DH_VERBOSE = 1

# see FEATURE AREAS in dpkg-buildflags(1)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all


# iem-plugin-suite uses some c++11 features requiring atomic_store_8 and
# atomic_load_8, so we need to link with libatomic on
# armel, powerpc, powerpcspe, m68k, mips, mipsel, and sh4
# see also:
# - https://gcc.gnu.org/wiki/Atomic
# - https://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary
# - the 'clasp' packaging
noatomicarch = $(shell dpkg-architecture -qDEB_HOST_ARCH | egrep -x "(armel|powerpc|powerpcspe|m68k|mips|mipsel|sh4)")

export MAKEFLAGS = V=1 CONFIG=Release TARGET_ARCH=-DDEBIAN_BUILD=1

PCDEPS=fftw3f

# see ENVIRONMENT in dpkg-buildflags(1)
export DEB_CPPFLAGS_MAINT_APPEND  = -DJUCE_JACK=1
# package maintainers to append CFLAGS
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic \
	$(shell pkg-config --cflags $(PCDEPS))
# package maintainers to append LDFLAGS
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed \
	$(shell pkg-config --libs $(PCDEPS))

# link with libatomic on architectures without built-in atomic
ifeq ($(if $(noatomicarch),atomic), atomic)
	DEB_LDFLAGS_MAINT_APPEND += -latomic
endif

JUCE_VERSION := $(shell dpkg-query --show --showformat='$${source:Version}' juce-modules-source)

%:
	dh $@

override_dh_auto_build:
	xvfb-run -a dh_auto_build --no-parallel
override_dh_install:
	for d in _man _vst _bin; do rm -rf $(CURDIR)/$$d; mkdir -p $(CURDIR)/$$d; done
	# install the VST plugins and the standalong applications
	find $(CURDIR) -mindepth 1 -maxdepth 2 -type f -name "*.jucer" -not -name "_*" -not -name "*/_*" -print0 \
		| LC_COLLATED=C sort -z \
		| xargs -r0 $(CURDIR)/debian/install.sh
	
	dh_install

	# create symlinks to the various VST search paths
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/lxvst/ $(CURDIR)/debian/tmp/usr/lib/vst/
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/lxvst/ $(CURDIR)/debian/tmp/usr/lib/vst/
	## ardour
	ln -s /usr/lib/$(DEB_HOST_MULTIARCH)/iem-plugin-suite/vst $(CURDIR)/debian/tmp/usr/lib/lxvst/iem.at
	## carla
	ln -s /usr/lib/$(DEB_HOST_MULTIARCH)/iem-plugin-suite/vst $(CURDIR)/debian/tmp/usr/lib/vst/iem.at

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog.md


override_dh_gencontrol:
	dh_gencontrol -- \
                -Vjuce:BuiltUsing="juce ( = $(JUCE_VERSION) )"


override_dh_auto_clean:
	rm -rf $(patsubst %/, %/JuceLibraryCode, $(dir $(wildcard */*.jucer)))
	rm -rf $(patsubst %/, %/Builds, $(dir $(wildcard */*.jucer)))
	rm -rf _man _vst _bin

licensecheck:
	licensecheck --deb-machine -r * \
		> debian/copyright_newhints
	cmp debian/copyright_hints debian/copyright_newhints \
		&& rm debian/copyright_newhints
