#!/usr/bin/make -f
# Copyright © 2025 IOhannes m zmölnig <umlaeute@debian.org>

#enable hardening
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

pd64ext:=$(shell /usr/share/puredata/debian/dekencpu $(DEB_HOST_ARCH))


FLAVORS = single double
builddir=debian/build/flavor-

CONFIG = \
	-DVST2=ON -DVST2DIR=/usr/include \
	-DVST3=ON -DVST3DIR=/usr/include/vst3sdk \
	$(empty)

## per flavor configure options
CONFIG_single =
CONFIG_double =

ifneq ($(wildcard /usr/include/SuperCollider/),)
	CONFIG += \
		-DSC_INSTALLDIR=/usr/share/SuperCollider/Extensions/ \
		-DSC_INCLUDEDIR=/usr/include/SuperCollider \
		$(empty)
	CONFIG_single += -DSC=ON
	CONFIG_double += -DSC=OFF
else
	CONFIG_single += -DSC=OFF
	CONFIG_double += -DSC=OFF
endif
ifneq ($(wildcard /usr/include/pd/),)
	CONFIG += \
		-DPD_INSTALLDIR=/usr/lib/pd/extra \
		$(empty)
	CONFIG_single += -DPD=ON
	CONFIG_double += -DPD=ON -DPD_EXTENSION=linux-$(pd64ext)-64.so -DPD_FLOATSIZE=64
else
	CONFIG_single += -DPD=OFF
	CONFIG_double += -DPD=OFF
endif

%:
	dh $@

override_dh_auto_configure: $(patsubst %,configure_%,$(FLAVORS))
override_dh_auto_build-arch: $(patsubst %,build_%,$(FLAVORS))
override_dh_auto_install: $(patsubst %,install_%,$(FLAVORS))
ifneq ($(wildcard /usr/include/SuperCollider/),)
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/SuperCollider/plugins
	-mv $(CURDIR)/debian/tmp/usr/share/SuperCollider/Extensions/VSTPlugin/plugins $(CURDIR)/debian/tmp/usr/lib/SuperCollider/plugins/VSTPlugin
endif

configure_%:
	mkdir -p $(builddir)$*
	dh_auto_configure --builddirectory=$(builddir)$* -- $(strip $(CONFIG) $(CONFIG_$*))
build_%:
	dh_auto_build -a  --builddirectory=$(builddir)$*
install_%:
	dh_auto_install   --builddirectory=$(builddir)$*


DEB_COPYRIGHT_CHECK_IGNORE_REGEX = \
        debian/.*|sc/HelpSource/Classes/[^/]*\.jpg|sc/HelpSource/Classes/noparams\.JPG|sc/icons/sc_cube\.ico
# licensecheck v1
.PHONY: licensecheck
licensecheck:
	LANG=C.UTF-8 licensecheck \
		-i "^($(DEB_COPYRIGHT_CHECK_IGNORE_REGEX))$$" \
		--check '.*' --recursive --deb-machine --lines 0 * \
		> debian/copyright_newhints
	cmp debian/copyright_hints debian/copyright_newhints \
		&& rm debian/copyright_newhints
