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

DEB_PYTHON_MODULE_PACKAGES = python-csound python-csoundac
include /usr/share/cdbs/1/rules/upstream-tarball.mk
include /usr/share/cdbs/1/class/cmake.mk
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/python-module.mk
include /usr/share/cdbs/1/rules/utils.mk
include /usr/share/cdbs/1/class/pd.mk

DEB_UPSTREAM_URL = http://downloads.sourceforge.net/csound

# Adjustments for unusual upstream tarballs:
# * capitalized project name
# * no dash between project name and version
# * version number included in top folder inside tarball
DEB_UPSTREAM_PACKAGE = Csound
DEB_UPSTREAM_TARBALL_BASENAME_MANGLE = s/\-//g

DEB_UPSTREAM_TARBALL_MD5 = 3c4d0d0af71371f30099baae8319805f

# Non-DFSG license: limited to educational and research purposes
DEB_UPSTREAM_REPACKAGE_EXCLUDES = ./Opcodes/scansyn*
# Embedded tarball contains possibly source-less python binary
DEB_UPSTREAM_REPACKAGE_EXCLUDES += ./csound5_osx_installer_files.tar.gz
# Contains explicit copyright but lack licensing info
DEB_UPSTREAM_REPACKAGE_EXCLUDES += ./samples/VintageDreamsWaves-v2.sf2

# Skip some data files, in addition to the default debian files
DEB_COPYRIGHT_CHECK_IGNORE_REGEX = ^(samples/hrtf-.*\.dat|debian/(changelog|copyright(|_hints|_newhints)))$

# Common docs for all packages
DEB_INSTALL_DOCS_ALL = readme-csound5.txt AUTHORS

# Core options of csound
DEB_CMAKE_EXTRA_FLAGS = \
	-DBUILD_RELEASE=ON \
	-DFAIL_MISSING=ON \
	-DUSE_DOUBLE=ON \
	-DUSE_GETTEXT=ON \
	-DBUILD_STATIC_LIBRARY=OFF \
	-DUSE_OPEN_MP=OFF \
	-DUSE_LRINT=ON \
	-DBUILD_UTILITIES=ON \
	-DBUILD_NEW_PARSER=OFF \
	-DNEW_PARSER_DEBUG=OFF \
	-DBUILD_MULTI_CORE=OFF \
	-DBUILD_CATALOG=OFF \

# 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 \
	-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=OFF \
	-DBUILD_PD_CLASS=ON \
	-DBUILD_WIIMOTE_OPCODES=OFF

# Language Wrappers
# TODO: build for all Python versions (not only current default one)
# Force build of only the default one
# UGLY HACK: abuse this variable to pass an appropriate -V option
DEB_DH_PYTHONHELPER_ARGS_ALL = -V $(shell pyversions -vd)
TCLVERSION := $(shell dpkg -l tcl-dev | sed -n 's/.*\(8\.[45]\)\..*/\1/p')
DEB_CMAKE_EXTRA_FLAGS += \
	-DBUILD_CXX_INTERFACE=ON \
	-DBUILD_PYTHON_INTERFACE=ON \
	-DBUILD_JAVA_INTERFACE=ON \
	-DBUILD_LUA_INTERFACE=ON \
	-DBUILD_TCLCSOUND=ON

# Frontends
DEB_CMAKE_EXTRA_FLAGS += \
	-DBUILD_CSBEATS=ON \
	-DBUILD_CSOUND_AC=ON \
	-DBUILD_CSOUND_AC_PYTHON_INTERFACE=ON \
	-DBUILD_CSOUND_AC_LUA_INTERFACE=ON


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

# Python >= 2.6 should install to dist-packages instead of site-packages
# Since 2.5 is the only version < 2.6 we support, we test for that.
# TODO: Use simpler second form when 2.6 is default in a stable release
PYLIBPATH = usr/lib/$(cdbs_python_current_binary)/$(if $(call cdbs_streq,$(cdbs_python_current_version),2.5),site-packages,dist-packages)
#PYLIBPATH = usr/lib/$(cdbs_python_current_binary)/dist-packages
# Path with PREFIX removed
PYLIBPATH2 = lib/$(cdbs_python_current_binary)/$(if $(call cdbs_streq,$(cdbs_python_current_version),2.5),site-packages,dist-packages)

CC = $(DEB_HOST_GNU_TYPE)-gcc
CXX = $(DEB_HOST_GNU_TYPE)-g++

# Support parallel building if enabled in DEB_BUILD_OPTIONS
DEB_BUILD_PARALLEL = 1

DEB_DH_INSTALL_SOURCEDIR = debian/tmp

upstreamtmpfiles = doc/latex/doxygen.sty

DEB_SCONS_INSTALL_OPTIONS = instdir=$(CURDIR)/debian/tmp
DEB_SCONS_INSTALL_TARGET = install=1


install/libcsound64-doc:: build-doxygen-stamp
build-doxygen-stamp:
	doxygen
	touch $@

# Put aside upstream cruft before build but after licensecheck
pre-build:: debian/stamp-upstreamtmpstuff
debian/stamp-upstreamtmpstuff:
	for file in $(upstreamtmpfiles); do \
		[ ! -e $$file ] || [ -e $$file.upstream ] || mv -v $$file $$file.upstream; \
	done
	for dir in $(upstreamtmpdirs); do \
		[ ! -e $$dir ] || [ -e $$dir.upstream ] || mv -v $$dir $$dir.upstream; \
	done
	sed -e 's,@PYLIBPATH@,$(PYLIBPATH2),' -e 's,@MTUNE@,$(MTUNE),' \
		debian/Custom.cmake > \
		Custom.cmake
	touch $@

