local m = {}

local rootdir = path.join(path.getabsolute(path.getdirectory(_SCRIPT)), "../")

local function generate_config_header(include_dir)
local output_file = path.join(include_dir, "ogg/config_types.h")

-- Only generate if it doesn't exist to avoid rebuilding every time
if os.isfile(output_file) then return end

	print("Generating " .. output_file)

	-- "Modern" config_types.h content that uses stdint.h (works on 32 and 64 bit)
	local content = [[
		#ifndef __CONFIG_TYPES_H__
		#define __CONFIG_TYPES_H__

		/* Generated automatically by Premake script */
		#include <stdint.h>

		typedef int16_t ogg_int16_t;
		typedef uint16_t ogg_uint16_t;
		typedef int32_t ogg_int32_t;
		typedef uint32_t ogg_uint32_t;
		typedef int64_t ogg_int64_t;
		typedef uint64_t ogg_uint64_t;

		#endif
	]]


	local f = io.open(output_file, "w")
	if f then
		f:write(content)
		f:close()
	else
			print("Error: Failed to write to " .. output_file)
	end
end

function m.generateproject(liboutdir, intdir)
	project"ogg"
	language"C" -- c++ will mangle names and sfml wont build
	kind"staticLib"
	targetdir (liboutdir)
	objdir(intdir)
	warnings"Off"

	local include_path = path.join(rootdir, "ogg/include")

	generate_config_header(include_path)

	includedirs {include_path}

	files
	{
		path.join(rootdir, "ogg/src/**.h"),
		path.join(rootdir, "ogg/src/**.c"),
	}

end

function m.link()
	links{"ogg"}
	includedirs{path.join(rootdir, "ogg/include")}
end

return m
