#! /usr/bin/python

import os, glob, sys

bundlename = "clam_lv2_example.lv2"

libraryName='clam_lv2_example'

options = Variables('options.cache', ARGUMENTS)
options.Add(PathVariable('clam_prefix', 'The prefix where CLAM was installed', ''))
options.Add(BoolVariable('verbose', 'Display the full command line instead a short command description', 'no') )
options.Add(BoolVariable('crossmingw', 'Using MinGW crosscompiler mode', 'no') )

bundlePath=os.path.expanduser(os.path.join("~/.lv2",bundlename))

toolChain = 'default'
if sys.platform == 'win32': toolChain = 'mingw'
env = Environment(ENV=os.environ, tools=[toolChain], options=options)
env.Tool("qt4", toolpath=["../../../scons/sconstools"])
env.EnableQt4Modules("QtGui QtUiTools".split())
env.ParseConfig('pkg-config gtk+-2.0 --libs --cflags')
#env.ParseConfig('pkg-config liblo --libs --cflags')
env['SHOBJPREFIX']='generated/'
env['QT4_MOCHPREFIX']='generated/'

options.Save('options.cache', env) # do not save options in NetworkEditor generated plugins
Help(options.GenerateHelpText(env))
env.SConsignFile() # Single signature file

CLAMInstallDir = env['clam_prefix']
clam_sconstoolspath = os.path.join(CLAMInstallDir,'share','clam','sconstools')
if env['crossmingw'] :
	env.Tool('crossmingw', toolpath=[clam_sconstoolspath])
env.Tool('clam', toolpath=[clam_sconstoolspath])
env.moveIntermediateInto('generated')
env.activateColorCommandLine()
if not env['verbose']: env.ClamQuietCompilation()
env.EnableClamModules([
	'clam_core',
	'clam_audioio',
	'clam_processing',
	'clam_spacialization',
	] , CLAMInstallDir)

networks = glob.glob("*.clamnetwork")

env['CLAMLV2'] = os.path.join(env['clam_prefix'],'bin','clam_lv2_generator')
env['CLAMLV2_BASEURI'] = "http://clam-project.org/examples/lv2"

env.Uic4(glob.glob("*.ui"))

manifest = env.Command("manifest.ttl", networks,
	"${CLAMLV2} --manifest "
		"-u ${CLAMLV2_BASEURI} "
		"${SOURCES} > ${TARGET}"
	)

mainSource = env.Command("clam_plugin.cxx", networks,
	"${CLAMLV2} --main "
		"-u ${CLAMLV2_BASEURI} "
		"${SOURCES} > ${TARGET}"
	)
ttls = [
	env.Command(os.path.splitext(os.path.basename(network))[0]+".ttl", network,
	"${CLAMLV2} --ttl "
		"-u ${CLAMLV2_BASEURI} "
		"--binary ${CLAMLV2_BINARY} "
		"${CLAMLV2_GUIBINARY and '--gui-binary '+CLAMLV2_GUIBINARY or ''} "
		"${CLAMLV2_DOAPFILE and ('-d '+CLAMLV2_DOAPFILE) or ''} "
		"${SOURCE} > ${TARGET}",
	CLAMLV2_BINARY=libraryName,
	CLAMLV2_GUIBINARY=libraryName
#	CLAMLV2_DOAPFILE="default.doap",
	)
	for network in networks ]


sources = Glob("*.cxx")

env.Append( CCFLAGS=['-g','-O3','-Wall'] )
libraries = [
	env.SharedLibrary(target=libraryName, source = sources, SHLIBPREFIX=''),
	]

install = env.Install(bundlePath, [libraries,ttls,manifest]) + env.Install(bundlePath, glob.glob("*ui"))

env.Alias('install', install)
env.Command('uninstall', [], [Delete(FindInstalledFiles()),Delete(bundlePath)])
env.Default(libraries, ttls, manifest)




