##############################################################################
#
# Copyright (c) 2003-2026 by the esys.escript Group
# https://github.com/LutzGross/esys-escript.github.io
#
# Primary Business: Queensland, Australia
# Licensed under the Apache License, version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# See CREDITS file for contributors and development history
# Development from 2019 by School of Earth and Environmental Sciences
#
##############################################################################

Import('*')

module_name = 'weipa'

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

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

local_env = env.Clone()

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

# collect dependencies for other modules
weipalibs = []
weipalibs += env['escript_libs']

if 'finley' in env['domains']:
    sources += ['FinleyDomain.cpp','FinleyElements.cpp','FinleyNodes.cpp']
    headers += ['FinleyDomain.h','FinleyElements.h','FinleyNodes.h']
    local_env.Append(CPPDEFINES = ['USE_FINLEY'])
    weipalibs += env['finley_libs']

if local_env['silo']:
    weipalibs += env['silo_libs']

if local_env['netcdf']:
    weipalibs += env['netcdf_libs']

pluginlibs = [] + weipalibs
pluginsources = [] + sources
# clone here to use same CPPDEFINES
visitplugin_env = local_env.Clone()

if 'oxley' in env['domains']:
    sources += ['OxleyDomain.cpp','OxleyElements.cpp','OxleyNodes.cpp']
    headers += ['OxleyDomain.h','OxleyElements.h','OxleyNodes.h']
    local_env.Append(CPPDEFINES = ['USE_OXLEY'])
    weipalibs += env['oxley_libs']
    weipalibs += env['p4est_libs']

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'])
    weipalibs += env['ripley_libs']

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'])
    weipalibs += env['speckley_libs']

sources.append(['VisItControl.cpp'])

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

local_env.PrependUnique(LIBS = weipalibs)


include_path = Dir(module_name, local_env['incinstall'])
hdr_inst = local_env.Install(include_path, headers)

lib = local_env.SharedLibrary(module_name, sources)
lib_inst = local_env.Install(local_env['libinstall'], lib)

if IS_OSX:
    env['weipa_libs'] = [lib_inst] + weipalibs
else:
    env['weipa_libs'] = [module_name] + weipalibs

### Python wrapper ###
py_env = local_env.Clone()
if IS_OSX:
    py_env.PrependUnique(LIBS = [lib_inst])
else:
    py_env.PrependUnique(LIBS = [module_name])
py_lib_name = module_name + 'cpp'
py_lib = py_env.PythonModule(py_lib_name, 'weipacpp.cpp')

mod_path = Dir(module_name, local_env['pyinstall'])
mod_inst = py_env.Install(mod_path, py_lib)

build = env.Alias('build_weipa', [hdr_inst, lib, py_lib])
env.Alias('install_weipa', [build, lib_inst, mod_inst])

######################
### Plugin library ###
######################
if 'finley' in env['domains']:
    visitplugin_env.PrependUnique(LIBS = pluginlibs)
    visitplugin_env.Append(CPPDEFINES = ['VISIT_PLUGIN'])
    visitplugin_env['OBJPREFIX']='reader_'

    plugin_lib = visitplugin_env.SharedLibrary('escriptreader', pluginsources)
    plugin_inst = local_env.Install(local_env['libinstall'], plugin_lib)

    build = env.Alias('build_escriptreader', plugin_lib)
    env.Alias('install_escriptreader', [build, plugin_inst])

