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

sys.exit()  #PAU: added this to get windows in green for 1.0!!!!!!!!!!!!!!!!!!!!!


version='testing-version'
options = Options('options.cache', ARGUMENTS)
options.Add(PathOption('install_prefix', 'The prefix where the networkeditor will be installed', ''))
options.Add(PathOption('clam_prefix', 'The prefix where CLAM was installed', ''))
options.Add(PathOption('vstsdk_path', 'The path to vstsdk (root dir)', ''))
options.Add(BoolOption('release', 'Build CLAM Vst plugin enabling compiler optimizations', 'no') )
options.Add(BoolOption('verbose', 'Display the full command line instead a short command description', 'no') )


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


env = Environment(ENV=os.environ, tools=['default'], options=options)
options.Save('options.cache', env)

env.Help(options.GenerateHelpText(env))

env.SConsignFile() # Single signature file

CLAMInstallDir = env['clam_prefix']

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

env['CXXFILESUFFIX'] = '.cxx'
if not env['verbose']:
	env['CXXCOMSTR'] = '== Compiling $SOURCE'
	env['LINKCOMSTR'] = '== Linking $TARGET'
	env['SHLINKCOMSTR'] = '== Linking library $TARGET'

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

env.EnableQt4Modules([
	'QtCore',
	'QtGui',
	'QtOpenGL',
#	'QtSql',
#	'QtNetwork',
#	'QtTest',
	'QtXml',
#	'QtSvg',
	'QtUiTools',
#	'QtDesigner',
#	'Qt3Support',
	], debug=False)

#vstsdk_path ='F:\\clam-external-libs\\vstsdk2.3'
vstsdk_common_path = os.path.join( env['vstsdk_path'], 'source', 'common' )

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

includePaths = sourcePaths + extraPaths

#commonSources = scanFiles('*.cpp', os.path.join(vstsdk_common_path, 'ADelay')
#commonSources = scanFiles('*.cpp', '.')
commonSources = ["CLAMVstPlugin.cpp"]
commonSources.append( os.path.join(vstsdk_common_path, 'AudioEffect.cpp') )
commonSources.append( os.path.join(vstsdk_common_path, 'audioeffectx.cpp') )


#env.Append(LIBS=['vstgui'])
env.Append(CPPPATH=includePaths)
env.Append(CPPFLAGS='-DRESOURCES_BASE="\\"' + env['install_prefix'] + '/share/networkeditor\\""')
env.Append(CPPDEFINES=[ ('FFTW_HEADER','"<rfftw.h>"') ])

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

env['WIN32_INSERT_DEF'] = 1
if env['release'] :
	env.Append(CPPDEFINES=['NDEBUG'])
else :
	env.Append(CPPDEFINES=['_DEBUG'])
#	env.Append( LINKFLAGS=['-rdynamic'] ) # TODO: Is it needed for MacOSX?

envConsole = env.Copy()
envPlugin = env.Copy()

if sys.platform == "win32" :
	envPlugin.Append( LINKFLAGS=['/subsystem:windows','/machine:i386'] )

programs = []
programs += [ envConsole.Program(target="TestPlugin", source = commonSources+["TestPlugin.cxx"]) ]

vstplugin = envPlugin.SharedLibrary("CLAMVstPlugin", source = commonSources+["CLAMVstPluginMain.cpp"] )
#vstplugin = envPlugin.SharedLibrary("ADelay", source = commonSources )


# Manual step: lupdate-
installation = {
	'/bin' : programs,
	'/bin' : vstplugin,
}

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


if sys.platform=='macosx' : # not really tested!!!
	env.Append(CPPFLAGS='-DRESOURCES_BASE="\\"CLAMVst.app/Contents/Resources\\""')
	#Binaries: networkeditor + sample extractor
	env.AppendUnique( LINKFLAGS=['-dynamic','-bind_at_load'])

	#Resource installation in Mac application directory (binaries, xml metadata, icon, sound)
	installTargets = [
		env.Install( 'CLAMVst.app/Contents/MacOS', programs ),
		env.Install( 'CLAMVst.app/Contents', 'resources/Info.plist'),
		env.Install( 'CLAMVst.app/Contents/Resources', 'resources/CLAM.icns'),
		]
	mac_packages = env.Dmg('notused', installTargets )
	env.Alias('package', mac_packages)

envPlugin.Alias('install', installTargets )

envPlugin.Default([vstplugin])
envConsole.Default(programs)
