local w,h = term.getSize()
term.clear()
local tArgs = {...}

--Pre Functions

function fwrite(path, text)
	local file = assert(io.open(path, "w"))
	file:write(text)
	file:close()
end
function fwriteFromTable(path, t)
	local text = ""
	for _, line in pairs(t) do
		text = text..line.."\n"
	end
	fwrite(path, text)
end
local function getTable(path)
	if fs.exists(path) then
		local file = io.open(path, "r")
		local lines = {}
		local i = 1
		local line = file:read("*l")
		while line ~= nil do
			lines[i] = line
			line = file:read("*l")
			i = i + 1
		end
		file:close()
		return lines
	end
	return {}
end
function replaceLine(path, n, text)
	local lines = getTable(path)
	lines[n] = text
	fwriteFromTable(path, lines)
end

-- Variables

local bootCONF = getTable(".boot/.conf")
local cRunning = true
local rDir = string.sub(bootCONF[2],string.find(bootCONF[2],":")+2)
local conf = getTable(rDir.."/.var/.sys/.DS_data/.config")
local iConLink = getTable(rDir.."/.var/.img/.icons/.iconlink")
local iConName = getTable(rDir.."/.var/.img/.icons/.iconname")
local iConRun = getTable(rDir.."/.var/.img/.icons/.iconrun")
local iconLocation = getTable(rDir.."/.var/.img/.icons/.iconloc")
local bgImg1 = "Documents/Pictures/Wallpapers/"..string.sub(conf[13],string.find(conf[13],":")+2)
local tBarCol = tonumber(string.sub(conf[1],string.find(conf[1],":")+2))
local tBarTextCol = tonumber(string.sub(conf[2],string.find(conf[2],":")+2))
local clockCol = tonumber(string.sub(conf[3],string.find(conf[3],":")+2))
local menuCol = tonumber(string.sub(conf[4],string.find(conf[4],":")+2))
local menuTextCol = tonumber(string.sub(conf[5],string.find(conf[5],":")+2))
local desktopCol = tonumber(string.sub(conf[6],string.find(conf[6],":")+2))
local desktopTextCol = tonumber(string.sub(conf[7],string.find(conf[7],":")+2))
local desktopTbgCol = tonumber(string.sub(conf[12],string.find(conf[12],":")+2))
local Screensaver = string.sub(conf[8],string.find(conf[8],":")+2)
local desktopIcons = string.sub(conf[9],string.find(conf[9],":")+2)
local allowTerm = string.sub(conf[10],string.find(conf[10],":")+2)
local cVer = string.sub(conf[11],string.find(conf[11],":")+2)
local uVer = string.sub(conf[18],string.find(conf[18],":")+2)
local netCon = string.sub(conf[19],string.find(conf[19],":")+2)
local bgs = fs.list("Documents/Pictures/Wallpapers/")
local hotBarIcon1 = rDir.."/"..iConLink[1]
local hotBarIcon2 = rDir.."/"..iConLink[2]
local hotBarIcon3 = rDir.."/"..iConLink[3]
local hotBarIcon4 = rDir.."/"..iConLink[4]
local bRunning = true
local cmx = 0
local ax = 8
local ay = 3
local cX,cY = 0,0
os.loadAPI(rDir.."/.var/.sys/kernal")
local tmpErrPrnt = paintutils.loadImage(rDir.."/.var/.err/.errImg")


-- Functions & Tables
local tPrint = function(xPos,yPos,text)
term.setCursorPos(xPos,yPos)
write(text)
end

local background = function(colour)
term.setBackgroundColour(colour)
end

local cText = function(xPos,yPos,text,colour)
term.setTextColour(colour)
tPrint(xPos,yPos,text)
end

local genList = function()
local fsDir = fs.list(".boot")
cpos = 15
for i,v in pairs(fsDir) do
	if i <= #fsDir then
		term.setCursorPos(cpos,9)
		print(v)
		cpos = cpos + 10
	end
end
end

local notifications = function()
if netCon ~= "true" then
network = false
else
network = true
end
if cVer ~= uVer then
update = true
end
if network then
	cText(math.floor(w-6),1,"((i))",1)
	cText(math.floor(w-4),1,"i",colours.black)
		if update then
		cText(math.floor(w-11),1,"(!)",1)
		end
elseif not network then
	if update then
	cText(math.floor(w-4),1,"(!)",1)
	end
end
end

