#! /usr/bin/env python
import os
import inspect
import pathlib

import waves
import turbo_turtle


# Accept command line options with fall back default values
AddOption(
    "--build-dir",
    dest="variant_dir_base",
    default="build",
    nargs=1,
    type=str,
    action="store",
    metavar="DIR",
    help="SCons build (variant) root directory. Relative or absolute path. (default: '%default')"
)
AddOption(
    "--turbo-turtle-command",
    dest="turbo_turtle_command",
    default="turbo-turtle",
    nargs=1,
    type="string",
    action="store",
    metavar="DIR",
    help="Override for the Turbo-Turtle command (default: '%default')"
)

env = Environment(
    ENV=os.environ.copy(),
    variant_dir_base=pathlib.Path(GetOption("variant_dir_base"))
    turbo_turtle_command=GetOption("turbo_turtle_command"),
)

env["abaqus"] = waves.scons_extensions.add_program(env, ["/apps/abaqus/Commands/abq2024", "abq2024"])
env["cubit"] = waves.scons_extensions.add_cubit(env, ["/apps/Cubit-16.16/cubit", "cubit"])

env.Append(BUILDERS={
    "TurboTurtleSphere": turbo_turtle.scons_extensions.sphere(
        program=env["turbo_turtle_command"],
        options="--model-name ${model_name} --part-name ${part_name} --backend ${backend}",
        abaqus_command=[env['abaqus']],
        cubit_command=[env['cubit']],
    ),
    "TurboTurtlePartition": turbo_turtle.scons_extensions.partition(
        program=env["turbo_turtle_command"],
        options="--model-name ${model_name} --part-name ${part_name} --backend ${backend}",
        abaqus_command=[env['abaqus']],
        cubit_command=[env['cubit']],
    ),
    "TurboTurtleMesh": turbo_turtle.scons_extensions.mesh(
        program=env["turbo_turtle_command"],
        options="--model-name ${model_name} --part-name ${part_name} --backend ${backend} --global-seed ${global_seed}",
        abaqus_command=[env['abaqus']],
        cubit_command=[env['cubit']],
    ),
    "TurboTurtleImage": turbo_turtle.scons_extensions.image(
        program=env["turbo_turtle_command"],
        options="--model-name ${model_name} --part-name ${part_name} --backend ${backend}",
        abaqus_command=[env['abaqus']],
        cubit_command=[env['cubit']],
    ),
    "TurboTurtleExport": turbo_turtle.scons_extensions.export(
        program=env["turbo_turtle_command"],
        options="--model-name ${model_name} --part-name ${part_name} --backend ${backend}",
        abaqus_command=[env['abaqus']],
        cubit_command=[env['cubit']],
    )
})

for backend, extension in [("abaqus", "cae"), ("cubit", "cub")]:
    build_dir = env["variant_dir_base"] / backend
    SConscript("SConscript", variant_dir=build_dir, exports=["env", "backend", "extension"], duplicate=False)

env.Default()

waves.scons_extensions.project_help()