# Revert upstream cruft that was put aside during build
clean::
	for file in $(upstreamtmpfiles); do \
		[ ! -e $$file.upstream ] || mv -v -f $$file.upstream $$file; \
	done
	for dir in $(upstreamtmpdirs); do \
		[ ! -e $$dir.upstream ] || mv -v -f $$dir.upstream $$dir; \
	done
	rm -f debian/stamp-upstreamtmpstuff

clean::
	rm -f build-arch-stamp build-indep-stamp manpages-stamp \
		build-doxygen-stamp
	rm -f Custom.cmake
	rm -rf doc/html
# fail-missing stuff
	rm -f debian/cdbs-install-diff
	rm -f debian/cdbs-reduced-install-list
	rm -f debian/cdbs-reduced-package-list

TMPDIR = $(CURDIR)/debian/tmp

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

# avoid conflict with extract
install/csound-utils::
	mv $(CURDIR)/debian/tmp/usr/bin/extract \
		$(CURDIR)/debian/tmp/usr/bin/csound_extract

install/pd-csound::
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/pd/extra
	mv $(CURDIR)/debian/tmp/usr/lib/csoundapi~.pd_linux \
		$(CURDIR)/debian/tmp/usr/lib/pd/extra

install/libcsnd-java::
	mkdir -p $(CURDIR)/debian/tmp/usr/share/java
	mv $(CURDIR)/debian/tmp/usr/lib/csnd.jar \
		$(CURDIR)/debian/tmp/usr/share/java/csnd-$(DEB_UPSTREAM_VERSION).jar
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/jni
	mv $(CURDIR)/debian/tmp/usr/lib/lib_jcsound.so \
		$(CURDIR)/debian/tmp/usr/lib/jni

install/tclcsound::
# Move the tcl interface to a private dir
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/tclcsound
	mv $(CURDIR)/debian/tmp/usr/lib/tclcsound.so \
		$(CURDIR)/debian/tmp/usr/lib/tclcsound
# Create the pkgIndex.tcl
	cd $(CURDIR)/debian/tmp/usr/lib && tclsh $(CURDIR)/pkgbuild.tcl

install/csladspa::
	mkdir $(CURDIR)/debian/tmp/usr/lib/ladspa
	mv $(CURDIR)/debian/tmp/usr/lib/csound/*/csladspa.so \
		$(CURDIR)/debian/tmp/usr/lib/ladspa/csladspa.so

install/libcsound64-doc::
	rm -f doc/html/*.md5 doc/html/*.map doc/html/*.dot


# Don't ship any executable examples, they don't need to be
# Currently only arch packages carry examples
common-binary-post-install-arch::
	find debian/*/usr/share/doc/*/examples -type f -executable -print0 \
		| xargs -0 chmod -v a-x

# Make sure that all files installed is also packaged
#  * ignore debhelper-installed files (we trust dh_install*)
#  * ignore python files (too tricky to track pycentral juggling)
binary: fail-missing
fail-missing: binary-arch binary-indep list-missing
	cat debian/cdbs-package-list | sort | uniq | \
		grep -v 'DEBIAN' | \
		grep -v '/$(PYLIBPATH)' | \
		grep -v '/usr/share/doc' | \
		grep -v '/usr/share/lintian/overrides' | \
		grep -v '/usr/share/menu' | \
		grep -v '/usr/share/pyshared' | \
		grep -v '/usr/lib/pyshared' \
		> debian/cdbs-reduced-package-list
	cat debian/cdbs-install-list | sort | uniq | \
		grep -v '/$(PYLIBPATH)' | \
		grep -v '/usr/share/pyshared' | \
		grep -v '/usr/lib/pyshared' \
		> debian/cdbs-reduced-install-list
	diff -u debian/cdbs-reduced-install-list \
		debian/cdbs-reduced-package-list \
		> debian/cdbs-install-diff \
		|| true
	@if [ "`wc -l debian/cdbs-install-diff`" != "0 debian/cdbs-install-diff" ]; then \
		echo "ERROR: Differences between intermediary installed and packaged files:"; \
		echo; \
		cat debian/cdbs-install-diff; \
		echo; \
		echo "Individual binary packages may work, but whole package build failed!"; \
		exit 1; \
	fi


# Needed by upstream build process
# First, the core dependencies
CDBS_BUILD_DEPENDS += , \
	cmake, \
	libsndfile1-dev

# Needed for I/O plugins
CDBS_BUILD_DEPENDS += , \
	libasound2-dev [linux-any], \
	libjack-dev, \
	portaudio19-dev, \
	libportmidi-dev, \
	libpulse-dev

# Language bindings
CDBS_BUILD_DEPENDS += , \
	swig, \
	liblua5.1-0-dev, \
	tk-dev, \
	tcl-dev, \
	python-dev (>= 2.6.6-3~), \
	puredata-dev, \
	default-jdk

# Opcodes and other features
CDBS_BUILD_DEPENDS += , \
	libfltk1.1-dev, \
	libfluidsynth-dev, \
	liblo-dev, \
	fluid, \
	ladspa-sdk, \
	libboost-dev, \
	libboost-serialization-dev, \
	libpng12-dev, \
	dssi-dev, \
	libstk0-dev, \
	libgmm++-dev, \
	bison, \
	flex

# Needed for documentation build process
# (should go to Build-Depends-Indep when we have proper build-arch support)
CDBS_BUILD_DEPENDS += , \
	doxygen, \
	graphviz, \
	ttf-dejavu
