for k,side in ipairs(redstone.getSides()) do
	if peripheral.getType(side) == "modem" then
		rednet.open(side)
	end
end

local function print(text)
	local x, y = term.getCursorPos()
	local w, h = term.getSize()
	
	if text then
		while x + #text - 1 > w do
			term.setCursorPos(x, y)
			term.write(text:sub(1, w - x + 1))
			text = text:sub(w - x + 2)
			x = 1
			y = y + 1
		end
		
		term.setCursorPos(x, y)
		term.write(text)
	end
	
	term.setCursorPos(1, y + 1)
end

local function printCompact(text)
	local x, y = term.getCursorPos()
	local w, h = term.getSize()
	
	local function write(text)
		while #text > 0 do
			if text:find("CookieOS") == 1 then
				term.setTextColor(colors.orange)
				term.write("CookieOS")
				text = text:sub(9)
			elseif text:find("CraftOS") == 1 then
				term.setTextColor(colors.yellow)
				term.write("CraftOS")
				text = text:sub(8)
			else
				term.setTextColor(colors.white)
				term.write(text:sub(1,1))
				text = text:sub(2)
			end
		end
	end
	
	if text then
		while #text + x - 1 > w do
			local line = text:sub(1, w - x)
			local i = w - x + 1
			
			repeat
				i = i - 1
			until line:sub(i,i) == " " or i < 1
			
			term.setCursorPos(x, y)
			write(text:sub(1, i))
			text = text:sub(i + 1)
			
			y = y + 1
		end
		
		term.setCursorPos(x, y)
		write(text)
	end
end

local w, h = term.getSize()
window = window.create(term.current(), 1, 2, w, h - 2)

local function drawText(text, x, y, color, bColor)
	term.setCursorPos(x, y)
	term.setBackgroundColor(bColor)
	term.setTextColor(color)
	term.write(text)
end

os.startTimer(1)

local t = 20
local menu = 1
local option = 1
local options = 2
local description = "Start CookieOS"

