#!/usr/bin/env python

# SPDX-FileCopyrightText: 2020-2024 Leroy Hopson <godot-xterm@leroy.nix.nz>
# SPDX-License-Identifier: MIT

import os

env = SConscript("thirdparty/godot-cpp/SConstruct")
env['ENV'] = os.environ

VariantDir('build', 'src', duplicate=0)
env['OBJPREFIX'] = os.path.join('build', '')

env.Append(CPPPATH=[
    "thirdparty/libtsm/src/tsm",
    "thirdparty/libtsm/external",
    "thirdparty/libtsm/src/shared",
    'thirdparty/libuv/src',
    'thirdparty/libuv/include',
])

sources = Glob("src/*.cpp") + Glob("thirdparty/libtsm/src/tsm/*.c")
sources.append([
    'thirdparty/libtsm/external/wcwidth/wcwidth.c',
    'thirdparty/libtsm/src/shared/shl-htable.c',
])

if env['platform'] == 'linux' or env['platform'] == 'macos':
    env.Append(LIBS=['util', env.File('thirdparty/libuv/build/libuv_a.a')])
else:
    env.Append(CPPDEFINES=['_PTY_DISABLED'])

if env["platform"] == "macos":
    library = env.SharedLibrary(
        "bin/libgodot-xterm.{}.{}.framework/libgodot-xterm.{}.{}".format(
            env["platform"], env["target"], env["platform"], env["target"]
        ),
        source=sources,
    )
    spawn_helper = env.Program(
        "bin/spawn-helper",
        source="thirdparty/node-pty/src/unix/spawn-helper.cc"
    )
    Default(spawn_helper)
else:
    library = env.SharedLibrary(
        "bin/libgodot-xterm{}{}".format(env["suffix"], env["SHLIBSUFFIX"]),
        source=sources,
    )

Default(library)