##############################################################################
#
# Copyright (c) 2003-2016 by The University of Queensland
# http://www.uq.edu.au
#
# Primary Business: Queensland, Australia
# Licensed under the Apache License, version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Development until 2012 by Earth Systems Science Computational Center (ESSCC)
# Development 2012-2013 by School of Earth Sciences
# Development from 2014 by Centre for Geoscience Computing (GeoComp)
#
##############################################################################

import os
Import('*')

local_env = env.Clone()
py_wrapper_local_env = env.Clone()

# Remove the shared library prefix on all platforms - we don't want 'lib'
# mucking with our python modules
del py_wrapper_local_env['SHLIBPREFIX']

sources = """
    DataVar.cpp
    EscriptDataset.cpp
    VisItControl.cpp
""".split()

headers = """
    DataVar.h
    DomainChunk.h
    ElementData.h
    EscriptDataset.h
    NodeData.h
    VisItControl.h
    vtkCellType.h
    weipa.h
""".split()

if 'dudley' in env['domains'] or 'finley' in env['domains']:
    sources += ['FinleyDomain.cpp','FinleyElements.cpp','FinleyNodes.cpp']
    headers += ['FinleyDomain.h','FinleyElements.h','FinleyNodes.h']
    if 'dudley' in env['domains']:
        local_env.Append(CPPDEFINES = ['USE_DUDLEY'])
        local_env.Prepend(LIBS = ['dudley'])
    if 'finley' in env['domains']:
        local_env.Append(CPPDEFINES = ['USE_FINLEY'])
        local_env.Prepend(LIBS = ['finley'])

if 'ripley' in env['domains']:
    sources += ['RipleyDomain.cpp','RipleyElements.cpp','RipleyNodes.cpp']
    headers += ['RipleyDomain.h','RipleyElements.h','RipleyNodes.h']
    local_env.Append(CPPDEFINES = ['USE_RIPLEY'])
    local_env.Prepend(LIBS = ['ripley'])

if 'speckley' in env['domains']:
    sources += ['SpeckleyDomain.cpp','SpeckleyElements.cpp','SpeckleyNodes.cpp']
    headers += ['SpeckleyDomain.h','SpeckleyElements.h','SpeckleyNodes.h']
    local_env.Append(CPPDEFINES = ['USE_SPECKLEY'])
    local_env.Prepend(LIBS = ['speckley'])

if local_env['visit']:
    sources.append(['VisItData.cpp'])
    headers.append(['VisItData.h'])
    local_env.Append(CPPDEFINES = ['USE_VISIT'])
    local_env.AppendUnique(LIBS = ['simV2'])

if not env['build_shared']:
    local_env.Prepend(LIBS = ['escript'])
else:
    local_env.Prepend(LIBS = ['escript', 'esysUtils'])

if local_env['silo']:
    local_env.Append(CPPDEFINES = ['USE_SILO'])
    local_env.AppendUnique(LIBS = env['silo_libs'])

if IS_WINDOWS:
    local_env.Append(CPPDEFINES = ['WEIPA_EXPORTS'])

module_name = 'weipa'

lib = local_env.SharedLibrary(module_name, sources)
env.Alias('build_weipa_lib', lib)

include_path = Dir('weipa', local_env['incinstall'])

hdr_inst = local_env.Install(include_path, headers)
env.Alias('install_weipa_headers', hdr_inst)

lib_inst = local_env.Install(local_env['libinstall'], lib)
env.Alias('install_weipa_lib', lib_inst)

### Python wrapper ###
py_wrapper_local_env.Prepend(LIBS = ['weipa'])
if 'dudley' in env['domains']:
    py_wrapper_local_env.Prepend(LIBS = ['dudley'])
if 'finley' in env['domains']:
    py_wrapper_local_env.Prepend(LIBS = ['finley'])
if 'ripley' in env['domains']:
    py_wrapper_local_env.Prepend(LIBS = ['ripley'])
if 'speckley' in env['domains']:
    py_wrapper_local_env.Prepend(LIBS = ['speckley'])

if not env['build_shared']:
    py_wrapper_local_env.Prepend(LIBS = ['escript'])
else:
    py_wrapper_local_env.Prepend(LIBS = ['escript', 'esysUtils'])

py_wrapper_name = module_name + 'cpp'
py_wrapper_lib = py_wrapper_local_env.SharedLibrary(py_wrapper_name, 'weipacpp.cpp')
env.Alias('build_weipacpp_lib', py_wrapper_lib)

tmp_inst = os.path.join(local_env['pyinstall'], module_name)
if IS_WINDOWS:
    wrapper_ext = '.pyd'
else:
    wrapper_ext = '.so'

share_name = os.path.join(tmp_inst, py_wrapper_name+wrapper_ext)
mod_inst = py_wrapper_local_env.InstallAs(target=share_name,
                                          source=py_wrapper_lib[0])
env.Alias('install_weipacpp_lib', mod_inst)

######################
### Plugin library ###
######################
visitplugin_env = env.Clone()
plugin_sources = """
    DataVar.cpp
    EscriptDataset.cpp
    FinleyDomain.cpp
    FinleyElements.cpp
    FinleyNodes.cpp
""".split()

visitplugin_env.Prepend(LIBS = ['esysUtils'])
visitplugin_env.Append(CPPDEFINES = ['VISIT_PLUGIN', 'USE_FINLEY'])
visitplugin_env['OBJPREFIX']='reader_'

if env['build_shared']:
    plugin_lib = visitplugin_env.SharedLibrary('escriptreader', plugin_sources)
else:
    if IS_WINDOWS:
        visitplugin_env.Append(CPPDEFINES = ['WEIPA_STATIC_LIB'])
    plugin_lib = visitplugin_env.StaticLibrary('escriptreader', plugin_sources)
env.Alias('build_escriptreader_lib', plugin_lib)

tmp = local_env.Install(local_env['libinstall'], plugin_lib)
env.Alias('install_escriptreader_lib', tmp)

# configure python module
local_env.SConscript(dirs = ['#/weipa/py_src'], variant_dir='py', duplicate=0, exports=['py_wrapper_lib'])

# configure unit tests
local_env.SConscript(dirs = ['#/weipa/test'], variant_dir='test', duplicate=0)

