import libtbx.load_env
import os
Import("env_base", "env_etc")

env_etc.xfel_dist = libtbx.env.dist_path("xfel")
env_etc.xfel_include = os.path.dirname(env_etc.xfel_dist)
env_etc.dials_dist = libtbx.env.dist_path("dials")
env_etc.dials_include = os.path.dirname(env_etc.dials_dist)
env_etc.xfel_common_includes = [
  env_etc.dials_include,
  env_etc.xfel_include,
  env_etc.libtbx_include,
  env_etc.cctbx_include,
  env_etc.scitbx_include,
  env_etc.chiltbx_include,
  env_etc.omptbx_include,
  env_etc.boost_include,
  env_etc.annlib_dist,
  env_etc.annlib_include[0],
  env_etc.annlib_include[1],
  env_etc.annlib_adaptbx_include[0],
  env_etc.annlib_adaptbx_include[1],
  env_etc.dxtbx_include,
]

env = env_base.Clone(SHLINKFLAGS=env_etc.shlinkflags)
env.Append(LIBS=["cctbx"] + env_etc.libm)
env_etc.include_registry.append(
  env=env,
  paths=env_etc.xfel_common_includes)
if (env_etc.static_libraries): builder = env.StaticLibrary
else:                          builder = env.SharedLibrary

builder(
  target="#lib/xfel_mono_sim",
  source=["mono_simulation/bandpass_gaussian.cpp",
          "mono_simulation/ccixfel_core.cpp",
  ])
builder(
  target="#lib/legacy_scale",
  source=["metrology/legacy_scale/scale_core.cpp",
          "metrology/legacy_scale/bandpass_gaussian.cpp"
  ])

if (not env_etc.no_boost_python):
  Import("env_boost_python_ext")
  env_xfel_boost_python_ext = env_boost_python_ext.Clone()
  env_xfel_boost_python_ext.Prepend(
                 LIBS=["cctbx", "scitbx_boost_python"])
  env_xfel_boost_python_ext.SharedLibrary(
                 target="#lib/acqiris_ext", source="acqiris_ext.cpp")
  env_xfel_boost_python_ext.SharedLibrary(
                 target="#lib/xfel_ext", source="ext.cpp")
  env_xfel_boost_python_ext.SharedLibrary(
                 target="#lib/xfel_metrology_ext", source="metrology_ext.cpp")
  env_xfel_boost_python_ext.SharedLibrary(
                 target="#lib/xes_ext", source="vonHamos/xes_ext.cpp")
  env_xfel_boost_python_ext.SharedLibrary(
                 target="#lib/xfel_sdfac_refine_ext", source="merging/algorithms/error_model/sdfac_refine_ext.cpp")

  env_legacy_boost_python_ext = env_boost_python_ext.Clone()
  env_legacy_boost_python_ext.Prepend(
                 LIBS=["legacy_scale", "cctbx", "scitbx_boost_python", "ann"])
  env_legacy_boost_python_ext.SharedLibrary(
                 target="#lib/xfel_legacy_scale_ext", source="metrology/legacy_scale/ext.cpp")


  env_etc.include_registry.append(
    env=env_xfel_boost_python_ext,
    paths=env_etc.xfel_common_includes)
  Export("env_xfel_boost_python_ext")

  env_etc.include_registry.append(
    env=env_legacy_boost_python_ext,
    paths=env_etc.xfel_common_includes)
  Export("env_legacy_boost_python_ext")

  SConscript("mono_simulation/SConscript")
  SConscript("clustering/SConscript")
  SConscript("merging/SConscript")
