#include "helper.h"
#include "c.h"
#include "ld.h"
#include "tier1/utlstring.h"
#include "tier1/commandline.h"

CUtlVector<CUtlString> engine_CompiledFiles = {
		"engine/console.cpp",
		"engine/filesystem.cpp",
		"engine/server.cpp",
		"engine/engine.cpp",
		"engine/physics.cpp",
		"engine/gamemode.cpp",


		/* entities */	
		"engine/baseentity.cpp",
		"engine/level.cpp",
		"engine/brush.cpp",
                /* server entities */
                "engine/sv_worldspawn.cpp",
                "engine/sv_light.cpp",
                "engine/sv_playerstart.cpp",

                /* client entities */
                "engine/cl_worldspawn.cpp",
                "engine/cl_light.cpp",	

		/* io */
		"engine/input.cpp",
		"engine/net.cpp"
};

CUtlVector<CUtlString> engine_Libraries = {
	"c",
	"SDL3",
};

int engine_build()
{
	if (Target_t::DefaultTarget().kernel == TARGET_KERNEL_IOS || Target_t::DefaultTarget().kernel == TARGET_KERNEL_DARWIN)
	{
		engine_CompiledFiles.AppendTail("engine/ml_videosdl.cpp");
		engine_CompiledFiles.AppendTail("engine/ml_video.cpp");
	} else {
		engine_CompiledFiles.AppendTail("engine/vk_videosdl.cpp");
		engine_CompiledFiles.AppendTail("engine/vk_video.cpp");
	}
	if (Target_t::DefaultTarget().kernel == TARGET_KERNEL_LINUX)
	{
		engine_Libraries.AppendTail("vulkan");
	}
	CCProject compileProject = {};
	CLDProject ldProject = {};

	compileProject.m_szName = "engine";
	compileProject.files = engine_CompiledFiles;
	compileProject.includeDirectories = all_IncludeDirectories;
	compileProject.bFPIC = true;
	if (bStaticBuild)
		compileProject.macros.AppendTail((C_Macro_t){"STATIC_BUILD","1"});
	ldProject = compileProject.Compile();
	ldProject.libraries = engine_Libraries;
	if (bStaticBuild)
		ldProject.linkType = ELINK_STATIC_LIBRARY;
	else
	{
		ldProject.objects.AppendTail((CObject){tier1_lib});
		ldProject.objects.AppendTail((CObject){rapier_lib});
		ldProject.linkType = ELINK_DYNAMIC_LIBRARY;
	}

	CUtlString outputProject = ldProject.Link();
	
	if (!bStaticBuild)
	{
		IFileSystem2::MakeDirectory(CUtlString("%s/bin",szOutputDir.GetString()));
		IFileSystem2::CopyFile(CUtlString("%s/bin", szOutputDir.GetString()), outputProject);
	} else {
		engine_lib = outputProject;
	}




	return 0;
};
DECLARE_BUILD_STAGE(engine, engine_build);
