#! /usr/bin/env python
"""Rectangle compression workflow: Sierra solve

Requires the following ``SConscript(..., exports={})``

* ``env`` - The SCons construction environment with the following required keys

  * ``unconditional_build`` - Boolean flag to force building of conditionally ignored targets
  * ``cubit`` - String path for the Cubit executable

* ``envSierra`` - The Sierra SCons construction environment with the following required keys

  * ``sierra`` - String path for the Sierra executable
"""

import pathlib

# Inherit the parent construction environment
Import("env", "envSierra")

# Simulation variables
build_directory = pathlib.Path(Dir(".").abspath)
workflow_name = build_directory.name

# Collect the target nodes to build a concise alias for all targets
workflow = []

element_type = "SHELL"
solver = "sierra"
SConscript(
    "cubit",
    exports={"env": env, "element_type": element_type, "solver": solver},
    duplicate=False,
)

# SolverPrep
sierra_source_list = ["#/modsim_package/sierra/rectangle_compression.i"]
sierra_source_list = [pathlib.Path(source_file) for source_file in sierra_source_list]
workflow.extend(env.CopySubstfile(sierra_source_list))

# Sierra Solve
solve_source_list = [source_file.name for source_file in sierra_source_list]
solve_source_list.append("rectangle_mesh.g")
workflow.extend(
    envSierra.Sierra(
        target=["rectangle_compression.e"],
        source=solve_source_list,
    )
)

# Collector alias based on parent directory name
env.Alias(workflow_name, workflow)

# Developer note: CI system configuration for Sierra is inconsistent. Do not force with ``unconditional build`` option.
if not envSierra["sierra"]:
    print(f"Program 'sierra' was not found in construction environment. Ignoring '{workflow_name}' target(s)")
    Ignore([".", workflow_name], workflow)
