#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# 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

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

# 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
# package maintainers to append LDFLAGS
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed \
	$(shell pkg-config --libs \
		jack flac vorbis vorbisenc vorbisfile ogg libjpeg libpng zlib)

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

%:
	dh $@


override_dh_auto_install:
	mkdir -p $(CURDIR)/_vst/
	mkdir -p $(CURDIR)/debian/tmp/usr/bin/
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/lxvst/ $(CURDIR)/debian/tmp/usr/lib/vst/
	mkdir -p $(CURDIR)/_man
	rm -f $(CURDIR)/_man/*
	# install the VST plugins and the standalong applications
	for project in $(patsubst %/, %, $(dir $(wildcard */*.jucer))); do \
	  cp $$project/Builds/LinuxMakefile/build/$$project.so $(CURDIR)/_vst/ \
	  || true; \
	  cp $$project/Builds/LinuxMakefile/build/$$project $(CURDIR)/debian/tmp/usr/bin/ \
	  || true; \
	  cp $(CURDIR)/debian/iem-plugin-suite.1 $(CURDIR)/_man/$$project.1; \
	done
	# create symlinks to the various VST search paths
	## ardour
	mkdir -p $(CURDIR)/usr/lib/lxvst
	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
	dh_auto_install

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

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