#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2015 IOhannes m zmölnig <umlaeute@debian.org>
# Description: Main Debian packaging script for JUCE

## export dummy TARGET_ARCH that doesn't do much,
## but disables "-march=native" optimization
export TARGET_ARCH=-Wl,--as-needed

DEB_SRCDIR=extras/Projucer/Builds/LinuxMakefile
DEBIAN_BUILD_ARTIFACTS = debian/artifacts

DEB_COPYRIGHT_CHECK_IGNORE_REGEX = \
        \.git/.*|debian/.*|.*\.gif|.*\.jpg|.*\.JPG|.*\.png|.*\.ico|.*\.icns|.*\.wav|.*\.caf|.*\.ogg|.*\.mp3|.*\.pdf|.*\.keystore|.*\.nib|.*/gradle-wrapper\.jar|.*\.zip


#DEB_COPYRIGHT_CHECK_IGNORE_REGEX = \
#^((.*\.jpg)|(.*\.JPG)|(.*\.gif)|(.*\.png)|(.*\.ico)|(.*\.icns)|(gradle-wrapper\.jar)|(.*\.mp3)|(.*\.caf)|(.*\.nib)|examples/InAppPurchase/Signing/InAppPurchase\.keystore|examples/(InAppPurchase/BinaryData/(Robot|Ed|Jules|JB|Fabian|Lukasz)[012]\.ogg|AUv3Synth|PlugInSamples/MultiOutSynth)/Source/BinaryData/singing\.ogg|examples/Demo/Resources/cello\.wav|examples/Demo/Resources/icons\.zip|modules/juce_audio_plugin_client/RTAS/juce_RTAS_WinResources\.rsr|examples/ComponentTutorialExample/Introduction to Components - Part 1\.pdf|debian/(changelog|copyright(|_hints|_newhints)))$

LDFLAGS += $(shell pkg-config --libs libjpeg libpng zlib)

# JUCE 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, 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|riscv64)")
# link with libatomic on architectures without built-in atomic
ifeq ($(if $(noatomicarch),atomic), atomic)
	LDFLAGS += -latomic
	CPPFLAGS += -DDEBIAN_JUCERPROJECT_LIBS='\"atomic\"'
endif

%:
	dh $@

override_dh_auto_build-arch:
	cp $(CURDIR)/examples/DemoRunner/Builds/iOS/DemoRunner/Images.xcassets/AppIcon.appiconset/Icon.png \
		$(DEBIAN_BUILD_ARTIFACTS)/juce.png
	dh_auto_build -D $(DEB_SRCDIR) -- \
		V=1 \
		LDFLAGS="$(LDFLAGS)" \
		CPPFLAGS="$(CPPFLAGS)" \
		$(empty)
	make -C debian/extra/lv2-ttl-generator/
	help2man -N \
		--version-string="Projucer $(DEB_UPSTREAM_VERSION)" \
		-n "the JUCE project-management tool" \
		debian/scripts/Projucer-help2man \
		> $(DEBIAN_BUILD_ARTIFACTS)/Projucer.1

override_dh_auto_build-indep:
	make -C doxygen

override_dh_auto_install-arch:
	install -d debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/juce/lv2-ttl-generator
	install debian/extra/lv2-ttl-generator/lv2_ttl_generator debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/juce/lv2-ttl-generator/lv2_ttl_generator
	install debian/extra/lv2-ttl-generator/generate-ttl.sh debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/juce/lv2-ttl-generator/generate-ttl.sh

override_dh_auto_install-indep:
	-dh_doxygen doxygen/doc/

override_dh_install-indep:
	dh_install --indep -X.tag
	-find $(CURDIR)/debian/juce-modules-source/usr/share/juce \
		-type f -executable \
		'(' -name "*.h" -or -name "*.cpp" ')' \
		-exec chmod -c 0644 {} +

override_dh_installchangelogs:
	dh_installchangelogs ChangeList.txt

override_dh_clean:
	dh_clean
	make -C doxygen/ clean
	make -C debian/extra/lv2-ttl-generator/ clean
	rm -rf build $(DEB_SRCDIR)/build
	rm -f $(DEBIAN_BUILD_ARTIFACTS)/*


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