local function tempErr(erMsg)
term.setBackgroundColor(128)
term.setTextColor(1)
term.clear()
term.setCursorPos(math.floor(w-string.len("Oh No! Something is horribly wrong! :("))/2, 1)
write("Oh No! Something is horribly wrong! :(")
term.setCursorPos(math.floor(w-string.len("Please oh pretty please tell NDFJay this Error..."))/2, 2)
write("Please oh pretty please tell NDFJay this Error...")
term.setCursorPos(math.floor(w-string.len(tostring(erMsg)))/2, 4)
print(tostring(erMsg))
term.setCursorPos(2, math.floor(h-4))
print("Send Error Report")
print("")
print(" Save Error Log")
paintutils.drawImage(tmpErrPrnt,math.floor(w-string.len("  88888888888888888  "))-2, math.floor(h)-8)
	while true do
	local event, button, X, Y = os.pullEventRaw()
		if event == "mouse_click" then
			if	X >2 and X <= string.len("Send Error Report") and Y == math.floor(h-4) and button == 1 then
				term.setBackgroundColor(128)
				term.clear()
				term.setCursorPos(math.floor(w-string.len("Error Report Sent!"))/2, 2)
				http.post(
				 "http://ndfos.ndfjay.co.uk/errcatch/NDFUIerrcatch.php?message="..textutils.urlEncode("Error Caught!\n"..tostring(erMsg))
				  )
				print("Error Report Sent!")
				term.setCursorPos(1, math.floor(h))
				term.setBackgroundColor(colors.black)
				term.clearLine()
				break
					elseif	X >2 and X <= string.len("Save Error Log") and Y == math.floor(h-2) and button == 1 then
					term.setBackgroundColor(128)
					term.clear()
					term.setCursorPos(math.floor(w-string.len('Error log saved to "//root/.errLog"'))/2, 2)
					if not fs.exists(".errLog") then
					fwrite(".errLog"," ")
					end
					kernal.fwriteAtStart(".errLog",string.rep("=",40))
					kernal.fwriteAtStart(".errLog"," ")
					kernal.fwriteAtStart(".errLog","--"..tostring(erMsg))
					kernal.fwriteAtStart(".errLog"," ")
					kernal.fwriteAtStart(".errLog",string.rep("=",40))
					kernal.fwriteAtStart(".errLog"," ")
					print('Error log saved to "//root/.errLog"')
					term.setCursorPos(1, math.floor(h))
					term.setBackgroundColor(colors.black)
					term.clearLine()
					break
			end
		end
	end
end
local cg_abt_txt = {
"                            [_][=][X]";
"                                     "; 
"    NDF-UI is made by NDFJay with    ";
"                                     ";
"  Most of the programs featured are  ";
"            by other Devs            ";
"                                     ";
"             Credits to              ";
"    TheOriginalBIT - ScreenSavers    ";
"    NitrogenFingers - nPaintPro      ";
"    BigShinyToys - File Browser      ";
"    Grimm Reaper - Notepad           ";
"                                     "
}
local function cg_About()
for i,v in pairs(cg_abt_txt) do
term.setTextColour(1)
term.setBackgroundColour(256)
	term.setCursorPos(ax,ay+i)
	write(v)
end
term.setCursorPos(ax,ay+1)
term.setBackgroundColour(128)
term.setTextColour(8)
write(cg_abt_txt[1])
end

local cg_Apps_txt = {
"                            [_][=][X]";
"                                     "; 
"     Games  < Launcher >   Programs  ";
"                                     ";
"  Speeder     |      |     FireWolf  ";
"  =========== |      | ============  ";
"  Numines     |      |    nPaintPro  ";
"  =========== |      | ============  ";
"                     |    LightShot  ";
"                     | ============  ";
"                     |    ccYouTube  ";
"                     | ============  ";
"                                     "
}

local input = {
"                                     ";
"                                     "; 
"                                     ";
"                                     "
}

local contextMenu1 = {
"         ";
" Icons   ";
" Config  ";
" Notepad ";
"         "
}

local cg_Context = function(mouseX,mouseY,t)
if mouseX > w-#t[1] then
	cX = w-#t[1]
else
cX = mouseX
end
if mouseY > h-table.getn(t) then
cY = h-3-table.getn(t)
else
cY = mouseY
end
for i,v in pairs(t) do
term.setBackgroundColour(128)
term.setTextColour(1)
term.setCursorPos(cX,cY+i)
write(v)
end
end

