#!/usr/bin/python
import os
import glob
import sys

options = Options('options.cache', ARGUMENTS)
options.Add(PathOption('clam_prefix', 'The prefix where CLAM was installed', ''))

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

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

def unique(list) :
	return dict.fromkeys(list).keys()

env = Environment(tools=['default', 'qt'], options=options)
options.Save('options.cache', env)
env.Tool('clam', toolpath=['../sconstools'])

env.SConsignFile() # Single signature file

env['CXXFILESUFFIX'] = '.cxx'
env['QT_MOCHPREFIX'] = 'generated/moc_'
env['QT_UICDECLPREFIX'] = 'generated/ui_'
env['QT_UICDECLSUFFIX'] = '.hxx'
if (sys.platform == 'linux2') :
	env['QT_LIB'] = 'qt-mt'
if (sys.platform == 'win32') :
	env['QT_LIB'] = 'qt-mt322'


CLAMInstallDir = env['clam_prefix']

sourcePaths = []
#sourcePaths += recursiveDirs("..")
extraPaths = []
extraPaths += recursiveDirs("examples/utils")
extraPaths += [
	CLAMInstallDir+'/include',
	CLAMInstallDir+'/include/CLAM', # KLUDGE to keep old style includes	
]
includePaths = sourcePaths + extraPaths

sources = scanFiles('*.cxx', sourcePaths)
sources = unique(sources)

env.Append(CPPPATH=includePaths)

env.EnableClamModules(libs=[
	'clam_core',
	'clam_audioio',
	'clam_processing',
#	'clam_vmfl',
	'clam_vmqt',
	], path=CLAMInstallDir)

env.Append(CPPFLAGS=['-DFFTW_HEADER="<rfftw.h>"'])

singleSourceExamples = [
	"../../examples/NetworkPersistence_example.cxx",
	"../../examples/NetworkUsage_example.cxx",
#	"../../examples/AudioApplicationExample.cxx", # TODO: Windows lacks GUIAudioApplication
	"../../examples/AudioFileReading_example.cxx",
	"../../examples/AudioFileWriting_example.cxx",
	"../../examples/AudioIOExample.cxx",
	"../../examples/Configurators_example.cxx",
	"../../examples/DescriptorComputation_example.cxx",
	"../../examples/FDFilterExample.cxx",
	"../../examples/FFT_example.cxx",
	"../../examples/FileInfo_example.cxx",
	"../../examples/FilePlayback_example.cxx",
	"../../examples/FundamentalDetect.cxx",
#	"../../examples/LPCAnalysis_example.cxx", # TODO FLTK Dependencies
	"../../examples/Midi2XmlExample.cxx",
	"../../examples/MIDIFileReadWriteExample.cxx",
	"../../examples/MIDIIOExample.cxx",
	"../../examples/MIDIOut_example.cxx",
#	"../../examples/MIDI_Synthesizer_example/MIDI_Synthesizer_example.cxx", # TODO: Windows lacks GUIAudioApplication
	"../../examples/NetworkPersistence_example.cxx",
	"../../examples/NetworkUsage_example.cxx",
	"../../examples/PersistenceThroughDTs_example.cxx",
#	"../../examples/MultiPlot_example.cxx",
#	"../../examples/PlotExamplesHelper.cxx",
#	"../../examples/SinglePlot_example.cxx",
	"../../examples/POCompositeExample.cxx",
	"../../examples/PortsExamples/SimplePortsUsage_example.cxx",
	"../../examples/ProcessingLifeCycle_example.cxx",
	"../../examples/ProcessingObject_controls_example.cxx",
#	"../../examples/SDIF_And_Segment_example.cxx", # TODO: FLTK Dependencies
	"../../examples/Spectrum_example.cxx",
	"../../examples/StereoToMonoFile.cxx",
#	"../../examples/StreamBufferExample.cxx", # TODO: Deprecated?
#	"../../examples/ThreadedProcessing_example.cxx", # TODO: Depends on Resample
	"../../examples/Tutorial/UserTutorial1.cxx",
	"../../examples/Tutorial/UserTutorial2.cxx",
	"../../examples/Tutorial/UserTutorial3.cxx",
	"../../examples/Tutorial/UserTutorial4.cxx",
	"../../examples/Tutorial/UserTutorial5.cxx",
	"../../examples/Tutorial/UserTutorial6.cxx",
]
# TODO: Weird ones (libraries, client server...)
#	"../../examples/CLT/LadspaAudioMultiplier.cxx",
#	"../../examples/CLT/LadspaSimpleOscillator.cxx",
# TODO: Files to be integred on the Network editor
#	"../../examples/CLAMRemoteController/CLAMRemoteController.cxx",
#	"../../examples/CLAMRemoteController/LADSPAHandler.cxx",
#	"../../examples/NetworkLADSPAPlugin/LADSPAHandler.cxx",
#	"../../examples/NetworkLADSPAPlugin/NetworkLADSPAPlugin.cxx",
qtPlotUtils = [
	"../../examples/QtPlots/utils/analysis_utils.cxx",
	"../../examples/QtPlots/utils/audio_file_utils.cxx",
	"../../examples/QtPlots/utils/wave_utils.cxx",
]
qtPlotMains = [
	"../../examples/QtPlots/DirectPlotsExamples/DPAudioExample.cxx",
	"../../examples/QtPlots/DirectPlotsExamples/DPAudioStereoExample.cxx",
	"../../examples/QtPlots/DirectPlotsExamples/DPFundamentalExample.cxx",
	"../../examples/QtPlots/DirectPlotsExamples/DPMultiPlotExample.cxx",
	"../../examples/QtPlots/DirectPlotsExamples/DPSpectrumAndPeaksExample.cxx",
	"../../examples/QtPlots/QtPlotsExamples/QtAudioPlotExample.cxx",
	"../../examples/QtPlots/QtPlotsExamples/QtSinTracksPlotExample.cxx",
	"../../examples/QtPlots/QtPlotsExamples/QtSpectrogramExample.cxx",
	"../../examples/QtPlots/QtPlotsExamples/QtSpectrumPlotExample.cxx",
	"../../examples/QtPlots/SegmentEditorExample/SegmentEditorExample.cxx",
]
if sys.platform != 'win32':
	qtPlotMains += [
	"../../examples/QtPlots/BPFEditorExample/BPFEditorExample.cxx", #TODO: Windows needs PortMidi
	"../../examples/QtPlots/DirectPlotsExamples/MultiBPFViewerExample.cxx", #TODO: Windows needs PortMidi
	"../../examples/QtPlots/ListPlotExample/ListPlotExample.cxx", #TODO: Windows needs PortMidi
]

directoryBasedExamples = [
#	"../../examples/TickExtractor", # TODO: Files left to be compilable
	"../../examples/PortsAndControlsUsageExample",
	"../../examples/ControlArrayExamples",
]

examples = []

for main in singleSourceExamples :
	executable = os.path.splitext(os.path.basename(main))[0]
	examples += [ env.Program(target=executable, source = sources + [main] ) ]
	
env.AppendUnique(CPPPATH=["../../examples/QtPlots/utils"])
for main in qtPlotMains :
	executable = os.path.splitext(os.path.basename(main))[0]
	examples += [ env.Program(target=executable, source = sources + [main] + qtPlotUtils) ]

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


env.Alias('examples',examples)

env.Default(examples)



