import os
Import('env')

#  
# Need to make a copy because SCons uses the environment
# at it's final state, so StGermain ends up depending on
# StgDomain, etc..
#

env = env.Clone()
env['CURR_PROJECT'] = "Underworld"

#
# Inside each project we will be accessing headers without the
# project name as a prefix, so we need to let SCons know how to
# find those headers.
#

env.Append(CPPPATH=env['build_dir'] + '/include/Underworld')

#
# Need to include the Underworld library for binaries/plugins.
#

libs = ['Underworld'] + env.get('LIBS', [])

# Keep a list of all the objects we build so we can make a library
# afterwards.
objs = []
suites = []
tst_exp = []
tst_input = []

#
# Process each directory uniformly.
#

dirs = Split('Function Rheology Utils/AdvectionDiffusion Utils libUnderworld')
for d in dirs:
    # Need the module name, which is just the directory.
    mod_name = env['ESCAPE']('"' + ''.join(d.split('/')) + '"')
    version_name = 'UNDERWORLD_VERSION'
    cpp_defs = [('CURR_MODULE_NAME', mod_name)] + env.get('CPPDEFINES', [])

    # Setup where to look for files.
    src_dir = d + '/src'
    inc_dir = 'include/Underworld/' + d
    tst_dir = d + '/tests'
    tst_exp_dir = tst_dir + '/expected'
    tst_input_dir = tst_dir + '/input'
    tst_install_dir = 'tests/Underworld/' + d

    # Install the headers and '.def' files.
    env.Install(os.path.join(env['build_dir'],inc_dir), Glob(src_dir + '/*.h'))
    env.Install(os.path.join(env['build_dir'],inc_dir), Glob(src_dir + '/*.hpp'))

    # Build our source files.
    srcs = Glob(src_dir + '/*.c') +  Glob(src_dir + '/*.cpp')
    for src in srcs:
        stg_cpp_defs = cpp_defs + [('SOURCE_FILE', '"\\"' + src.name + '\\""')]
        objs += env.SharedObject(src, CPPDEFINES=stg_cpp_defs)

    # Build any test suites we might find.
    # suites += env.Object(Glob(tst_dir + '/*Suite.c'))

    # Install any test expected and input files
    # tst_exp += env.Install(os.path.join(env['build_dir'],tst_install_dir,'/expected'), Glob(tst_exp_dir + '/*'))
    # tst_input += env.Install(os.path.join(env['build_dir'],tst_install_dir,'/input'), Glob(tst_input_dir + '/*'))

dirs = [
        'SysTest/AnalyticPlugins/Velic_solA',
        'SysTest/AnalyticPlugins/Velic_solB',
        'SysTest/AnalyticPlugins/Velic_solC',
        'SysTest/AnalyticPlugins/Velic_solCA',
        'SysTest/AnalyticPlugins/Velic_solCx',
        'SysTest/AnalyticPlugins/Velic_solDA',
#        'SysTest/AnalyticPlugins/Velic_solE',
#        'SysTest/AnalyticPlugins/Velic_solF',
#        'SysTest/AnalyticPlugins/Velic_solG',
        'SysTest/AnalyticPlugins/Velic_solH',
        'SysTest/AnalyticPlugins/Velic_solHA',
#        'SysTest/AnalyticPlugins/Velic_solHAy',
#        'SysTest/AnalyticPlugins/Velic_solHy',
#        'SysTest/AnalyticPlugins/Velic_solI',
#        'SysTest/AnalyticPlugins/Velic_solIA',
#        'SysTest/AnalyticPlugins/Velic_solJ',
#        'SysTest/AnalyticPlugins/Velic_solJA',
        'SysTest/AnalyticPlugins/Velic_solKx',
        'SysTest/AnalyticPlugins/Velic_solKz',
#        'SysTest/AnalyticPlugins/Velic_solL',
#        'SysTest/AnalyticPlugins/Velic_solS',
        ]

for d in dirs:
    # Need tee module name, which is just the directory.
    mod_name = env['ESCAPE']('"' + ''.join(d.split('/')) + '"')
    version_name = 'UNDERWORLD_VERSION'
    cpp_defs = [('CURR_MODULE_NAME', mod_name)] + env.get('CPPDEFINES', [])

    # Setup where to look for files.
    src_dir = d
    inc_dir = 'include/Underworld/' + d

    # Install the headers and '.def' files.
    env.Install(os.path.join(env['build_dir'],inc_dir), Glob(src_dir + '/*.h'))

    # Build our source files.
    srcs = Glob(src_dir + '/*.c') +  Glob(src_dir + '/*.cpp')
    for src in srcs:
        stg_cpp_defs = cpp_defs + [('SOURCE_FILE', '"\\"' + src.name + '\\""')]
        objs += env.SharedObject(src, CPPDEFINES=stg_cpp_defs)


# Need to install headers from libUnderworld.
env.Install(os.path.join(env['build_dir'],'include/Underworld'), Glob('libUnderworld/src/*.h'))

#
# Build plugins.
#
# Added an Import/export command so that items added here are added to env and can be used by other 
# scons executables. Added it as Dictionaries so that the Toolbox ID can be preserved.
# --KathleenHumble -20-08-2010

pluginDirs  = ['libUnderworld/Toolbox',
         ]


pl_objs = []
pl_regs = []
for d in pluginDirs:

    name = 'Underworld_' + d.split('/')[-1] + 'module'
    mod_name = env['ESCAPE']('"' + ''.join(d.split('/')) + '"')
    cpp_defs = [('CURR_MODULE_NAME', mod_name)] + env.get('CPPDEFINES', []) 
    
    env.Install(os.path.join(env['build_dir'],'include/Underworld/',d.split('/')[-1]), Glob(d + '/*.h'))

    srcs = Glob(d + '/*.c')
    cur_objs = env.SharedObject(srcs, CPPDEFINES=cpp_defs)

    # If we have shared libraries, build the dynamic plugin.
    if env['shared_libs']:
        lib_pre = env['LIBPREFIXES']
        if not isinstance(lib_pre, list):
            lib_pre = [lib_pre]
        env.SharedLibrary('lib/' + name, cur_objs,
                          SHLIBPREFIX='',
                          LIBPREFIXES=lib_pre + [''],
                          LIBS=libs)

    # If we are building static libs we need to construct a C file
    # mapping the plugin's name to its register function.
    if env['static_libs']:
        pl_regs += [name]

    # Keep track of all the plugin objects.
    pl_objs += cur_objs

#
# Build shared library.
#

if env['shared_libs']:
    env.SharedLibrary('lib/Underworld', objs)
    env.SharedLibrary(env['prefix'] + '/lib/Underworld', objs)
