#! /usr/bin/python

import os, glob, sys

moduleName='spectral'
version = "1.4.0"
description = "Barcelona Media contributed spacialization algorigthms for the CLAM framework"
url = 'http://clam-project.org'
requires = ['fftw3f'] # used for pc file
otherDependencies = [
	'fftw3f',
	]
cflags = []

options = Variables('options.cache', ARGUMENTS)
options.Add(PathVariable('prefix', 'Installation prefix (default /usr/local)', "/usr/local"))
options.Add(PathVariable('sandbox_path', 'Path where third party libraries were installed (in windows)', "", validator=PathVariable.PathAccept))
options.Add(BoolVariable('verbose', 'Display the full command line', 'no') )
options.Add(BoolVariable('crossmingw', 'Using MinGW crosscompiler mode', 'no') )
options.Add(BoolVariable('compile_test', 'Enable test compiling (adds dependency on mini_cpp_unit)', 'false') )
options.Add(BoolVariable('avoid_fftw', 'avoid/use fftw', 'false') )

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

sconstoolspath = '.'
if env['crossmingw'] :
	env.Tool('crossmingw', toolpath=[sconstoolspath])
env.Tool('pc', toolpath=[sconstoolspath])

tests = []
if env['compile_test'] :
	testEnv = env.Clone()
	testEnv.ParseConfig('pkg-config --cflags --libs mini_cppunit')
	testEnv.AppendUnique(
		LIBS=[moduleName],
		LIBPATH=['.'],
		)
	tests += [ testEnv.Program("spectral_unittests", Glob("*Test.cxx")) ]

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


# spectral lib
spectral_sources = [ x for x in glob.glob('*.cxx') if "Test" not in x ]
spectral_headers = Glob('*.hxx')

if env['avoid_fftw'] :
		env.Append(CPPDEFINES=['AVOID_FFTW'])
		requires= []
		otherDependencies = []
		cflags = ['-DAVOID_FFTW']
else: 
		env.ParseConfig('pkg-config --cflags --libs fftw3f')



spectral_lib = env.SharedLibrary('spectral',spectral_sources)

if sys.platform=="darwin" : #TODO fix. should be available in clamlibs pc
	env.Append( LIBPATH=['/opt/local/lib'] )
	env.Append(CPPFLAGS=['-arch', 'i386'] )
	env.Append(LINKFLAGS=['-arch', 'i386'] )

spectral_dll = []
if sys.platform=="win32" or 'crossmingw' in env['TOOLS'] :
	spectral_dll = spectral_lib[0:1]
	spectral_lib = spectral_lib[1:]



spectral_pcfile = env.PkgConfigFile(
	package='spectral',
	version=version,
	prefix=env['prefix'],
	description='spectral utilities',
	url=url,
	requires=requires,
	cflags=cflags
)

default = [spectral_lib, spectral_pcfile, tests]
install = [
	env.Install(os.path.join(env['prefix'],'bin'), spectral_dll),
	env.Install(os.path.join(env['prefix'],'lib'), spectral_lib),
	env.Install(os.path.join(env['prefix'],'lib', 'pkgconfig'), spectral_pcfile),
	env.Install(os.path.join(env['prefix'],'include', 'spectral'), spectral_headers),
	]

env.Alias('install', install)
env.Default(default)

