#!/usr/bin/env python

import os, glob, sys

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

options = Variables('options.cache', ARGUMENTS)
options.Add(PathVariable('clam_prefix', 'The prefix where CLAM was installed', ''))
options.Add(PathVariable('prefix', 'Installation prefix (normally /usr, by default this is clam_prefix)', "", validator=PathVariable.PathAccept))
options.Add(BoolVariable('crossmingw', 'Using MinGW crosscompiler mode', 'no') )
options.Add(BoolVariable('verbose', 'Display the full command line instead a short command description', 'yes') )


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']
InstallDir = env['prefix'] or 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([
	'clam_core',
	'clam_audioio',
	'clam_processing',
	] , CLAMInstallDir)

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 = {
	'SimpleOboeSynthesizer': 'SimpleOboeSynthesizer.cxx',
#	'TestAudioDatabaseReader': 'toreview/TestAudioDatabaseReader.cxx',
#	'EbowSynthesizer': 'toreview/ebowSynthesizer.cxx',
#	'lookMaker': 'loopMaker/loopMaker.cxx',
}
for binary, mainSource in mainSources.items() :
	sources.remove(mainSource)

programs = [ env.Program(target=program, source = [mainSource] + sources)
	for program, mainSource in mainSources.items()]
libraries = [
	env.SharedLibrary(target=libraryName, source = sources)
	]
	
directoryBasedExamples = [
#	"TickExtractor", # TODO: Files left to be compilable
#	"PortsAndControlsUsageExample",
#	"ControlArrayExamples",
#	"Wav2SDIF",
#	"SDIF2Wav",
#	"SDIF2WavStreaming",
	"loopMaker",
]

examples = []
for folder in directoryBasedExamples :
	exampleSources = glob.glob(folder+"/*.cxx")
	executable = os.path.basename(folder)+"Exe"
	examples += [ env.Program(target=executable, source = sources + exampleSources ) ]


examples = [
	'synth_with_osc.clamnetwork',
	'synth_with_sliders.clamnetwork',
	]

install = [
	env.Install(os.path.join(InstallDir,'lib','clam'), libraries),
	env.Install(os.path.join(InstallDir,'bin'), programs),
	env.Install(os.path.join(InstallDir,'share','networkeditor','example-data'), examples)
	]
env.Alias('install', install)
env.Default(programs + libraries)

