#! /usr/bin/env python

import pathlib

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

# Simulation variables
workflow_name = "submit_beam_cae"

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

# Comment used in tutorial code snippets: marker-1

# Create the job-ready beam example CAE file
cae_prep.extend(
    env.Command(
        target=["beamExample.py"],
        source=["SConscript"],
        action=[
            "cd ${TARGET.dir.abspath} && ${abaqus} fetch job=beamExample",
            "echo \"mdb.saveAs(pathName='beam.cae')\" >> ${TARGET.abspath}",
        ],
        abaqus=env["ABAQUS_PROGRAM"],
    )
)

cae_prep.extend(
    env.AbaqusJournal(
        target=["beam.cae"],
        source=["beamExample.py"],
    )
)

# Comment used in tutorial code snippets: marker-2

# Run the tutorial CAE job submission solution
workflow.extend(
    env.AbaqusJournal(
        target=["beam.odb"],
        source=["submit_cae.py", "beam.cae"],
        subcommand_options="--input-file ${SOURCES[1].abspath} --job-name beam --model-name Beam",
    )
)

# Comment used in tutorial code snippets: marker-3

# Collector alias based on build directory name
env.Alias("create_beam_cae", cae_prep)
env.Alias(workflow_name, workflow)

if not env["unconditional_build"] and not env["ABAQUS_PROGRAM"]:
    print(f"Program 'abaqus' was not found in construction environment. Ignoring '{workflow_name}' target(s)")
    Ignore([".", workflow_name], workflow)
    Ignore([".", workflow_name], cae_prep)
