--[[
	cLinux : Lore of the Day!
	Made by Piorjade, daelvn

	NAME:        /boot/load
	CATEGORY:    boot
	SET:         Boot II
	VERSION:     01:alpha0
	DESCRIPTION:
		This script is ran after /boot/clinux.i
		and loads important libraries up
		and starts the shell at the end.
]]--

if _G['flag'] and _G['flag']['STATE_INIT'] then
	if not fs.exists("/sys/fs.l") then
		cLinuxPrintError("/sys/fs.l not found...")
		sleep(2)
		flag.STATE_DEAD = true
	elseif not fs.exists("/sys/perm.l") then
		cLinuxPrintError("/sys/perm.l not found...")
		sleep(2)
		flag.STATE_DEAD = true
	elseif not fs.exists("/sys/thread.l") then
		cLinuxPrintError("/sys/thread.l not found...")
		sleep(2)
		flag.STATE_DEAD = true
	elseif not fs.exists("/sys/sThread.l") then
		cLinuxPrintError("/sys/sThread.l not found...")
		sleep(2)
		flag.STATE_DEAD = true
	elseif not fs.exists("/sys/services.l") then
		cLinuxPrintError("/sys/services.l not found...")
		sleep(2)
		flag.STATE_DEAD = true
	end

	print("Loading perm API")
	local ok, err = loadAPI("/sys/perm.l")
	if ok == false or _G.lib.perm == nil then
		cLinuxPrintError("[perm.l] "..tostring(err))
		flag.STATE_DEAD = true
		sleep(1)
		return
	end
	sleep(0.5)
	print("Loading FS API")
	local ok, err = loadAPI("/sys/fs.l")
	if ok == false or _G.fs == nil then
		cLinuxPrintError("[fs.l] "..tostring(err))
		flag.STATE_DEAD = true
		sleep(1)
		return
	end
	sleep(0.5)
	print("Loading thread API")
	local ok, err = loadAPI("/sys/thread.l")
	if ok == false or _G.thread == nil then
		cLinuxPrintError("[thread.l] "..tostring(err))
		flag.STATE_DEAD = true
		sleep(1)
		return
	end
	print("Loading sThread API")
	local ok, err = loadAPI("/sys/sThread.l")
	if ok == false or _G.sThread == nil then
		cLinuxPrintError("[thread.l] "..tostring(err))
		flag.STATE_DEAD = true
		sleep(1)
		return
	end
	sleep(0.5)
	print("Loading services API")
	local ok, err = loadAPI("/sys/services.l")
	if ok == false or _G.sThread == nil then
		cLinuxPrintError("[services.l] "..tostring(err))
		flag.STATE_DEAD = true
		sleep(1)
		return
	end
	sleep(0.5)
	for _, a in ipairs(fs.list("/lib/")) do
		print("Loading "..a)
		local ok, err = loadAPI("/lib/"..a)
		if ok == false then
			local c = term.getTextColor()
			term.setTextColor(colors.red)
			print("["..a.."] "..tostring(err))
			term.setTextColor(c)
			sleep(0.5)
		else
			local c = term.getTextColor()
			term.setTextColor(colors.green)
			term.write("[OK]")
			term.setTextColor(c)
			print(" "..a.." loaded!")
			sleep(0.5)
		end
	end
	print("Finished!")
	sleep(0.5)
	if _G.lib.os then
		_G.lib.os['shutdown'] = os.shutdown
		_G.lib.os['reboot'] = os.reboot
	end

	--initiate shell-level-tasklist
	local tasks = {}
	tasks['list'] = {}
	tasks['last_uid'] = 0
	tasks['somethingInFG'] = false

	local xBox = {}

	for _, a in pairs(_G) do
		xBox[_] = a
	end
	--xBox._G = xBox
	--setmetatable(xBox, {__index = xBox.lib})
	--xBox._G = xBox
	local x, err = thread.new("/sys/shell.lua", xBox, "Shell", tasks)
	if x == false then
		cLinuxPrintError("Shell", err)
		sleep(2)
	end
	local running = true
	local evt = {}
	while running do
		local ok = thread.resumeAll(tasks, evt)
		evt = {os.pullEventRaw()}
		if ok == false then
			running = false
		end
	end
	flag.STATE_DEAD = true
else
	term.clear(1,1)
	term.setCursorPos(1,1)
	printError("cLinux not initialized, boot cLinux.i")
end
