#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2007-2009 Felipe Sateler <fsateler@gmail.com>
# Copyright © 2008-2010 Jonas Smedegaard <dr@jones.dk>
# Copyright © 2021 IOhannes m zmölnig <umlaeute@debian.org>
# Description: Main Debian packaging script for CSound
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Csound has many format errors, so disable that. Fix pending upstream
# https://github.com/csound/csound/pull/880
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

DEB_CMAKE_EXTRA_FLAGS =

# Core options of csound
DEB_CMAKE_EXTRA_FLAGS += \
	-DBUILD_RELEASE=ON \
	-DFAIL_MISSING=ON \
	-DUSE_DOUBLE=ON \
	-DUSE_GETTEXT=ON \
	-DUSE_GIT_COMMIT=OFF \
	-DBUILD_STATIC_LIBRARY=ON \
	-DUSE_LRINT=ON \
	-DBUILD_UTILITIES=ON \
	-DNEW_PARSER_DEBUG=OFF \
	-DREQUIRE_PTHREADS=ON \
	-DBUILD_MULTI_CORE=ON \
	-DBUILD_TESTS=ON \
	-DUSE_COMPILER_OPTIMIZATIONS=OFF \
	-DLIBRARY_INSTALL_DIR=/usr/lib/$(DEB_HOST_MULTIARCH) \
	-DPYTHON3_MODULE_INSTALL_DIR=/usr/lib/python3/dist-packages \
	-DJAVA_MODULE_INSTALL_DIR=/usr/lib \
	-DLUA_MODULE_INSTALL_DIR=/usr/lib/$(DEB_HOST_MULTIARCH)/lua/5.1/ \
	-DUSE_ATOMIC_BUILTIN=ON \
	$(empty)

# I/O plugins
DEB_CMAKE_EXTRA_FLAGS += \
	-DUSE_AUDIOUNIT=OFF \
	-DUSE_COREMIDI=OFF \
	-DUSE_PORTAUDIO=ON \
	-DUSE_PORTMIDI=ON \
	-DUSE_PULSEAUDIO=ON \
	-DUSE_JACK=ON \
	$(empty)

ifeq "$(DEB_HOST_ARCH_OS)" "linux"
	DEB_CMAKE_EXTRA_FLAGS += -DUSE_ALSA=ON
else
	DEB_CMAKE_EXTRA_FLAGS += -DUSE_ALSA=OFF
endif

# Optional opcodes
DEB_CMAKE_EXTRA_FLAGS += \
	-DBUILD_DSSI_OPCODES=ON \
	-DBUILD_DSSI_OPCODES=ON \
	-DBUILD_OSC_OPCODES=ON \
	-DUSE_CURL=OFF \
	-DBUILD_PADSYNTH_OPCODES=ON \
	-DBUILD_SCANSYN_OPCODES=OFF \
	$(empty)

# Language Wrappers
# TODO: build for all Python versions (not only current default one)
# Force build of only the default one
DEB_CMAKE_EXTRA_FLAGS += \
	-DBUILD_CXX_INTERFACE=ON \
	-DBUILD_LUA_INTERFACE=ON \
	$(empty)

# Frontends
DEB_CMAKE_EXTRA_FLAGS += \
	-DBUILD_CSBEATS=ON \
	-DBUILD_BELA=OFF \
	$(empty)

DEB_CMAKE_EXTRA_FLAGS += \
	-DCMAKE_BUILD_RPATH_USE_ORIGIN=ON \
	$(empty)

# This is a huge optimization boost
ifneq "$(filter amd64 i386, $(DEB_HOST_ARCH_CPU))" ""
	MTUNE = ON
endif
ifneq "$(filter amd64, $(DEB_HOST_ARCH_CPU))" ""
	ENABLE_SIMD = 1
else
	ENABLE_SIMD = 0
endif

# no java packages an hppa & hurd
ifeq (,$(filter $(DEB_HOST_ARCH),hppa hurd-i386))
DEB_CMAKE_EXTRA_FLAGS += \
	-DBUILD_JAVA_INTERFACE=ON \
	$(empty)
else
export DH_OPTIONS += -Nlibcsnd6-java
endif


BUILDDIR=obj-$(DEB_HOST_MULTIARCH)

%:
	dh $@ --buildsystem=cmake+ninja --builddirectory=$(BUILDDIR)

PACKAGE_DATE = $(shell env --ignore-environment TZ=UTC LANG=C date -d@$(SOURCE_DATE_EPOCH) +%F)

override_dh_auto_configure:
	sed -e 's,@MTUNE@,$(MTUNE),' \
		-e 's,@ENABLE_SIMD@,$(ENABLE_SIMD),' \
		-e 's,@DATE@,$(PACKAGE_DATE),' \
		debian/Custom.cmake > \
		Custom.cmake
	dh_auto_configure -- $(DEB_CMAKE_EXTRA_FLAGS)

execute_before_dh_auto_build:
	@echo "blhc: ignore-line-regexp: ^C\+\+ analysis\.\.\.$$"

execute_after_dh_auto_build-indep:
	mkdir -p doc/doxygen
	cd doc && doxygen

override_dh_auto_test:
	OPCODE6DIR64=$(BUILDDIR) dh_auto_test

execute_after_dh_auto_clean:
	rm -f Custom.cmake
	rm -rf doc/doxygen
	rm -f tests/c/debug.orc tests/c/debug.sco
	rm -f tests/c/*.wav
	rm -f Android/CsoundAndroid/jni/version.h

execute_after_dh_auto_install-arch:
	# don't install the static library
	rm $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libcsound64.a
	# fix overly generic name
	mv $(CURDIR)/debian/tmp/usr/bin/extract \
		$(CURDIR)/debian/tmp/usr/bin/csound_extract
	# JAVA
	mkdir -p $(CURDIR)/debian/tmp/usr/share/java
	mv $(CURDIR)/debian/tmp/usr/lib/csnd6.jar \
		$(CURDIR)/debian/tmp/usr/share/java/csnd6-$(DEB_VERSION_UPSTREAM).jar
	ln -s csnd6-$(DEB_VERSION_UPSTREAM).jar $(CURDIR)/debian/tmp/usr/share/java/csnd6.jar
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/jni
	mv $(CURDIR)/debian/tmp/usr/lib/lib_jcsound6.so \
		$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/jni
	# create some manpages
	debian/csound2man
	debian/csoundutils2man

libcsound64-installdocs:
	dh_installdocs -plibcsound64-doc --doc-main-package=libcsound64-dev
override_dh_installdocs-indep: libcsound64-installdocs
override_dh_installdocs-indep override_dh_installdocs-arch:
	dh_installdocs --remaining-packages -A README.md AUTHORS


execute_after_dh_installdocs-indep:
	dh_doxygen

DEB_COPYRIGHT_CHECK_IGNORE_REGEX = \
	debian/.*|.*\.wav|.*\.aiff?|.*\.mp3|.*\.lpc|.*\.pvx|.*\.png|.*\.tiff|.*\.sf2|.*\.dat|tests/soak/HRTFcompact|.*\.jar|.*\.jar
# 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
