#! /usr/bin/python

import os, glob, sys

moduleName='spacialization'
version = "1.4.0"
description = "Barcelona Media contributed spacialization algorigthms for the CLAM framework"
url = 'http://clam-project.org'
clamDependencies = [
	'clam_core',
	'clam_audioio',
	'clam_processing',
	]
otherDependencies = [
	]

options = Variables('options.cache', ARGUMENTS)
options.Add(PathVariable('clam_prefix', 'The prefix where CLAM was installed', '/usr/local'))
options.Add(PathVariable('prefix', 'Installation prefix (normally /usr, by default this is clam_prefix)', "", validator=PathVariable.PathAccept))
options.Add(BoolVariable('verbose', 'Display the full command line', 'no') )
options.Add(PathVariable('sandbox_path', 'Path where third party libraries were installed (in windows)', "", validator=PathVariable.PathAccept))
options.Add(BoolVariable('crossmingw', 'Using MinGW crosscompiler mode', 'no') )
options.Add(EnumVariable('raytracing', 'Linking with Barcelona Media raytracing (not GPL)', 'disabled',
	allowed_values= ('convergent', 'traditional', 'disabled') ) )

toolChain = 'mingw' if sys.platform == "win32" else 'default'
env = Environment(ENV=os.environ, tools=[toolChain], options=options)
options.Save('options.cache', env)
Help(options.GenerateHelpText(env))
env.SConsignFile() # Single signature file

CLAMInstallDir = env['clam_prefix']
if not env['prefix'] : env['prefix'] = env['clam_prefix']

clam_sconstoolspath = os.path.join(CLAMInstallDir,'share','clam','sconstools')
if not os.access(os.path.join(clam_sconstoolspath,"clam.py"),os.R_OK):
	raise Exception("CLAM not installed at '%s'. Use clam_prefix option."%CLAMInstallDir)
if env['crossmingw'] :
	env.Tool('crossmingw', toolpath=[clam_sconstoolspath])
env.Tool('clam', toolpath=[clam_sconstoolspath])
env.EnableClamModules(clamDependencies, CLAMInstallDir)

env.Append( CCFLAGS=['-g','-O3','-Wall'] )

# Sources and headers
sourcePaths = ["src"]
sources = env.scanFiles('*.cxx', sourcePaths)
headers = env.scanFiles('*.hxx', sourcePaths)
env.AppendUnique(CPPPATH=sourcePaths)

if env['raytracing']=='traditional' or env['raytracing']=='convergent':
	print 'linking with raytracing'
	env.Append(CPPPATH=[os.path.join('..','..','..','..','acustica','raytracing')])
	env.Append(LIBPATH=[os.path.join('..','..','..','..','acustica','raytracing')])
	env.Append(LIBS=['raytracing',"GL","GLU","lo",'glut'])
	env.ParseConfig('pkg-config --libs sdl')
else :
	sources.remove(os.path.join('src','RoomImpulseResponseSimulator.cxx'))
	sources.remove(os.path.join('src','EnvironmentManager.cxx'))
	sources.remove(os.path.join('src','OcclusionFilterControl.cxx'))

if sys.platform=="darwin" : #TODO fix. should be available in clamlibs pc
	env.Append( LIBPATH=['/opt/local/lib'] )
	env.Append( LIBS='sndfile' )
if sys.platform=="win32" or 'crossmingw' in env['TOOLS'] :
	env.Append( LIBPATH=[os.path.join(env['sandbox_path'],'local','lib')] )
	env.Append( LIBS=['fftw3'] )

install, default = env.ClamModule(
	moduleName,
	version,
	description = description,
	url = url,
	sources = sources,
	headers = headers,
	clamDependencies = clamDependencies,
	otherDependencies = otherDependencies,
	)

tests = [
	env.Program("UnitTests", env.scanFiles("*.cxx", ['tests'])),
	]
# TODO: This is a hack until LD_LIBRARY_PATH environment is nicely propagated
ld_library_path = ""
if env['ENV'].has_key("LD_LIBRARY_PATH") :
	ld_library_path = "LD_LIBRARY_PATH=%s "%env['ENV']['LD_LIBRARY_PATH']

runTests = Command("UnitTests.passed",tests, "%s ./UnitTests"%ld_library_path)

examples = env.scanFiles('*.clamnetwork', ['example-data'])
examples+= env.scanFiles('*.ui', ['example-data'])


install += [
#	env.Install(os.path.join(env['prefix'],'share','networkeditor','example-data'), examples)
	]

env.Alias('install', install)
env.Alias('tests', runTests)
env.Default(default)


