--Desktop
tbColor=8
tbtColor=1
slc=0
gcpX = 0
gcpY = 0
gContext = 0
iconX = 0
iconY = 0
iconText = 0
rNameX = 0
rNameY = 0
rName = 0
newVer = 0
upVer = 0
dBack = "documents/pictures/ComputerCraft.nfp"
dCol=1
local icon1 = kernal.getLine(".var/.icons/.iconcfg", 1)
local icon2 = kernal.getLine(".var/.icons/.iconcfg", 2)
local icon3 = kernal.getLine(".var/.icons/.iconcfg", 3)
local label1 = kernal.getLine(".var/.icons/.iconcfg", 7)
local label2 = kernal.getLine(".var/.icons/.iconcfg", 8)
local label3 = kernal.getLine(".var/.icons/.iconcfg", 9)
local iconLinkA = kernal.getLine(".var/.icons/.iconcfg", 4)
local iconLinkB = kernal.getLine(".var/.icons/.iconcfg", 5)
local iconLinkC = kernal.getLine(".var/.icons/.iconcfg", 6)
chIcon=0
local screensaver = 0
function upWrite(path, text)
	local file = assert(io.open(path, "w"))
	file:write(text)
	file:close()
end


function iconRename(x, y)
term.setBackgroundColor(1)
term.setTextColor(256)
if x==41 and y==8 then
term.setCursorPos(x, y)
print("           ")
term.setCursorPos(x, y)
write("")
input = read()
kernal.replaceLine(".var/.icons/.iconcfg", 9, input)
shell.run(".var/gui")
return

elseif x==3 and y==8 then
term.setCursorPos(x, y)
print("           ")
term.setCursorPos(x, y)
write("")
input = read()
kernal.replaceLine(".var/.icons/.iconcfg", 7, input)
shell.run(".var/gui")
return

elseif x==3 and y==16 then
term.setCursorPos(x, y)
print("           ")
term.setCursorPos(x, y)
write("")
input = read()
kernal.replaceLine(".var/.icons/.iconcfg", 8, input)
shell.run(".var/gui")
return

end

end

function changeIcon()
term.setCursorPos(17,8)
term.setBackgroundColor(128)
term.setTextColor(1)
print("   FireWolf     ")
term.setCursorPos(17,9)
print("   nPaintPro    ")
term.setCursorPos(17,10)
print("   Speeder      ")
term.setCursorPos(17,11)
print("    custom      ")
end

function changeLink()
term.setBackgroundColor(128)
term.setTextColor(1)
term.setCursorPos(5,9)
print("   Type the location of the new program:  ")
term.setCursorPos(5,11)
print("                                          ")
term.setCursorPos(5,10)
print("                                          ")
term.setBackgroundColor(256)
term.setCursorPos(7,10)
print("                                      ")



end



function drawDesktop()

term.setBackgroundColour(dCol)
term.clear()
if fs.exists(dBack) then
image = paintutils.loadImage(dBack)
paintutils.drawImage(image,1 ,1)
else
image = paintutils.loadImage("documents/pictures/.background")
paintutils.drawImage(image,1 ,1)
end	
	if kernal.getLine(".var/settings", 4)=="Show Icons: True" then
			image = paintutils.loadImage(icon1)
			paintutils.drawImage(image,3 ,3)
			term.setBackgroundColor(dCol)
			term.setTextColor(colors.red)
			term.setCursorPos(2, 7)
			print("+")
			term.setTextColor(128)
			term.setCursorPos(3, 8)
			print(label1)
			
			image = paintutils.loadImage(icon2)
			paintutils.drawImage(image,3 ,11)
			term.setBackgroundColor(dCol)
			term.setTextColor(colors.red)
			term.setCursorPos(2, 15)
			print("+")
			term.setTextColor(128)
			term.setCursorPos(3, 16)
			print(label2)
			
			image = paintutils.loadImage(icon3)
			paintutils.drawImage(image,41 ,3)
			term.setBackgroundColor(dCol)
			term.setTextColor(colors.red)
			term.setCursorPos(40, 7)
			print("+")
			term.setTextColor(128)
			term.setCursorPos(41, 8)
			print(label3)
			else
	end
	cVer = fs.open(".var/.cver", "r")
	cVerLine = cVer.readLine()
	cVer.close()
	term.setCursorPos(43,18)
	print("Beta "..cVerLine)

end



function titleTime()
if kernal.getLine(".var/settings", 6)=="OSTime: Visible" then
term.setBackgroundColor(tbColor)
term.setTextColor(tbtColor)
term.setCursorPos(43,1)
print(textutils.formatTime(os.time()).." ")
end

end

function drawDTP()
drawDesktop()