local cg_Input = function(txt)
for i,v in pairs(input) do
term.setTextColour(1)
term.setBackgroundColour(256)
	term.setCursorPos(ax,ay+i+6)
	write(v)
end
term.setCursorPos(ax,ay+6)
term.setBackgroundColour(128)
term.setTextColour(8)
write(txt)
end

local function cg_Apps()
for i,v in pairs(cg_Apps_txt) do
term.setTextColour(1)
term.setBackgroundColour(256)
	term.setCursorPos(ax,ay+i)
	write(v)
end
term.setCursorPos(ax,ay+1)
term.setBackgroundColour(128)
term.setTextColour(8)
write(cg_Apps_txt[1])
end
local startMenuOpt = {
[" #########"] = {x=2, y=2, bg=menuCol, txt=menuCol, action=function() end},
["Run       "] = {x=2, y=3, bg=menuCol, txt=menuTextCol, action=function() end},
["Explorer  "] = {x=2, y=4, bg=menuCol, txt=menuTextCol, action=function() shell.run(rDir.."/.apps/system/explorer","--dir","Documents") cmx = 0 os.queueEvent("mouse_click") end},
["Settings >"] = {x=2, y=5, bg=menuCol, txt=menuTextCol, action=function() end},
["Restart   "] = { x=2, y=6, bg=menuCol, txt=menuTextCol, action=os.reboot},
["Shutdown  "] = { x=2, y=7, bg=menuCol, txt=menuTextCol, action=os.shutdown},
["CraftOS   "] = { x=2, y=8, bg=menuCol, txt=menuTextCol, action=function() term.setCursorPos(1,1) term.setBackgroundColour(colors.black) term.clear() bRunning = false end},
["    ######"] = {x=2, y=9,bg=menuCol, txt=menuCol, action=function() end},
["About     "] ={x=2, y=10,bg=menuCol, txt=menuTextCol, action=function() cmx = 3 end},
["   #######"] = {x=2, y=11,bg=menuCol, txt=menuCol, action=function() end}
}
local function drawStartMenuOpt()
  for k,v in pairs(startMenuOpt) do
        term.setCursorPos(v.x-1,v.y)
        term.setBackgroundColor(v.bg)
		print(string.rep(" ",12))
		term.setCursorPos(v.x,v.y)
        term.setTextColor(v.txt)
        write(k)
  end
end
local accessoriesMenuOpts = {
["#############"] = {x=14, y=2, bg=menuCol, txt=menuCol, action=function() end},
["Applications "] = {x=14, y=3, bg=menuCol, txt=menuTextCol, action=function() cmx = 4 end},
["Games        "] = {x=14, y=4, bg=menuCol, txt=menuTextCol, action=function() cmx = 5 end},
["#######      "] = {x=14, y=5, bg=menuCol, txt=menuCol, action=function() end}
}
local function drawaccessoriesMenuOpts()
  for k,v in pairs(accessoriesMenuOpts) do
        term.setCursorPos(v.x-1,v.y)
        term.setBackgroundColor(v.bg)
		print(string.rep(" ",12))
		term.setCursorPos(v.x,v.y)
        term.setTextColor(v.txt)
        write(k)
  end
end
local function startMenuOptHit(mouseX, mouseY)
  for k,v in pairs(startMenuOpt) do
        if mouseX >= v.x and mouseX < v.x + #k and mouseY == v.y then
          v.action()
          return true
        end
  end
  cmx = 0
  return false

end
local function accessoriesMenuOptsHit(mouseX, mouseY)
  for k,v in pairs(accessoriesMenuOpts) do
        if mouseX >= v.x and mouseX < v.x + #k and mouseY == v.y then
          v.action()
          return true
        end
  end
  amx = 0
  return false

