--[[
	Creates a single file and minified distributable version.
]]
local minified = false

local api = '--Bedrock Build: '

local h = fs.open('.buildn','r')
local buildn = tonumber(h.readAll())
h.close()
buildn = buildn + 1

local h = fs.open('.buildn','w')
h.write(tostring(buildn))
h.close()
api = api .. buildn .. '\n'

print('Starting Bedrock Build: '..buildn)

function readFile(path)
	local h = fs.open(path,'r')
	if h then
		local file = h.readAll()
		h.close()
		return file
	else
		error('Failed to open file: '..path)
	end
end

function append(str)
	api = api .. str
end

append('--This code is squished down in to one, rather hard to read file.\n')
append('--As such it is not much good for anything other than being loaded as an API.\n')
append('--If you want to look at the code to learn from it, copy parts or just take a look,\n')
append('--you should go to the GitHub repo. http://github.com/oeed/Bedrock/\n')
append('\n')
append([[--
--		Bedrock is the core program framework used by all OneOS and OneCode programs.
--							Inspired by Apple's Cocoa framework.
--									   (c) oeed 2014
--
--		  For documentation see the Bedrock wiki, github.com/oeed/Bedrock/wiki/
--

]])

append('local apis = {\n')

for i, v in ipairs(fs.list((minified and '/Build/Minified' or '')..'/API/')) do
	if v ~= '.DS_Store' then
		append('["'..string.match(v, '(%a+)%.?.-')..'"] = [[\n')
		append(readFile('/API/'..v))
		append('\n]],\n')
	end
end

append('}\n')

append('local objects = {\n')

for i, v in ipairs(fs.list((minified and '/Build/Minified' or '')..'/Objects/')) do
	if v ~= '.DS_Store' then
		append('["'..string.match(v, '(%a+)%.?.-')..'"] = [[\n')
		append(readFile('/Objects/'..v))
		append('\n]],\n')
	end
end

append('}\n')

append(readFile('Bedrock'))

local h = fs.open('Build/Bedrock', 'w')
h.write(api)
h.close()
print('Build completed successfully!')