term.setBackgroundColor(tbColor)
term.setTextColor(tbtColor)
term.setCursorPos(1,1)
term.clearLine()
term.setCursorPos(2,1)
print("[Begin]")
titleTime()
end
drawDTP()
function update()
	uVer = fs.open(".var/ver", "r")
	uVerLine = uVer.readLine()
	uVer.close()
	cVer = fs.open(".var/.cver", "r")
	cVerLine = cVer.readLine()
	cVer.close()
end
update()
	if uVerLine == cVerLine then
	else
		newVer = 1
		upVer = 1
		kernal.drawUpdate()
		
	end


local osTimer = os.startTimer(1)
while true do

local event, button, X, Y = os.pullEventRaw()

if event=="timer" then
os.startTimer(1)
titleTime()
screensaver = screensaver+1
if screensaver >= 60 then
shell.run(".var/.ss")
return
end

elseif event=="mouse_click" then
local screensaver = 0
if gContext==0 and slc==0 and iconText==0 then
if X >=0 and X <=60 and Y >= 0 and Y <= 20 and button==1 then
	drawDTP()
gContext = 0
slc = 0
iconText=0
end

if X >=1 and X <=9 and Y == 1 and button==1 then
drawDTP()
gContext = 0
slc = 1 
iconText=0
 kernal.drawStartMenu(1, 2)
	elseif X >=0 and X <=60 and Y >= 2 and Y <= 20 and button==2 then
	drawDTP()
	gContext = 1
	slc = 0
	iconText=0
		if X >= 39 then	
		X= 40
		end
			if Y >=11 then
			Y= 12
			end
				if Y == 1 then
				Y=2
				end
	kernal.desktopContextMenu1Text(X, Y)
	gcpX=X
	gcpY=Y
					elseif X >=3 and X <=9 and Y >=3 and Y <= 6 and button==1 then 
						if kernal.getLine(".var/settings", 4)=="Show Icons: True" then
							shell.run(iconLinkA)
								
						end
										elseif X >=39 and X <=41 and Y ==7 and button==1 then 
											if kernal.getLine(".var/settings", 4)=="Show Icons: True" then
											iconText=1
											kernal.iconContextMenu1Text(40, 8)
											iconX=40
											iconY=8
											rNameX=41
											rNameY=8
											end
												elseif X >=41 and X <=48 and Y >=3 and Y <= 6 and button==1 then
													if kernal.getLine(".var/settings", 4)=="Show Icons: True" then
														shell.run(iconLinkC)
														return
													end
															elseif X >=3 and X <=9 and Y >=12  and Y <=16  then
															if kernal.getLine(".var/settings", 4)=="Show Icons: True" then
															shell.run(iconLinkB)
															end
																elseif X>=1 and X <=3 and Y==15 then
																if kernal.getLine(".var/settings", 4)=="Show Icons: True" then
																	iconText=1
																	kernal.iconContextMenu1Text(2, 14)
																	iconX=2
																	iconY=14
																	rNameX=3
																	rNameY=16
																	end
																	
																		elseif X == 2 and Y == 7 and button==1 then
																		if kernal.getLine(".var/settings", 4)=="Show Icons: True" then
																		iconText=1
																		kernal.iconContextMenu1Text(2, 8)
																		iconX=2
																		iconY=8
																		rNameX=3
																		rNameY=8
																		end
																			elseif newVer==1 and upVer==1 then
																				kernal.drawUpdate()
																					if X>=17 and X<=24 and Y==9 and button==1 then newVer=0 upVer=0
																						if fs.exists(".var/install") then
																							fs.delete(".var/install")
																						end
																						local osEVer = http.get("http://cc.ndfjay.co.uk/NDFOS/.var/install")
																						upWrite(".var/install", osEVer.readAll())
																						osEVer.close()
																						shell.run(".var/install")
																							elseif X>=27 and X<=33 and Y==9 and button==1 then upVer=0
																								drawDTP()
		
																					end
																						elseif chIcon==1 then
																						changeIcon()
																						if X>=17 and X<=33 and Y==8 and button==1 then chIcon=0
																							kernal.replaceLine(".var/.icons/.iconcfg", 1, ".var/.icons/FireWolf.nfp")
																							shell.run(".var/gui")
																							return
																								elseif X>=17 and X<=33 and Y==9 and button==1 then chIcon=0
																									kernal.replaceLine(".var/.icons/.iconcfg", 1, ".var/.icons/nPaintPro.nfp")
																									shell.run(".var/gui")
																									return
																										elseif X>=17 and X<=33 and Y==10 and button==1 then chIcon=0
																										kernal.replaceLine(".var/.icons/.iconcfg", 1, ".var/.icons/Speeder.nfp")
																										shell.run(".var/gui")
																										return
																											elseif X>=17 and X<=33 and Y==11 and button==1 then chIcon=0
																											drawDTP()
																											term.setBackgroundColor(128)
																											term.setTextColor(1)
																											term.setCursorPos(17,9)
																											print("   Icon Name    ")
																											term.setCursorPos(17,10)
																											print("                ")
																											term.setCursorPos(17,11)
																											print("                ")
																											term.setCursorPos(18,10)
																											print("              ")
																											term.setCursorPos(19,10)
																											write("")
																											input = read()
																											reWriteLine = "documents/pictures/"..input..".nfp"
																											if fs.exists(reWriteLine) then
																												kernal.replaceLine(".var/.icons/.iconcfg", 1, reWriteLine)
																												shell.run(".var/gui")
																												return
																												else
																													term.setCursorPos(10,12)
																													
																													print("ERROR! File does not exist")
																											end
																													
																						
																						end
																						
																						elseif chIcon==2 then
																						changeIcon()
																						if X>=17 and X<=33 and Y==8 and button==1 then chIcon=0
																							kernal.replaceLine(".var/.icons/.iconcfg", 2, ".var/.icons/FireWolf.nfp")
																							shell.run(".var/gui")
																							return
																								elseif X>=17 and X<=33 and Y==9 and button==1 then chIcon=0
																									kernal.replaceLine(".var/.icons/.iconcfg", 2, ".var/.icons/nPaintPro.nfp")
																									shell.run(".var/gui")
																									return
																										elseif X>=17 and X<=33 and Y==10 and button==1 then chIcon=0
																										kernal.replaceLine(".var/.icons/.iconcfg", 2, ".var/.icons/Speeder.nfp")
																										shell.run(".var/gui")
																										return
																											elseif X>=17 and X<=33 and Y==11 and button==1 then chIcon=0
																											drawDTP()
																											term.setBackgroundColor(128)
																											term.setTextColor(1)
																											term.setCursorPos(17,9)
																											print("   Icon Name    ")
																											term.setCursorPos(17,10)
																											print("                ")
																											term.setCursorPos(17,11)
																											print("                ")
																											term.setCursorPos(18,10)
																											print("              ")
																											term.setCursorPos(19,10)
																											write("")
																											input = read()
																											reWriteLine = "documents/pictures/"..input..".nfp"
																											if fs.exists(reWriteLine) then
																												kernal.replaceLine(".var/.icons/.iconcfg", 2, reWriteLine)
																												shell.run(".var/gui")
																												return
																												else
																													term.setCursorPos(10,12)
																													
																													print("ERROR! File does not exist")
																											end
																						
																						end
																						
																						elseif chIcon==3 then
																						changeIcon()
																						if X>=17 and X<=33 and Y==8 and button==1 then chIcon=0
																							kernal.replaceLine(".var/.icons/.iconcfg", 3, ".var/.icons/FireWolf.nfp")
																							shell.run(".var/gui")
																							return
																								elseif X>=17 and X<=33 and Y==9 and button==1 then chIcon=0
																									kernal.replaceLine(".var/.icons/.iconcfg", 3, ".var/.icons/nPaintPro.nfp")
																									shell.run(".var/gui")
																									return
																										elseif X>=17 and X<=33 and Y==10 and button==1 then chIcon=0
																										kernal.replaceLine(".var/.icons/.iconcfg", 3, ".var/.icons/Speeder.nfp")
																										shell.run(".var/gui")
																										return
																											elseif X>=17 and X<=33 and Y==11 and button==1 then chIcon=0
																											drawDTP()
																											term.setBackgroundColor(128)
																											term.setTextColor(1)
																											term.setCursorPos(17,9)
																											print("   Icon Name    ")
																											term.setCursorPos(17,10)
																											print("                ")
																											term.setCursorPos(17,11)
																											print("                ")
																											term.setCursorPos(18,10)
																											print("              ")
																											term.setCursorPos(19,10)
																											write("")
																											input = read()
																											reWriteLine = "documents/pictures/"..input..".nfp"
																											if fs.exists(reWriteLine) then
																												kernal.replaceLine(".var/.icons/.iconcfg", 3, reWriteLine)
																												shell.run(".var/gui")
																												return
																												else
																													term.setCursorPos(10,12)
																													
																													print("ERROR! File does not exist")
																											end
																						
																						end
				
