import os, sys, time, traceback

def p(*a):
    print(*a, flush=True)

try:
    p("RUNNER START")
    p("Runner file:", os.path.abspath(__file__))
    p("Initial CWD:", os.getcwd())

    # Immer in den Script-Ordner wechseln
    base = os.path.dirname(os.path.abspath(__file__))
    os.chdir(base)
    p("CWD after chdir:", os.getcwd())
    p("Files here:", os.listdir("."))

    # Simuliere argv fürs stepanalyser.py
    sys.argv = [
        "stepanalyser.py",
        "--material", "stainless",
        # "--thickness-mm", "1.0",
    ]
    p("ARGV forced:", sys.argv)

    # Step analyser laden & ausführen
    path = os.path.join(base, "stepanalyser.py")
    p("Executing:", path)
    code = open(path, "r", encoding="utf-8").read()
    exec(compile(code, path, "exec"), {"__name__": "__main__"})

    p("RUNNER END (normal)")
except Exception:
    p("RUNNER ERROR:")
    p(traceback.format_exc())
finally:
    # Hard exit, damit wirklich Schluss ist
    os._exit(0)