if not fs.exists("programs/Browser/host") then
	while true do
		drawText("CookieOS " .. __version, 1, 1, colors.orange, colors.black)
		drawText("Creator:", 1, 2, colors.white, colors.black)
		drawText("Maxboxx", 10, 2, colors.cyan, colors.black)
		
		if menu == 1 then
			drawText(string.rep("_", w), 1, 4, colors.white, colors.black)
			drawText(" Start  " .. t .."s ", 3, 6, colors.white, option == 1 and colors.gray or colors.black)
			drawText(" Options    ", 3, 8, colors.white, option == 2 and colors.gray or colors.black)
			drawText(string.rep("_", w), 1, 9, colors.white, colors.black)
			
			drawText("Use arrow keys to change option", 1, 12, colors.white, colors.black)
			drawText("Press enter to select", 1, 13, colors.white, colors.black)
		elseif menu == 2 then
			drawText(string.rep("_", w), 1, 4, colors.white, colors.black)
			drawText(" CraftOS    ", 3, 6, colors.white, option == 1 and colors.gray or colors.black)
			drawText(" Reinstall  ", 3, 8, colors.white, option == 2 and colors.gray or colors.black)
			drawText(" Uninstall  ", 3, 10, colors.white, option == 3 and colors.gray or colors.black)
			drawText(" Back       ", 3, 12, colors.white, option == 4 and colors.gray or colors.black)
			drawText(string.rep("_", w), 1, 13, colors.white, colors.black)
			
			drawText("Use arrow keys to change option", 1, 16, colors.white, colors.black)
			drawText("Press enter to select", 1, 17, colors.white, colors.black)
		end
		
		term.setCursorPos(30, 6)
		printCompact(description)
		
		local event, p1, p2, p3 = os.pullEvent()
		
		if event == "timer" then
			
			t = t - 1
			os.startTimer(1)
			
			if t < 0 and menu == 1 then
				break
			end
			
		elseif event == "key" then
		
			if p1 == keys.up and option > 1 then
				option = option - 1
				
				term.clear()
			elseif p1 == keys.down and option < options then
				option = option + 1
				
				term.clear()
			elseif p1 == keys.enter then
			
				if menu == 1 then
					if option == 1 then
						break
					elseif option == 2 then
						menu = 2
						option = 1
						options = 4
					end
				elseif menu == 2 then
					if option == 1 then
						__CraftOS = true
						error()
					elseif option == 2 then
						
						local t = fs.list("")
						
						for i = 1,#t do
							if t[i] ~= "rom" and t[i] ~= "disk" then
								fs.delete(t[i])
							end
						end
						
						term.clear()
						term.setCursorPos(1,1)
						
						shell.run("pastebin run W9XG8BM2")
						
					elseif option == 3 then
					
						local t = fs.list("")
						
						for i = 1,#t do
							if t[i] ~= "rom" and t[i] ~= "disk" then
								fs.delete(t[i])
							end
						end
						
						os.reboot()
					
					elseif option == 4 then
						menu = 1
						option = 1
						options = 2
						t = 20
					end
				end
				
				term.clear()
				
			end
			
			if menu == 1 then
				if option == 1 then
					description = "Start CookieOS"
				elseif option == 2 then
					description = "CookieOS options"
				end
			elseif menu == 2 then
				if option == 1 then
					description = "Get access to the CraftOS commands"
				elseif option == 2 then
					description = "Reinstall CookieOS. All of your programs and settings will be removed"
				elseif option == 3 then
					description = "Uninstall everything on the computer. All files will be removed"
				elseif option == 4 then
					description = "Go back to the start up menu"
				end
			end
			
		end
	end
	
	term.setTextColor(colors.green)
	term.clear()
	term.setCursorPos(1,1)
	print("Checking for updates")
	term.setTextColor(colors.white)
	sleep(1)
	term.setTextColor(colors.white)
	sleep(2)
	print()
	
	if fs.exists("versionInfo") then
		local file = io.open("versionInfo", "r")
		local version = file:read()
		file:close()
		
		if version ~= __version then
			__changelog = true
			term.setTextColor(colors.orange)
			term.write("CookieOS " .. version)
			term.setTextColor(colors.white)
			print(" is available")
			term.setTextColor(colors.green)
			print("Installing updates")
			term.setTextColor(colors.white)
			print()
			sleep(1)
			shell.run("pastebin get aKkRztpZ changelog")
			shell.run("pastebin run W9XG8BM2")
		else
			term.setTextColor(colors.red)
			print("No updates available")
		end
	end
	
	sleep(3)
	
	shell.run("main")

	for i = 20,1,-1 do
		term.setCursorPos(31, 10)
		print(i .. " seconds ")
		sleep(1)
	end
	
	os.reboot()
else
	term.setCursorPos(1,1)

	local function display()
		local x, y = term.getCursorPos()
		term.setBackgroundColor(colors.black)
		term.setTextColor(colors.orange)
		term.setCursorPos(1,1)
		term.write("CookieOS " .. __version .. " Server" .. string.rep(" ", w - 23))
		term.setBackgroundColor(colors.red)
		term.setTextColor(colors.white)
		term.write("Stop")
		term.setBackgroundColor(colors.black)
		term.setCursorPos(x, y)
	end
	
	term.setBackgroundColor(colors.black)
	term.setTextColor(colors.orange)
	term.clear()
	display()
	print()

	local websites = {}
	
	for k,v in pairs(fs.list("programs/Browser/websites")) do
		shell.run("programs/Browser/websites/" .. v)
		websites[v] = WebInfo
	end

	while true do
		local event, p1, p2, p3 = os.pullEvent()
		local id, message = p1, p2
		
		if event == "rednet_message" and type(message) == "string" then
			if #message > 15 then
				if message:sub(1,15) == "BrowserAddress:" then
					message = message:sub(16)
					
					if websites[message] then
						rednet.send(id, websites[message])
						
						term.setTextColor(colors.white)
						term.write("Computer ")
						term.setTextColor(colors.cyan)
						term.write(id .. "")
						term.setTextColor(colors.white)
						term.write(" is visiting: ")
						term.setTextColor(colors.green)
						print(message)
						display()
					end
				end
			end
		elseif event == "mouse_click" then
			if p2 >= w - 3 and p2 <= w and p3 == 1 then
				fs.delete("programs/Browser/host")
				os.reboot()
			end
		end
	end
end