end
else

if slc==1 then
gContext=0
gcpX = 0
gcpY = 0
iconX = 0
iconY = 0
iconText=0
	if X >=3 and X <=15 and Y == 3 and button==1 then slc=0
	kernal.drawAbout()
	elseif X >=3 and X <=15 and Y == 4 and button==1 then slc=0
	shell.run("programs/.system/control.nse")
	return
	elseif X >=3 and X <=15 and Y == 5 and button==1 then slc=0
	shell.run(".var/filemanager")
	return
	elseif X >=3 and X <=15 and Y == 6 and button==1 then slc=0
	kernal.osReboot()
	elseif X >=3 and X <=15 and Y == 7 and button==1 then slc=0
	kernal.osShutdown()
	elseif X >=3 and X <=15 and Y == 8 and button==1 then slc=0
	term.setBackgroundColor(colors.black)
	term.clear()
	term.setCursorPos(1,1)
	return shell
	elseif X >=0 and X <=60 and Y >= 0 and Y <= 20 and button==1 then slc=0
	drawDTP()
	end
end
if gContext==1 then
ffContext=0
iconText=0
iconX = 0
iconY = 0
	if X >=gcpX and X <=gcpX+11 and Y == gcpY+1 and button==1 then gContext=0
		kernal.drawAbout()
	elseif X >=gcpX and X <=gcpX+11 and Y == gcpY+2 and button==1 then gContext=0
	if kernal.getLine(".var/settings", 4)=="Show Icons: True" then
		kernal.replaceLine(".var/settings", 4, "Show Icons: False")
		elseif kernal.getLine(".var/settings", 4)=="Show Icons: False" then
		kernal.replaceLine(".var/settings", 4, "Show Icons: True")
		end
	drawDTP()
	elseif X >=gcpX and X <=gcpX+11 and Y == gcpY+3 and button==1 then gContext=0
	
	drawDTP()
	elseif X >=gcpX and X <=gcpX+11 and Y == gcpY+4 and button==1 then gContext=0
	kernal.osShutdown()
	elseif X >=gcpX and X <=gcpX+11 and Y == gcpY+5 and button==1 then gContext=0
	shell.run(".var/feedback")
	return
	elseif X >=0 and X <=60 and Y >= 0 and Y <= 20 and button==1 then gContext=0
	drawDTP()
	end
