# project_root/src/SConscript
import os
import shutil

# Import the environment from the SConstruct file
Import("env")
with open(env["PROJECT_TOOL_S"]) as f:
    exec(f.read())

# The GCC version needs to be greater than or equal to 8.
gcc_dumpversion = CC_cmd_execute("-dumpversion")
if int(gcc_dumpversion.split(".")[0]) >= 8:
    check_component("firebird")

    SRCS = Glob("src/*.c*")
    INCLUDE = [ADir("include"), ADir(".")]
    PRIVATE_INCLUDE = []
    REQUIREMENTS = ["pthread", "tommath"]
    STATIC_LIB = []
    DYNAMIC_LIB = []
    DEFINITIONS = ["--std=c++17"]
    DEFINITIONS_PRIVATE = []
    LDFLAGS = []
    LINK_SEARCH_PATH = []

    INCLUDE.append(ADir("../../../github_source/firebird/single"))
    gcc_dumpmachine = env["GCC_DUMPMACHINE"].split("-")
    if "arm" in gcc_dumpmachine and "gnueabihf" in gcc_dumpmachine:
        package_name = "Firebird-5.0.0.1306-0-linux-arm32"
        Firebird_path = str(
            ADir("../../../github_source/Firebird-5.0.0.1306-0-linux-arm32")
        )
        down_url = "https://github.com/FirebirdSQL/firebird/releases/download/v5.0.0/Firebird-5.0.0.1306-0-linux-arm32.tar.gz"
        INCLUDE.append(
            ADir(
                "../../../github_source/Firebird-5.0.0.1306-0-linux-arm32/Firebird-5.0.0.1306-0-linux-arm32/buildroot/opt/firebird/include"
            )
        )
        DYNAMIC_LIB.append(
            AFile(
                "../../../github_source/Firebird-5.0.0.1306-0-linux-arm32/Firebird-5.0.0.1306-0-linux-arm32/buildroot/opt/firebird/lib/libfbclient.so.2"
            )
        )
    elif "x86_64" in gcc_dumpmachine and "linux" in gcc_dumpmachine:
        Firebird_path = str(
            ADir("../../../github_source/Firebird-5.0.0.1306-0-linux-x64")
        )
        down_url = "https://github.com/FirebirdSQL/firebird/releases/download/v5.0.0/Firebird-5.0.0.1306-0-linux-x64.tar.gz"
        package_name = "Firebird-5.0.0.1306-0-linux-x64"
        INCLUDE.append(
            os.path.join(
                str(Firebird_path), package_name, "buildroot/opt/firebird/include"
            )
        )
        DYNAMIC_LIB.append(
            os.path.join(
                str(Firebird_path),
                package_name,
                "buildroot/opt/firebird/lib/libfbclient.so.2",
            )
        )
    Firebird_path = check_wget_down(
        down_url,
        package_name + ".tar.gz",
    )
    Firebird_buildroot = os.path.join(Firebird_path, package_name, "buildroot")
    if not os.path.exists(Firebird_buildroot):
        import tarfile

        with tarfile.open(Firebird_buildroot + ".tar.gz", "r:gz") as tar:
            tar.extractall(Firebird_buildroot)

    env["COMPONENTS"].append(
        {
            "target": "cpp_firebird_demo",
            "SRCS": SRCS,
            "INCLUDE": INCLUDE,
            "PRIVATE_INCLUDE": PRIVATE_INCLUDE,
            "REQUIREMENTS": REQUIREMENTS,
            "STATIC_LIB": STATIC_LIB,
            "DYNAMIC_LIB": DYNAMIC_LIB,
            "DEFINITIONS": DEFINITIONS,
            "DEFINITIONS_PRIVATE": DEFINITIONS_PRIVATE,
            "LDFLAGS": LDFLAGS,
            "LINK_SEARCH_PATH": LINK_SEARCH_PATH,
            "REGISTER": "project",
        }
    )
