#! /usr/bin/python

import os, glob, sys

baseName = 'continuous_excitation_synth'
libraryName='plugins_'+baseName

def scanFiles(pattern, paths) :
	files = []
	for path in paths :
		files+=glob.glob(os.path.join(path,pattern))
	return files

def recursiveDirs(root) :
	return filter( (lambda a : a.rfind( ".svn")==-1 ),  [ a[0] for a in os.walk(root)]  )

options = Options('options.cache', ARGUMENTS)
options.Add(PathOption('clam_prefix', 'The prefix where CLAM was installed', ''))
options.Add(BoolOption('crossmingw', 'Using MinGW crosscompiler mode', 'no') )
env = Environment(ENV=os.environ, options=options)
options.Save('options.cache', env)
Help(options.GenerateHelpText(env))
env.SConsignFile() # Single signature file

CLAMInstallDir = env['clam_prefix']
clam_sconstoolspath = os.path.join(CLAMInstallDir,'share','clam','sconstools')
if env['crossmingw'] :
	env.Tool('crossmingw', toolpath=[clam_sconstoolspath])
env.Tool('clam', toolpath=[clam_sconstoolspath])
env.EnableClamModules([
	'clam_core',
	'clam_audioio',
	'clam_processing',
	] , CLAMInstallDir)

sourcePaths = recursiveDirs(".")
print sourcePaths
sources = glob.glob('*.cxx')
sources += glob.glob('toreview/*.cxx')
extraPaths = [
	CLAMInstallDir+'/include',
	CLAMInstallDir+'/include/CLAM', # KLUDGE to keep old style includes
	".",
]

env.Append(CPPPATH=extraPaths)
sources = dict.fromkeys(sources).keys() # eliminate repeated
print sources

if sys.platform=='linux2' :
	env.Append( CCFLAGS=['-g','-O3','-Wall'] )
if sys.platform=="darwin" : #TODO fix. should be available in clamlibs pc
	env.Append( LIBPATH=['/opt/local/lib'] )
	env.Append( LIBS=['fftw3'] )
	


mainSources = {
	'ContinuousExcitationSynthesizer': 'ContinuousExcitationSynthesizer.cxx',
	'TestAudioDatabaseReader': 'toreview/TestAudioDatabaseReader.cxx',
	'EbowSynthesizer': 'toreview/ebowSynthesizer.cxx',
}
for binary, mainSource in mainSources.items() :
	sources.remove(mainSource)

programs = [ env.Program(target=program, source = [mainSource] + sources)
	for program, mainSource in mainSources.items()]

library = env.SharedLibrary(target=libraryName, source = sources)

install = env.Install(os.path.join(CLAMInstallDir,'lib','clam'), library) #TODO maybe install binaries?
env.Alias('install', install)

env.Default(programs + [library])