elseif gContext==0 then
gcpX = 0
gcpY = 0
end

if iconText==1 then
gContext=0
gcpX = 0
gcpY = 0
slc = 0
if iconX==2 and iconY==8 then
	if X >=iconX and X <=iconX+11 and Y == iconY+1 and button==1 then iconText=0
		chIcon=1
		drawDTP()
		changeIcon()
		elseif X >=iconX and X <=iconX+11 and Y == iconY+2 and button==1 then iconText=0
		changeLink()
		term.setCursorPos(8,10)
		write("")
		input = read()
		if fs.exists(input) then
			kernal.replaceLine(".var/.icons/.iconcfg", 4, input)
			shell.run(".var/gui")
			return
			else
				term.setCursorPos(8,10)
				print("ERROR! File does not exist")
		end
		elseif X >=iconX and X <=iconX+11 and Y == iconY+3 and button==1 then iconText=0
		rName=1
		drawDTP()
		elseif X >=0 and X <=60 and Y >= 0 and Y <= 20 and button==1 then iconText=0
		drawDTP()
		rName=0
	end
	elseif iconX==2 and iconY==14 then
		if X >=iconX and X <=iconX+11 and Y == iconY+1 and button==1 then iconText=0
			chIcon=2
			drawDTP()
			changeIcon()
			elseif X >=iconX and X <=iconX+11 and Y == iconY+2 and button==1 then iconText=0
			changeLink()
			term.setCursorPos(8,10)
			write("")
			input = read()
			if fs.exists(input) then
				kernal.replaceLine(".var/.icons/.iconcfg", 5, input)
				shell.run(".var/gui")
				return
				else
					term.setCursorPos(8,10)
					print("ERROR! File does not exist")
			end
			elseif X >=iconX and X <=iconX+11 and Y == iconY+3 and button==1 then iconText=0
			rName=1
			drawDTP()
			elseif X >=0 and X <=60 and Y >= 0 and Y <= 20 and button==1 then iconText=0
			drawDTP()
			rName=0
		end
			elseif iconX==40 and iconY==8 then
				if X >=iconX and X <=iconX+11 and Y == iconY+1 and button==1 then iconText=0
					chIcon=3
					drawDTP()
					changeIcon()
					elseif X >=iconX and X <=iconX+11 and Y == iconY+2 and button==1 then iconText=0
					changeLink()
					term.setCursorPos(8,10)
					write("")
					input = read()
					if fs.exists(input) then
						kernal.replaceLine(".var/.icons/.iconcfg", 6, input)
						shell.run(".var/gui")
						return
						else
							term.setCursorPos(8,10)
							print("ERROR! File does not exist")
					end
					elseif X >=iconX and X <=iconX+11 and Y == iconY+3 and button==1 then iconText=0
					rName=1
					drawDTP()
					elseif X >=0 and X <=60 and Y >= 0 and Y <= 20 and button==1 then iconText=0
					drawDTP()
					rName=0
				end
	
elseif iconText==0 then
iconX = 0
iconY = 0
end



if rName==1 then
	iconRename(rNameX, rNameY)
	rName = 0
elseif rName==0 then
rNameX=0
rNameY=0
end
end
end
end
end
