#encoding: latin-1

# Mute
# Modular Sequencer
# Copyright (C) 2006 The Mute Development Team
#
# 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
# of the License, 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import os, glob, sys

posix = os.name == 'posix'
win32 = os.name == 'nt'
mac = os.name == 'mac'

######################################
#
# init environment and define options
#
######################################

opts = Options( 'options.conf', ARGUMENTS )
opts.Add("PREFIX", 'Set the install "prefix" ( /path/to/PREFIX )', "/usr/local")
opts.Add("DESTDIR", 'Set the root directory to install into ( /path/to/DESTDIR )', "")
opts.Add("LADSPA", 'Support LADSPA plugins', posix == True)
opts.Add("ASIO", 'Support ASIO', win32 == True)
opts.Add("SNDFILE", 'Support loading of waves via libsndfile', True)

env = Environment(ENV = os.environ)
opts.Update( env )
opts.Save('options.conf', env)
Help( opts.GenerateHelpText( env ) )

######################################
#
# settings
#
######################################

USE_LADSPA = env['LADSPA']
USE_SNDFILE = env['SNDFILE']
USE_ASIO = env['ASIO']

######################################
#
# build settings
#
######################################

CCFLAGS = ['-D__SCONS__']
LIBPATH = ['lib']
LIBS = []
LINKFLAGS=[]

if posix:
	CCFLAGS += [
		'-DPOSIX',
	]
	LINKFLAGS += [
		'--rpath${PREFIX}/lib',
		'--rpath-link${PREFIX}/lib',
	]
elif win32:
	CCFLAGS += [
		'-D_WIN32',
		'-DWIN32',
		'-DSTRICT',
		'-D_WINDOWS',
		'-D_WIN32_WINNT=0x0500',
		'-DNDEBUG',		
	]
	LIBS += [
		'kernel32',
		'user32',
	]

gcc = env['CC'] == 'gcc'
cl = env['CC'] == 'cl'

if gcc:
	env['CXXFLAGS'] += ['--no-rtti']

	CCFLAGS += ['-g','-Wall']

if cl:
	CCFLAGS += [
		'-nologo',
		'-MT',
		'-EHsc',
		'-W3',
	]

######################################
#
# install paths
#
######################################

binpath = "${DESTDIR}${PREFIX}/bin"
libpath = "${DESTDIR}${PREFIX}/lib"
pluginpath = "${DESTDIR}${PREFIX}/lib/zzub"
mutepath = "${DESTDIR}${PREFIX}/share/mute"
muterespath = "${DESTDIR}${PREFIX}/share/mute/res"

def install(target, source):
	env.Alias(target='install', source=env.Install(dir=target, source=source))

def install_lib(source):
	install(libpath, source)
def install_bin(source):
	install(binpath, source)
def install_plugin(source):
	install(pluginpath, source)

######################################
#
# dsplib shared library
#
######################################

files = """
dsplib.cpp
""".strip().split('\n')

files = ['dsplib/'+filename for filename in files]

dsplib_LIBS = LIBS[:]
dsplib = env.SharedLibrary(
	'lib/dsplib',
	files,
	CCFLAGS=CCFLAGS+['-DDI_EXPORT'],
	LIBS=dsplib_LIBS,
	CPPPATH=['buzz2zzub'],
	LINKFLAGS=LINKFLAGS)
install_lib(dsplib)

#######################################
#
# libzzub shared library
#
#######################################

