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

moduleName='continuous_excitation_synth'
version = "1.4.0"
description = "Continuous excitation instrument synthesizer 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(BoolVariable('crossmingw', 'Using MinGW crosscompiler mode', 'no') )

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)

isWindowsPlatform = sys.platform=="win32" or env['crossmingw']

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

# Sources and headers
mainSources = {
	'ContinuousExcitationSynthesizer': os.path.join('.','ContinuousExcitationSynthesizer.cxx'),
#	'TestAudioDatabaseReader': os.path.join('toreview','TestAudioDatabaseReader.cxx'),
#	'EbowSynthesizer': os.path.join('toreview','ebowSynthesizer.cxx'),
}
sourcePaths = ["."]
sources = env.scanFiles('*.cxx', sourcePaths,
	)
print sources, mainSources.values()
headers = env.scanFiles('*.hxx', sourcePaths)
env.AppendUnique(CPPPATH=sourcePaths)

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

#if isWindowsPlatform :
#        env.AppendUnique(LINKFLAGS=['-Wl,--export-all-symbols'])

for binary, mainSource in mainSources.items() :
	sources.remove(mainSource)
env.Prepend(LIBPATH=["."])
programs = [ env.Program(target=program, source = [mainSource], LIBS='clam_'+moduleName)
	for program, mainSource in mainSources.items()]

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

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

# TODO: Execute that command as part of the scons
# help2man -N -n 'OSC controlled synthesizer based on Spectral Modelling techniques' ./ContinuousExcitationSynthesizer > ContinuousExcitationSynthesizer.1
manpages = [ program+".1" for program in mainSources.keys() ]

install+= [
	env.Install(os.path.join(env['prefix'],'bin'), programs),
	env.Install(os.path.join(env['prefix'],'share','networkeditor','example-data'), examples),
	env.Install(os.path.join(env['prefix'],'share','man','man1'), manpages),
	]
env.Alias('install', install)
env.Default(programs, default)

