#!/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>
# 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

# 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=lib/$(DEB_HOST_MULTIARCH) \
	-DPYTHON_MODULE_INSTALL_DIR=/usr/lib/python$(PYVERSION)/dist-packages \
	-DPYTHON3_MODULE_INSTALL_DIR=/usr/lib/python$(PYVERSION)/dist-packages \
	-DJAVA_MODULE_INSTALL_DIR=/usr/lib \
	-DLUA_MODULE_INSTALL_DIR=/usr/lib/lua/5.1/ \
	-DUSE_ATOMIC_BUILTIN=ON \


# I/O plugins
DEB_CMAKE_EXTRA_FLAGS += \
	-DUSE_PULSEAUDIO=ON \
	-DUSE_PORTAUDIO=ON \
	-DUSE_PORTMIDI=ON \
	-DUSE_COREMIDI=OFF \
	-DUSE_AUDIOUNIT=OFF \
	-DUSE_JACK=ON
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 += \
	-DUSE_FLTK=ON \
	-DBUILD_STK_OPCODES=ON \
	-DDEFAULT_STK_RAWWAVE_PATH=/usr/share/stk/rawwaves \
	-DBUILD_VIRTUAL_KEYBOARD=ON \
	-DBUILD_DSSI_OPCODES=ON \
	-DBUILD_CHUA_OPCODES=ON \
	-DBUILD_DSSI_OPCODES=ON \
	-DBUILD_FLUID_OPCODES=ON \
	-DBUILD_JACK_OPCODES=ON \
	-DBUILD_LINEAR_ALGEBRA_OPCODES=ON \
	-DBUILD_OSC_OPCODES=ON \
	-DBUILD_IMAGE_OPCODES=ON \
	-DBUILD_PYTHON_OPCODES=ON \
	-DBUILD_LUA_OPCODES=ON \
	-DBUILD_WIIMOTE_OPCODES=OFF \
	-DBUILD_P5GLOVE_OPCODES=OFF \
	-DUSE_CURL=OFF \
	-DBUILD_VST4CS_OPCODES=OFF \
	-DBUILD_FAUST_OPCODES=OFF \
	-DBUILD_CUDA_OPCODES=OFF \
	-DBUILD_OPENCL_OPCODES=OFF \
	-DBUILD_EXCITER_OPCODES=ON \
	-DBUILD_BUCHLA_OPCODES=ON \
	-DBUILD_PADSYNTH_OPCODES=ON \
	-DBUILD_HDF5_OPCODES=OFF \
	-DBUILD_SCANSYN_OPCODES=OFF \
	-DBUILD_FRAMEBUFFER_OPCODES=ON \
	-DBUILD_WEBSOCKET_OPCODE=OFF \
	-DBUILD_SELECT_OPCODE=ON \
	-DBUILD_STACK_OPCODES=ON \
	-DBUILD_ABLETON_LINK_OPCODES=OFF \
	-DBUILD_EMUGENS_OPCODES=ON \
	-DBUILD_PVSGENDY_OPCODE=OFF \


# 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_PYTHON_INTERFACE=ON \
	-DBUILD_JAVA_INTERFACE=ON \
	-DBUILD_LUA_INTERFACE=ON \

# Frontends
DEB_CMAKE_EXTRA_FLAGS += \
	-DBUILD_CSBEATS=ON \
	-DBUILD_BELA=OFF


# 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

BUILDDIR=obj-$(DEB_HOST_MULTIARCH)

%:
	dh $@ --with python3,buildinfo  --buildsystem=cmake+ninja --builddirectory=$(BUILDDIR)

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

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

override_dh_auto_build-arch:
	dh_auto_build

override_dh_auto_build-indep:
	# we need this to install headers
	dh_auto_build
	mkdir -p doc/doxygen
	cd doc && doxygen

override_dh_auto_test:
	OPCODE6DIR64=$(BUILDDIR) dh_auto_test

override_dh_auto_clean:
	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

override_dh_auto_install: override_dh_auto_install-indep override_dh_auto_install-arch

do_dh_auto_install:
	dh_auto_install
	# don't install the static library
	rm $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libcsound64.a

override_dh_auto_install-indep: do_dh_auto_install
	mkdir -p $(CURDIR)/debian/tmp/usr/share/csound/hrtf
	cp samples/*.dat $(CURDIR)/debian/tmp/usr/share/csound/hrtf

override_dh_auto_install-arch: do_dh_auto_install
	mv $(CURDIR)/debian/tmp/usr/bin/extract \
		$(CURDIR)/debian/tmp/usr/bin/csound_extract
	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
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/jni
	mv $(CURDIR)/debian/tmp/usr/lib/lib_jcsound6.so \
		$(CURDIR)/debian/tmp/usr/lib/jni

override_dh_installdocs:
	dh_installdocs
	dh_installdocs -A README.md AUTHORS
	dh_doxygen

override_dh_missing:
	dh_missing --fail-missing

override_dh_python3:
	dh_python3 -V$(PYVERSION)
