#!/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_SYSTEM = pycentral
DEB_PYTHON_MODULE_PACKAGES = python-csound python-csoundac
include /usr/share/cdbs/1/rules/upstream-tarball.mk
include /usr/share/cdbs/1/class/scons.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

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 = 70b0c4a159c4960a09719674657949c9

# 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

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

# Core options of csound
DEB_SCONS_OPTIONS = \
	dynamicCsoundLibrary=1 \
	useDouble=1 \
	buildNewParser=0 \
	NewParserDebug=0 \
	useGettext=1 \
	buildRelease=1 \
	buildUtilities=1 \
	noDebug=0 \
	useLrint=0 \
	Word64=0 \
	Lib64=0 \
	useAltivec=0 \
	withICL=0 \
	withMSVC=0 \
	withSunStudio=0 \
	prefix=/usr \
	generatePdf=0 \
	generateTags=0 \
	bufferoverflowu=0 \
	LINKFLAGS='-Wl,-as-needed' \
	SHLINKFLAGS='-Wl,-as-needed'

# I/O plugins
DEB_SCONS_OPTIONS += \
	useCoreAudio=0 \
	usePortAudio=1 \
	usePortMIDI=1 \
	useJack=1 \
	buildVirtual=1  # Virtual MIDI keyboard
# PulseAudio does not have an option
ifeq "$(DEB_HOST_ARCH_OS)" "linux"
	DEB_SCONS_OPTIONS += useALSA=1
else
	DEB_SCONS_OPTIONS += useALSA=0
endif

# Optional opcodes
DEB_SCONS_OPTIONS += \
	useFLTK=1 \
	noFLTKThreads=1 \
	useOSC=1 \
	buildPythonOpcodes=1 \
	buildDSSI=1 \
	buildLoris=0 \
	useUDP=1 \
	buildStkOpcodes=1 \
	buildvst4cs=0 \
	includeMP3=0 \
	includeWii=0 \
	includeP5Glove=0 \
	buildImageOpcodes=1

# Language Wrappers
# TODO: build for all Python versions (not only current default one)
TCLVERSION := $(shell dpkg -l tcl-dev | sed -n 's/.*\(8\.[45]\)\..*/\1/p')
DEB_SCONS_OPTIONS += \
	buildInterfaces=1 \
	buildJavaWrapper=1 \
	buildPythonWrapper=1 \
	pythonVersion=$(cdbs_python_current_version) \
	buildLuaWrapper=1 \
	buildTclcsound=1 \
	tclversion=$(TCLVERSION) \
	buildPDClass=1

# Frontends
DEB_SCONS_OPTIONS += \
	buildCSEditor=0 \
	buildCsoundAC=1 \
	buildCsoundVST=0 \
	buildCsound5GUI=0 \
	buildOSXGUI=0 \
	buildWinsound=1


# This is a huge optimization boost
ifneq "$(filter amd64 i386, $(DEB_HOST_ARCH_CPU))" ""
	DEB_SCONS_OPTIONS += gcc4opt=generic
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

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

# Enable parallel building
ifneq (,$(findstring parallel,$(DEB_BUILD_OPTIONS)))
    PARALLEL_JOBS := $(shell echo $(DEB_BUILD_OPTIONS) | \
        sed -e 's/.*parallel=\([0-9]\+\).*/\1/')
NJOBS := -j$(PARALLEL_JOBS)
endif


DEB_DH_INSTALL_SOURCEDIR = debian/tmp

fltk-orig-files := $(shell find * -type f -name '*.fl')
fltk-autofiles = $(fltk-orig-files:.fl=.cpp) $(fltk-orig-files:.fl=.cxx) $(fltk-orig-files:.fl=.hpp) $(fltk-orig-files:.fl=.h)
swig-autofiles = interfaces/python_interface_wrap.cc interfaces/python_interface_wrap.h
upstreamtmpfiles = custom.py $(fltk-autofiles) $(swig-autofiles) doc/latex/doxygen.sty

common-configure-arch common-configure-indep:: common-configure-impl
common-configure-impl::
	cp --remove-destination debian/custom.py custom.py

DEB_SCONS_BUILD_OPTIONS = $(NJOBS)

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
	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
# and there still is stuff that doesn't get deleted.
	rm -f interfaces/*.java
	rm -f interfaces/*_interface_wrap.*
	rm -f frontends/CsoundVST/CsoundVST_wrap.h
	rm -f frontends/CsoundAC/CsoundAC_wrap.h
	rm -f lib_CsoundVST.so CsoundVST.py
	rm -f libcsound64.so
	rm -f libcsound64.so.5
	rm -f frontends/cseditor/cseditor.o
	rm -f libcsnd.so
	rm -f CsoundAC.py lib_CsoundAC.so
	rm -f frontends/CsoundAC/*.os
	rm -f libCsoundAC.so*
	rm -rf doc/html
	rm -f csnd.py
# Localisation stuff
	rm -rf po/*/
# fail-missing stuff
	rm -f debian/cdbs-install-diff
	rm -f debian/cdbs-reduced-install-list
	rm -f debian/cdbs-reduced-package-list

install/libcsound64-5.2::
	mkdir -p $(CURDIR)/debian/tmp/usr/share/locale
	cp -r po/*/ $(CURDIR)/debian/tmp/usr/share/locale

# Scons sucks
install/libcsound64-dev::
	ln -sf libcsound64.so.* $(CURDIR)/debian/tmp/usr/lib/libcsound64.so

# 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/python-csound::
	mkdir -p $(CURDIR)/debian/tmp/$(PYLIBPATH)
	cp csnd.py _csnd.so \
		$(CURDIR)/debian/tmp/$(PYLIBPATH)

install/liblua5.1-luacsnd5.2::
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/lua/5.1/
	install luaCsnd.so $(CURDIR)/debian/tmp/usr/lib/liblua5.1-luacsnd.so.5.2
	ln -s ../../lua5.1-luacsnd.so.5.2 \
	       	$(CURDIR)/debian/tmp/usr/lib/lua/5.1/luaCsnd.so

install/libcsnd-dev::
	ln -s libcsnd.so.5.2 $(CURDIR)/debian/tmp/usr/lib/libcsnd.so
	ln -s liblua5.1-luacsnd.so.5.2 \
		$(CURDIR)/debian/tmp/usr/lib/liblua5.1-csnd.so

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/python-csoundac::
	cp CsoundAC.py _CsoundAC.so \
		$(CURDIR)/debian/tmp/$(PYLIBPATH)

install/libcsoundac-dev::
	cp -P libCsoundAC.so $(CURDIR)/debian/tmp/usr/lib/

install/csladspa::
	mkdir $(CURDIR)/debian/tmp/usr/lib/ladspa
	mv $(CURDIR)/debian/tmp/usr/lib/libcsladspa.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' \
		> debian/cdbs-reduced-package-list
	cat debian/cdbs-install-list | sort | uniq | \
		grep -v '/$(PYLIBPATH)' | \
		grep -v '/usr/share/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 += , \
	libsndfile1-dev

# Needed for I/O plugins
CDBS_BUILD_DEPENDS += , \
	libasound2-dev [!hurd-i386 !kfreebsd-amd64 !kfreebsd-i386], \
	libjack-dev, \
	portaudio19-dev, \
	libportmidi-dev, \
	libpulse-dev

# Language bindings
CDBS_BUILD_DEPENDS += , \
	swig, \
	liblua5.1-0-dev, \
	tk-dev, \
	tcl-dev, \
	python-all-dev (>= 2.3.5-11), \
	puredata, \
	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

# 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
