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

options = Variables('options.cache', ARGUMENTS)
options.Add(PathVariable('prefix', 'The prefix where the application will be installed', '/usr/local'))
options.Add(PathVariable('clam_prefix', 'The prefix where CLAM was installed', '/usr/local'))
options.Add(BoolVariable('release', 'Enabling compiler optimizations', 'no') )
options.Add(PathVariable('vstsdk_path', 'The path to vstsdk (root dir)', ''))
options.Add(BoolVariable('verbose', 'Display the full command line instead a short command description', 'no') )
if sys.platform=="linux2" :
	options.Add(BoolVariable('crossmingw', 'Using MinGW crosscompiler mode', 'no') )

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)]  )


toolchain='default'
if sys.platform == 'win32': toolchain = 'mingw'
env = Environment(ENV=os.environ, tools=[toolchain], options=options)
options.Save('options.cache', env)
Help(options.GenerateHelpText(env))

env.SConsignFile() # Single signature file

crosscompiling = env.has_key("crossmingw") and env["crossmingw"]
isWindowsPlatform = sys.platform=='win32' or crosscompiling
isLinuxPlatform = sys.platform=='linux2' and not crosscompiling
isDarwinPlatform = sys.platform=='darwin'

CLAMInstallDir = env['clam_prefix']
clam_sconstoolspath = os.path.join(CLAMInstallDir,'share','clam','sconstools')

env.Tool('qt4', toolpath=[clam_sconstoolspath])
env.Tool('clam', toolpath=[clam_sconstoolspath])
env.Tool('nsis', toolpath=[clam_sconstoolspath])
if sys.platform=='darwin' : env.Tool('bundle', toolpath=[clam_sconstoolspath])
env.Tool('dmg', toolpath=[clam_sconstoolspath])
if crosscompiling :
	env.Tool('crossmingw', toolpath=[clam_sconstoolspath])
sys.path.append(clam_sconstoolspath)

env['CXXFILESUFFIX'] = ['.cxx']
env['QT4_UICDECLSUFFIX'] = '.hxx'
env['QT4_MOCHPREFIX'] = os.path.join('generated','moc_')
env['QT4_UICDECLPREFIX'] = os.path.join('generated','uic_')
env['QT4_QRCCXXPREFIX'] = os.path.join('generated','qrc_')
if not env['verbose']:
	env['CXXCOMSTR'] = '== Compiling $SOURCE'
	env['SHCXXCOMSTR'] = '== Compiling shared $SOURCE'
	env['LINKCOMSTR'] = '== Linking $TARGET'
	env['SHLINKCOMSTR'] = '== Linking library $TARGET'
	env['QT4_RCCCOMSTR'] = '== Embeding resources $SOURCE'
	env['QT4_UICCOMSTR'] = '== Compiling interface $SOURCE'
	env['QT4_LRELEASECOMSTR'] = '== Compiling translation $TARGET'
	env['QT4_MOCFROMHCOMSTR'] = '== Generating metaobjects for $SOURCE'
	env['QT4_MOCFROMCXXCOMSTR'] = '== Generating metaobjects for $SOURCE'

env.EnableClamModules([
	'clam_core',
	'clam_audioio',
	'clam_processing',
	], CLAMInstallDir)

env.EnableQt4Modules([
	'QtCore',
	'QtGui',
	'QtOpenGL',
	'QtXml',
	'QtSvg',
	'QtUiTools',
	],
	debug=False,
	crosscompiling=crosscompiling,
	)


vstsdk_common_path = os.path.join( env['vstsdk_path'], 'public.sdk', 'source', 'vst2.x' )
vstgui_path = os.path.join( env['vstsdk_path'], 'vstgui.sf', 'vstgui' )

sourcePaths = [
	os.path.join('.'),
	os.path.join( vstsdk_common_path )
]
extraPaths = [
	env['vstsdk_path'],
	vstgui_path,
	vstsdk_common_path,
	CLAMInstallDir+'/include',
	CLAMInstallDir+'/include/CLAM', # KLUDGE to keep old style includes
]
includePaths = sourcePaths + extraPaths

commonSources = scanFiles("*.cpp", [vstsdk_common_path])
vstguiSources = scanFiles("*.cpp", [vstgui_path])

#env.Append(LIBS=['vstgui'])
env.Append(CPPPATH=includePaths)
env.Append(CPPFLAGS='-DRESOURCES_BASE="\\"' + env['prefix'] + '/share/networkeditor\\""')

env.Append(CPPFLAGS=['-D_USE_MATH_DEFINES']) # to have M_PI defined TESTING - bug-fix: Load Simple Clam Networks

env.AppendUnique(LIBS = [
	'ole32',
	'gdi32',
	'uuid',
	'comdlg32',
])

if env['release'] :
	env.Append(CPPDEFINES=['NDEBUG'])
	env.Append( CCFLAGS=['-g','-O3','-fomit-frame-pointer','-Wall','-pipe'] )
else :
	env.Append(CPPDEFINES=['_DEBUG'])
	env.Append( CCFLAGS=['-g','-O3','-Wall','-pipe'] )

plugins = [
	env.SharedLibrary("CLAMVstPlugin", source = commonSources + [
		"CLAMVstPlugin.cxx",
		"VstNetworkExporter.cxx",
		"QClamVstEditor.cxx",
		"Files.s",
		]),
#	# A VstGui based plugin would need that
#	env.SharedLibrary("CLAMVstPlugin", source = commonSources + vstguiSources [
#		"CLAMVstGuiPlugin.cxx",
#		"CLAMVstGuiEditor.cxx",
#		env.RES("CLAMVstGuiEditor.rc"),
#	# A Vst Qt based plugin would need that
#	env.SharedLibrary("CLAMVstPlugin", source = commonSources + [
#		"CLAMVstQtlugin.cxx",
#		"CLAMVstQtditor.cxx",
#		env.Qrc("CLAMVstQtEditor.qrc"),
#		]),
]

installation = {
	'/bin' : plugins,
}

installTargets = [
	env.Install( env['prefix']+path, files ) for path, files in installation.items() ]

env.Alias('install', installTargets )
env.Default([plugins])