class libzzub:
	files = glob.glob('libzzub/*.cpp')

	if win32:
		files.append('libzzub/libzzub.def')

	ccflags = CCFLAGS + ['-D_LIB','-D_LIBZZUB']	
	libs = LIBS[:]
	cpppath = []

	# include dsplib
	libs += ['dsplib']

	# neccessities
	if posix:
		libs += [
			'dl',
			'pthread',
		]
	elif win32:
		libs += ['oleaut32']

	if USE_SNDFILE:
		libs += [
			'sndfile',
		]
		ccflags += [
			'-DUSE_SNDFILE',
		]
		
	# add portaudio dependencies
	ccflags += [
		'-DPA_LITTLE_ENDIAN', # set this to make sure amd64 is happy
	]

	files += glob.glob('portaudio/src/common/*.c')
	for f in files:
		if 'pa_debugprint.c' in f:
			files.remove(f)

	if posix:
		files += glob.glob('portaudio/src/os/unix/*.c')
		files += glob.glob('portaudio/src/hostapi/alsa/pa_linux_alsa.c')
		files += glob.glob('portaudio/src/hostapi/oss/pa_unix_oss.c')
		files += glob.glob('portaudio/src/hostapi/jack/pa_jack.c')
		libs += [
			'asound',
			'pthread',
			'rt',
			'm',
			'jack',
		]
		cpppath += [
			'portaudio/include',
			'portaudio/src/common',
			'portaudio/src/os/unix',
		]
		ccflags += [
			'-DPA_USE_ALSA',
			'-DPA_USE_JACK',
			'-DPA_USE_OSS',
		]
		
	elif win32:
		files += glob.glob('portaudio/src/os/win/*.c')
		files += glob.glob('portaudio/src/hostapi/wmme/*.c')
		cpppath += [
			'portaudio/include',
			'portaudio/src/common',
		]
		ccflags += [
			'-DPA_NO_DS',
		]
		
		if USE_ASIO:
			files += glob.glob('portaudio/src/hostapi/asio/*.cpp')
			files += glob.glob('ASIOSDK2/common/asio.cpp')
			files += glob.glob('ASIOSDK2/host/asiodrivers.cpp')
			files += glob.glob('ASIOSDK2/host/pc/asiolist.cpp')	
			
			cpppath += [
				'ASIOSDK2/common',
				'ASIOSDK2/host',
				'ASIOSDK2/host/pc',
				'libsndfile',
			]
			
			libs += [
				'Advapi32.lib',
				'ole32.lib',
				'sndfile.lib',
			]
		else:
			ccflags += [
				'-DPA_NO_ASIO',
			]

	libzzub = env.SharedLibrary('lib/zzub',files,
		CCFLAGS=ccflags,
		LIBS=libs,
		LIBPATH=LIBPATH,
		CPPPATH=cpppath,
		LINKFLAGS =LINKFLAGS,
	)
	install_lib(libzzub)

#######################################
#
# zzub plugins
#
#######################################

class buzz2zzub:
	libname = "buzz2zzub"
	dir = "buzz2zzub"
	files = """
	buzz2zzub.cpp
	mdkimpl.cpp
	"""

class ladspadapter:
	libname = "ladspadapter"
	dir = "ladspadapter"
	files = """
	ladspadapter.cpp
	load.c
	search.c
	default.c
	"""

class Multi2:
	libname = "Joachims Multi 2"
	dir = "multi-2"
	files = """
	main.cpp
	"""

class MatildeTracker:
	libname = "Matilde Tracker"
	dir = "Matilde Tracker"	
	files = """
	BuzzInstrument.cpp
	BuzzSample.cpp
	Channel.cpp
	Envelope.cpp
	ExtResampler.cpp
	IInstrument.cpp
	ISample.cpp
	Track.cpp
	Tracker.cpp
	WavetableManager.cpp
	../SurfsDSPLib/SRF_2PFilter.cpp
	../SurfsDSPLib/SRF_Amp.cpp
	../SurfsDSPLib/SRF_AutoGain.cpp
	../SurfsDSPLib/SRF_DSP.cpp
	../SurfsDSPLib/SRF_Resampler.cpp
	"""

class Softy2:
	libname = "Softy2"
	dir = "Softy2"
	files = """
	Softy2.cpp
	"""

machines = [
	MatildeTracker,
	#Multi2, # not ready yet, but in store for 0.2
]

if USE_LADSPA:
	machines += [
		ladspadapter,
	]

if cl:
	machines += [
		buzz2zzub, # works only with vc		
	]

pluginlibs = {}

for machine in machines:
	files = ['plugins/'+machine.dir+'/'+filename.strip() for filename in machine.files.strip().split('\n')]
	if win32:
		files.append('plugins/zzub_plugin.def')

	pluginlibs[machine] = env.SharedLibrary('lib/zzub/'+machine.libname, files,
		CCFLAGS=CCFLAGS,
		LIBS=LIBS + ['dsplib'],
		LIBPATH=LIBPATH,
		CPPPATH=['libzzub/plugin','dsplib'],
		LINKFLAGS=LINKFLAGS,
	)
	install_plugin(pluginlibs[machine])

################################################
#
# bmxplay demo bmx player using the C interface
#
################################################

files = glob.glob('bmxplay/*.cpp') + glob.glob('bmxplay/*.c')

bmxplay_CCFLAGS = CCFLAGS[:]
bmxplay_LIBS = LIBS[:]

bmxplay = env.Program('bin/bmxplayer',files,
	CCFLAGS=bmxplay_CCFLAGS,
	LIBS=bmxplay_LIBS + ['zzub','dsplib'],
	LIBPATH=LIBPATH,
	LINKFLAGS=LINKFLAGS,
)
install_bin(bmxplay)

################################################
#
# mute tracker
#
################################################

for f in glob.glob('mute/*.py'):
	install(mutepath, f)
install(mutepath, 'test/gear/index.txt')
install_bin('mute/mute')
if win32:
	install_bin('mute/mute.bat')
	install_lib('mute/libsndfile-1.dll')

################################################
#
# mute resources
#
################################################

for f in glob.glob('mute/res/*.png'):
	install(muterespath, f)