end
local dIC = {
[iConName[1]] = {xline=1, yline=2, x=tonumber(iconLocation[1]) ,y=tonumber(iconLocation[2]) ,location=rDir.."/"..iConLink[1], action=function() shell.run(rDir.."/"..iConRun[1]) end},
[iConName[2]] = {xline=3, yline=4, x=tonumber(iconLocation[3]) ,y=tonumber(iconLocation[4]) ,location=rDir.."/"..iConLink[2], action=function() shell.run(rDir.."/"..iConRun[2]) end},
[iConName[3]] = {xline=5, yline=6, x=tonumber(iconLocation[5]) ,y=tonumber(iconLocation[6]) ,location=rDir.."/"..iConLink[3], action=function() shell.run(rDir.."/"..iConRun[3]) end},
[iConName[4]] = {xline=7, yline=8, x=tonumber(iconLocation[7]) ,y=tonumber(iconLocation[8]) ,location=rDir.."/"..iConLink[4], action=function() shell.run(rDir.."/"..iConRun[4]) end},
[iConName[5]] = {xline=9, yline=10, x=tonumber(iconLocation[9]) ,y=tonumber(iconLocation[10]) ,location=rDir.."/"..iConLink[5], action=function() shell.run(rDir.."/"..iConRun[5]) end},
[iConName[6]] = {xline=11, yline=12, x=tonumber(iconLocation[11]) ,y=tonumber(iconLocation[12]) ,location=rDir.."/"..iConLink[6], action=function() shell.run(rDir.."/"..iConRun[6]) end},
[iConName[7]] = {xline=13, yline=14, x=tonumber(iconLocation[13]) ,y=tonumber(iconLocation[14]) ,location=rDir.."/"..iConLink[7], action=function() shell.run(rDir.."/"..iConRun[7]) end},
[iConName[8]] = {xline=15, yline=16, x=tonumber(iconLocation[15]) ,y=tonumber(iconLocation[16]) ,location=rDir.."/"..iConLink[8], action=function() shell.run(rDir.."/"..iConRun[8]) end},
[iConName[9]] = {xline=17, yline=18, x=tonumber(iconLocation[17]) ,y=tonumber(iconLocation[18]) ,location=rDir.."/"..iConLink[9], action=function() shell.run(rDir.."/"..iConRun[9]) end},
[iConName[10]] = {xline=19, yline=20, x=tonumber(iconLocation[19]) ,y=tonumber(iconLocation[20]) ,location=rDir.."/"..iConLink[10], action=function() shell.run(rDir.."/"..iConRun[10]) end}
}
local drawIcons = function()
local nIcons = 0
for k,v in pairs(dIC) do
if k ~= "None" then
nIcons = nIcons+1
	paintutils.drawImage(paintutils.loadImage(v.location),v.x,v.y)
	term.setCursorPos(v.x,v.y+5)
	term.setBackgroundColour(desktopTbgCol)
	term.setTextColour(desktopTextCol)
	write(k)
	
end
end
end
local hitIC = function(mouseX, mouseY, newX, newY)
	for k,v in pairs(dIC) do
	if k ~= "None" then
		if mouseX >= v.x-2 and mouseX <= v.x+5 and mouseY >= v.y-2 and mouseY <= v.y+1 then
			if newY >= math.floor(h-6) then
				v.y = math.floor(h-6)
			else
			v.y = newY
		end
			v.x = newX
			replaceLine(rDir.."/.var/.img/.icons/.iconloc",v.xline,newX)
			replaceLine(rDir.."/.var/.img/.icons/.iconloc",v.yline,newY)
			drawIcons()
		end
	end
	end
	return false
end
local runIC = function(mouseX, mouseY)
for k,v in pairs(dIC) do
	if k ~= "None" then
		if mouseX >= v.x and mouseX < v.x + 6 and mouseY >= v.y+2 and mouseY <= v.y+5 then
          v.action() -- this runs the function stored in the table!		  
          return true -- no need to check anymore, they clicked it!
        end
	end
end
return false -- none clicked
end

