package.path = package.path..";../cimgui/generator/?.lua"
local cpp2ffi = require"cpp2ffi"
local save_data = cpp2ffi.save_data
local read_data = cpp2ffi.read_data
local location = cpp2ffi.location
----------comand line
local script_args = {...}
local FREETYPE_GENERATION = script_args[1]:match("freetype") and true or false
local WCHAR32_GENERATION = script_args[1]:match("wchar32") and true or false

----------------------------------
----utility functions
local function get_cdefs(gccline,locat,cdef)
	cdef = cdef or {}
	local pipe,err = io.popen(gccline,"r")
	if not pipe then error("could not execute gcc "..err) end

	for line in location(pipe,{locat}) do
		line = line:gsub("extern __attribute__%(%(dllexport%)%)%s*","")
		line = line:gsub("extern __declspec%(dllexport%)%s*","")
		if line~="" then table.insert(cdef,line) end
	end
	pipe:close()
	return cdef
end

local function get_all_cdefs(sources)
	local cdefs = {}
	for i,v in ipairs(sources) do
		print("get cdefs from",v)
		local def_ft = FREETYPE_GENERATION and " -DIMGUI_ENABLE_FREETYPE -DIMGUI_ENABLE_STB_TRUETYPE " or ""
		local def_w32 = WCHAR32_GENERATION and " -DIMGUI_USE_WCHAR32 " or ""
		cdefs = get_cdefs([[gcc -E ]]..def_ft..def_w32..[[-DCIMGUI_DEFINE_ENUMS_AND_STRUCTS -I "../cimgui" ]].."../"..v.."/"..v..".h",v,cdefs)
	end
	return cdefs
end

--------------------------------------------------------
--first get cdefs
print"get cdefs"
local sources = {"cimgui", "cimplot", "cimguizmo", "cimguizmo_quat", "cimnodes","cimnodes_r","cimCTE","cimplot3d"}
local cdefs = get_all_cdefs(sources)

print"get cimgui_impl cdefs"
cdefs = get_cdefs([[gcc -E -DCIMGUI_API="" -DCIMGUI_USE_OPENGL3 -DCIMGUI_USE_SDL2 -DCIMGUI_USE_SDL3 -DCIMGUI_USE_GLFW -DCIMGUI_USE_OPENGL2 -DCIMGUI_DEFINE_ENUMS_AND_STRUCTS ../cimgui/cimgui_impl.h]],"cimgui_impl",cdefs)

table.insert(cdefs,1,"typedef void FILE;")

----- create imgui/cdefs.lua
print"save cdefs.lua"
--time_t is needed for implot_internal
local time_t_cdefs = [=[
--time_t needed for implot_internal
local ffi = require"ffi"
local IS_64_BIT = ffi.abi('64bit')
local cdecl = ""
cdecl = cdecl .. [[
typedef struct tm
{
   int tm_sec   ;
   int tm_min   ;
   int tm_hour  ;
   int tm_mday  ;
   int tm_mon   ;
   int tm_year  ;
   int tm_wday  ;
   int tm_yday  ;
   int tm_isdst ;
} tm;
]]
if ffi.os == "Windows" then
	if IS_64_BIT then
		cdecl = cdecl..[[typedef __int64 time_t;]]
	else
		cdecl = cdecl..[[typedef __int32 time_t;]]
	end
else
	cdecl = cdecl..[[typedef size_t time_t;]]
end

]=]
table.insert(cdefs,1,time_t_cdefs)
table.insert(cdefs,2,"--[[ BEGIN AUTOGENERATED SEGMENT ]]\n cdecl = cdecl .. [[")
table.insert(cdefs,"]]\n--[[ END AUTOGENERATED SEGMENT ]]\n")
local hstrfile = read_data"./imgui_base_cdefs.lua"
save_data("./imgui/cdefs.lua",table.concat(cdefs,"\n"), hstrfile)
print"generate classes"
local class_gen = require"class_gen"
local classes = class_gen(sources, FREETYPE_GENERATION)
----- generate imgui/glfw.lua
print"save glfw.lua"
local iniclass = "local cimguimodule = 'cimgui_glfw' --set imgui directory location\n"
local base = read_data("./imgui_base.lua")
local base_glfw = read_data("./imgui_base_glfw.lua")
save_data("./imgui/glfw.lua",iniclass, base, base_glfw, classes)

----- generate imgui/sdl.lua
print"save sdl.lua"
local iniclass = "local cimguimodule = 'cimgui_sdl' --set imgui directory location\n"
local base_sdl = read_data("./imgui_base_sdl.lua")
save_data("./imgui/sdl.lua",iniclass, base, base_sdl, classes)

----- generate imgui/sdl3.lua
print"save sdl3.lua"
local iniclass = "local cimguimodule = 'cimgui_sdl3' --set imgui directory location\n"
local base_sdl3 = read_data("./imgui_base_sdl3.lua")
save_data("./imgui/sdl3.lua",iniclass, base, base_sdl3, classes)

print"-----------------------------done generation"