#!/usr/bin/env python

import os

env = {}
Import("env")

godot_headers_path = "../godot-cpp/godot-headers/"
cpp_bindings_path = "../godot-cpp/"
cpp_library = "libgodot-cpp"
bits = env["bits"]

# Process some arguments
if env["use_llvm"]:
    env["CC"] = "clang"
    env["CXX"] = "clang++"

if env["p"] != "":
    env["platform"] = env["p"]

if env["platform"] == "":
    print("No valid target platform selected.")
    quit()

env["target_path"] = "../" + env["target_path"]

if not os.path.isdir(env["target_path"]):
    os.mkdir(env["target_path"])

# Check our platform specifics
if env["platform"] == "osx":
    env["target_path"] += "osx/"
    cpp_library += ".osx"
    env.Append(LIBPATH=[env["macos_ssl_path"] + "lib/"])
    env.Append(LIBS=["ssl", "crypto"])

    if env["target"] in ("debug", "d"):
        env.Append(CCFLAGS=["-g", "-O2", "-std=c++17"])
    else:
        env.Append(CCFLAGS=["-g", "-O3", "-std=c++17"])

elif env["platform"] == "linux":
    env["target_path"] += "linux/"
    cpp_library += ".linux"
    env.Append(LIBS=["ssl"])

    if env["target"] in ("debug", "d"):
        env.Append(CCFLAGS=["-fPIC", "-g3", "-Og", "-std=c++17"])
    else:
        env.Append(CCFLAGS=["-fPIC", "-g", "-O3", "-std=c++17"])

elif env["platform"] == "windows":
    env["target_path"] += "win64/"
    cpp_library += ".windows"
    # This makes sure to keep the session environment variables on windows,
    # that way you can run scons in a vs 2017 prompt and it will find all the required tools
    env.Append(ENV=os.environ)

    env.Append(CCFLAGS=["-DWIN32", "-D_WIN32", "-D_WINDOWS",
               "-W3", "-GR", "-D_CRT_SECURE_NO_WARNINGS", "/std:c++17"])
    env.Append(LIBS=["Advapi32", "Winhttp", "Rpcrt4",
               "crypt32", "OLE32", "user32"])
    if env["target"] in ("debug", "d"):
        env.Append(CCFLAGS=["-EHsc", "-D_DEBUG", "-MDd", "/DEBUG"])
        env["PDB"] = env["target_name"] + ".pdb"
    else:
        env.Append(CCFLAGS=["-O2", "-EHsc", "-DNDEBUG", "-MD"])

if env["target"] in ("debug", "d"):
    cpp_library += ".debug"
else:
    cpp_library += ".release"

if env['platform'] == 'osx' and env['macos_arch'] != 'universal':
    cpp_library += "." + env['macos_arch']
else:
    cpp_library += "." + str(bits)

env.Append(CPPPATH=[".", "src/"])
env.Append(CPPPATH=[godot_headers_path, cpp_bindings_path + "include/",
           cpp_bindings_path + "include/core/", cpp_bindings_path + "include/gen/"])
env.Append(CPPPATH=["../thirdparty/git2/libgit2/include/"])
env.Append(LIBPATH=[cpp_bindings_path + "bin/", "../thirdparty/bin/"])
env.Append(LIBS=[cpp_library, "git2", "ssh2"])

library = env.SharedLibrary(
    target=env["target_path"] + env["target_name"],
    source=Glob("src/*.cpp"))
Default(library)