local function cg_Draw()
term.setBackgroundColor(desktopCol)
term.clear()
paintutils.drawImage(paintutils.loadImage(bgImg1),1,1)
term.setBackgroundColor(tBarCol)
term.setTextColour(tBarTextCol)
term.setCursorPos(2,1)
term.clearLine()
print("[File]     [Apps]")
notifications()
drawIcons()
background(128)
term.setCursorPos(1,math.floor(h))
term.clearLine()
cText(1,math.floor(h)," <<<",1)
cText(math.floor(w-#" >>>"),math.floor(h),">>> ",1)
cText(math.floor(w-#"Background Image")/2,math.floor(h),"Background Image",1)
drawIcons()
end
local bgList = function()
local conf = ""
local conf = getTable(rDir.."/.var/.sys/.DS_data/.config")
local bgs = ""
local bgs = fs.list("Documents/Pictures/Wallpapers/")
	for i,v in pairs(bgs) do
		if string.sub(conf[13],string.find(conf[13],":")+2) == v then
		bI = i
		end
	end
background(1)
cText(math.floor(w-string.len("Alpha: "..cVer)),math.floor(h-1),"Alpha: "..cVer,128)
end

local hitContext1 = function(mouseX, mouseY,t)
if mouseX >= cX and mouseX <= cX+#t[1] and mouseY == cY+2 then

elseif mouseX >= cX and mouseX <= cX+#t[1] and mouseY == cY+3 then
shell.run(rDir.."/.apps/programs/LuaIDE",rDir.."/.var/.sys/.DS_data/.config")
cmx = 0
os.queueEvent("mouse_click")
elseif mouseX>=cX and mouseX <= cX+#t[1] and mouseY == cY+4 then
cmx = 0
cg_Draw()
shell.run(rDir.."/.apps/programs/LuaIDE")
os.queueEvent("mouse_click")
end
end

local changeBG = function(mouseX)
bgList()
if mouseX <= 4 then
bI = bI - 1
	if bI <= 1 then 
		bI = 1
	end
if not fs.exists("Documents/Pictures/Wallpapers/"..bgs[bI]) then
bgImg1 = "Documents/pictures/Wallpapers/GrassBlock.bg"
cg_Draw()
replaceLine(rDir.."/.var/.sys/.DS_data/.config",13,"Desktop Background Image: GrassBlock.bg")
else
bgImg1 = "Documents/pictures/Wallpapers/"..bgs[bI]
cg_Draw()
replaceLine(rDir.."/.var/.sys/.DS_data/.config",13,"Desktop Background Image: "..bgs[bI])
end
elseif mouseX >= 4 then
bI = bI + 1
	if bI >= table.getn(bgs) then 
		bI = table.getn(bgs)
	end
if not fs.exists("Documents/Pictures/Wallpapers/"..bgs[bI]) then
bgImg1 = "Documents/pictures/Wallpapers/GrassBlock.bg"
cg_Draw()
replaceLine(rDir.."/.var/.sys/.DS_data/.config",13,"Desktop Background Image: GrassBlock.bg")
else
bgImg1 = "Documents/pictures/Wallpapers/"..bgs[bI]
cg_Draw()
replaceLine(rDir.."/.var/.sys/.DS_data/.config",13,"Desktop Background Image: "..bgs[bI])
end
end
end

term.clear()

--Main Loop

local function main()

if string.sub(conf[14],string.find(conf[14],":")+2) == "false" then
	shell.run(rDir.."/.var/.sys/.DS_data/.DS_login")
	return
end
cg_Draw()
while bRunning do
bgList()
if cmx == 0 then
local event = {os.pullEventRaw()}
if event[1] == "terminate" then
	cg_Draw()
	cmx = 0
end
if event[1] == "mouse_drag" then
if cmx == 0 then
	if event[2] == 1 then
		hitIC(event[3], event[4], event[3], event[4])
		cg_Draw()
	end	
end
end
if event[1] == "mouse_click" then
cg_Draw()
if event[2] == 2 then
	cg_Context(event[3],event[4],contextMenu1)
	cmx = 5
end
if event[2] == 1 then
runIC(event[3], event[4])
		if event[3] >=2 and event[3] <= string.len("[File]") and event[4] == 1 then
			cmx = 1
			amx = 0
			elseif event[3] >= string.len("[File]        ") and event[3] <= string.len("[File]       [Apps]") and event[4] == 1 then
			cmx = 4
				elseif event[4] == math.floor(h) then
					changeBG(event[3])
			else
			cRunning = true
	    end
	end
end
elseif cmx==1 then
amx = 0
drawStartMenuOpt()
local event = {os.pullEventRaw()}
if event[1] == "terminate" then
	cg_Draw()
	cmx = 0
end
  if event[1] == "mouse_click" then
	  if event[2] == 1 then		
		startMenuOptHit(event[3], event[4])
			if not startMenuOptHit(event[3], event[4]) then
			cg_Draw()
			end
	  end
  end
elseif cmx == 3 then
cg_Draw()
cg_About()
local event = {os.pullEventRaw()}
if event[1] == "terminate" then
	cg_Draw()
	cmx = 0
end
  if event[1] == "mouse_click" then
	  if event[2] == 1 then	
		if event[3] >= ax+#cg_abt_txt[1]-3 and event[3] <= ax+#cg_abt_txt[1]  and event[4] ==ay+1 then
			cmx = 0
			ax = 8
			ay = 3
			cg_Draw()
		end
	  end
	elseif event[1] == "mouse_drag" then
		if event[2] == 1 then
			if event[3]>= ax-2 and event[3] <= ax+#cg_abt_txt[1]-8 and event[4] >= ay and event[4] <=ay+2 then
			if	event[3]-1 > math.floor(w-#cg_abt_txt[1]) then
				ax = math.floor(w-#cg_abt_txt[1])+1
				else
				ax = event[3]
			end
			if event[4] == 1 then
			ay = 1
			else
			ay = event[4]-1
			end
			cg_Draw()
			cmx = 3
			end
		end
	end
elseif cmx == 4 then
cg_Apps()
	local event = {os.pullEventRaw()}
	if event[1] == "terminate" then
		cmx = 0
		cg_Draw()
	end
	if event[1] == "mouse_click" then
		if event[2] == 1 then
			if event[3] >= ax+#cg_Apps_txt[1]-3 and event[3] <= ax+#cg_Apps_txt[1]  and event[4] ==ay+1 then
				cmx = 0
				ax = 8
				ay = 3
				cg_Draw()
				elseif event[3] >= ax and event[3] <= ax+15 and event[4] >= ay+5 and event[4] <= ay+6 then
					shell.run(rDir.."/.apps/games/Speeder/Speeder")
				elseif event[3] >= ax and event[3] <= ax+15 and event[4] >= ay+7 and event[4] <= ay+8 then
					shell.run(rDir.."/.apps/games/numines/numines")
				elseif event[3] >= ax+22 and event[3] <= ax+#cg_Apps_txt[1] and event[4] >= ay+5 and event[4] <= ay+6 then
					shell.run(rDir.."/.apps/programs/firewolf")
				elseif event[3] >= ax+22 and event[3] <= ax+#cg_Apps_txt[1] and event[4] >= ay+7 and event[4] <= ay+8 then
					cmx = 0
					cg_Draw()
					cg_Input("<nPaintPro Launcher>    Name of file:")
					term.setCursorPos(ax+1,ay+8)
					term.setBackgroundColor(256)
					term.setTextColour(128)
					arg = "Documents/Pictures/"..read()
					shell.run(rDir.."/.apps/programs/npaintpro.nse",arg)
					os.queueEvent("mouse_click")
				elseif event[3] >= ax+22 and event[3] <= ax+#cg_Apps_txt[1] and event[4] >= ay+9 and event[4] <= ay+10 then
					cmx = 0
					cg_Draw()
					cg_Input("<LightShot Launcher>    Name of file:")
					term.setCursorPos(ax+1,ay+8)
					term.setBackgroundColor(256)
					term.setTextColour(128)
					arg = "Documents/Videos/"..read()
					shell.run(rDir.."/.apps/programs/lightshot",arg)
					os.queueEvent("mouse_click")
				elseif event[3] >= ax+22 and event[3] <= ax+#cg_Apps_txt[1] and event[4] >= ay+11 and event[4] <= ay+12 then
					shell.run(rDir.."/.apps/programs/ccYouTube") 
					cmx = 0
					os.queueEvent("mouse_click")
			end
		end
	elseif event[1] == "mouse_drag" then
		if event[2] == 1 then
			if event[3]>= ax-2 and event[3] <= ax+#cg_Apps_txt[1]-8 and event[4] >= ay and event[4] <=ay+2 then
			if	event[3]-1 > math.floor(w-#cg_Apps_txt[1]) then
				ax = math.floor(w-#cg_Apps_txt[1])+1
				else
				ax = event[3]
			end
			if event[4] == 1 then
			ay = 1
			else
			ay = event[4]-1
			end
			cg_Draw()
			cmx = 4
			end
		end
	end
elseif cmx == 5 then
local e = {os.pullEventRaw()}
	if e[1] == "terminate" then
		cmx = 0
		cg_Draw()
	end
	if e[1] == "mouse_click" then
		if e[2] == 1 then
			hitContext1(e[3],e[4],contextMenu1)
			cmx = 0
			cg_Draw()
			elseif e[2] == 2 then
				cmx = 0
				cg_Draw()
				cmx = 5
				cg_Context(e[3],e[4],contextMenu1)
		end
	end
end
end
return true
end

-- Error Catching

local ok, err = pcall(main)
if not ok then
	cRunning = false
  tempErr(err)
end
