#!/usr/bin/env python
"""
SConstruct for testextension - uses Zig compiler with godot-cpp.
Links to pre-built godot-cpp library when available.
"""
import os
import sys

# Use godot-cpp's SConstruct which properly configures everything
env = SConscript("godot-cpp-godot-4.5-stable/SConstruct")

# Get the directory where scons is running (the project root)
project_path = os.getcwd()
zig_exe = os.path.join(project_path, "zig-x86_64-windows-0.16.0-dev.1484+d0ba6642b", "zig.exe")

# Configure Zig as the compiler
env["CC"] = zig_exe + " cc"
env["CXX"] = zig_exe + " c++"
env["LINK"] = zig_exe + " c++"
env["RANLIB"] = zig_exe + " ranlib"

# Use temp files for sources to avoid "command line too long"
if sys.platform == "win32":
    env["ARCOM"] = zig_exe + " ar $ARFLAGS $TARGET ${TEMPFILE('$SOURCES')}"
    env["LINKCOM"] = zig_exe + " c++ $LINKFLAGS -o $TARGET ${TEMPFILE('$SOURCES')} $SHLINKFLAGS"
else:
    env["AR"] = zig_exe + " ar"
    env["LINK"] = zig_exe + " c++"

# Fix for "App Data Dir Unavailable"
zig_cache = os.path.join(project_path, "zig_cache")
if not os.path.exists(zig_cache):
    os.makedirs(zig_cache)

env["ENV"]["ZIG_GLOBAL_CACHE_DIR"] = zig_cache
env["ENV"]["ZIG_LOCAL_CACHE_DIR"] = zig_cache

# Sources - ONLY our extension code
sources = Glob("testextension/src/*.cpp")

if env["platform"] == "macos":
    library = env.SharedLibrary(
        "testextension/bin/gdexample.{}.{}.framework/gdexample.{}.{}".format(
            env["platform"], env["target"], env["platform"], env["target"]
        ),
        source=sources,
    )
else:
    library = env.SharedLibrary(
        "testextension/bin/gdexample{}{}".format(env["suffix"], env["SHLIBSUFFIX"]),
        source=sources,
    )

Default(library)
