local w,h = term.getSize()

local fwrite = function(path, text)
	local file = assert(io.open(path, "w"))
	file:write(text)
	file:close()
end

local folders = {
".boot";
".ccYouTube";
".numinesData";
"Documents";
"NDF-UI";
".boot/.err";
".ccYouTube/apis";
".ccYouTube/downloads";
".ccYouTube/logos";
".ccYouTube/recycleBin";
"Documents/Other";
"Documents/Pictures";
"Documents/Pictures/Wallpapers";
"Documents/Projects";
"Documents/Text-Files";
"Documents/Videos";
"NDF-UI/.apps";
"NDF-UI/.apps/games";
"NDF-UI/.apps/home";
"NDF-UI/.apps/programs";
"NDF-UI/.apps/system";
"NDF-UI/.apps/games/numines";
"NDF-UI/.apps/games/speeder";
"NDF-UI/.apps/games/speeder/sprites";
"NDF-UI/.apps/games/speeder/world";
"NDF-UI/.var";
"NDF-UI/.var/.boot";
"NDF-UI/.var/.err";
"NDF-UI/.var/.img";
"NDF-UI/.var/.install";
"NDF-UI/.var/.sys";
"NDF-UI/.var/.boot/login";
"NDF-UI/.var/.img/.DS_data";
"NDF-UI/.var/.img/.icons";
"NDF-UI/.var/.sys/.DS_data";
"NDF-UI/.var/.sys/.keys"
}

local files = {
"startup";
".boot/.bios";
".boot/.conf";
".boot/boot";
".boot/CCatch";
".boot/leftos.ico";
".boot/ndfui.ico";
".boot/none.ico";
".boot/rightos.ico";
".boot/.err/.errImg";
".ccYouTube/apis/box";
".ccYouTube/apis/extStrLib";
".ccYouTube/apis/loadingScreen";
".ccYouTube/logos/ccytLogo";
".ccYouTube/logos/home";
".numinesData/bg1.nfa";
".numinesData/bg2.nfa";
".numinesData/gameutils";
"Documents/Pictures/Wallpapers/ComputerCraft.bg";
"Documents/Pictures/Wallpapers/GrassBlock.bg";
"Documents/Pictures/Wallpapers/NDFJay.bg";
"Documents/Pictures/Wallpapers/NDF-UI.bg";
"NDF-UI/.DS_draw";
"NDF-UI/.apps/games/numines/bg1.nfa";
"NDF-UI/.apps/games/numines/gameutils";
"NDF-UI/.apps/games/numines/numines";
"NDF-UI/.apps/games/speeder/sprites/1.nfp";
"NDF-UI/.apps/games/speeder/sprites/2.nfp";
"NDF-UI/.apps/games/speeder/sprites/3.nfp";
"NDF-UI/.apps/games/speeder/sprites/4.nfp";
"NDF-UI/.apps/games/speeder/sprites/5.nfp";
"NDF-UI/.apps/games/speeder/sprites/player.nfp";
"NDF-UI/.apps/games/speeder/world/road.nfp";
"NDF-UI/.apps/games/speeder/speeder";
"NDF-UI/.apps/games/speeder/Speeder.ico";
"NDF-UI/.apps/programs/ccYouTube";
"NDF-UI/.apps/programs/firewolf";
"NDF-UI/.apps/programs/lightshot";
"NDF-UI/.apps/programs/npaintpro.nse";
"NDF-UI/.apps/programs/overlay.nfp";
"NDF-UI/.apps/programs/word.nse";
"NDF-UI/.apps/system/explorer";
"NDF-UI/.apps/system/mouse.cfg";
"NDF-UI/.var/.boot/login/login.nfp";
"NDF-UI/.var/.boot/login/logo.nfp";
"NDF-UI/.var/.boot/login/overlay.nfp";
"NDF-UI/.var/.boot/login/userPic.nfp";
"NDF-UI/.var/.err/.errImg";
"NDF-UI/.var/.img/.DS_data/.DS_Backgrounds";
"NDF-UI/.var/.img/.icons/.iconlink";
"NDF-UI/.var/.img/.icons/.iconloc";
"NDF-UI/.var/.img/.icons/.iconname";
"NDF-UI/.var/.img/.icons/.iconrun";
"NDF-UI/.var/.img/.icons/icon.nfp";
"NDF-UI/.var/.install/install";
"NDF-UI/.var/.sys/kernal";
"NDF-UI/.var/.sys/.DS_data/.config";
"NDF-UI/.var/.sys/.DS_data/.DS_login";
"NDF-UI/.var/.sys/.keys/.passwords";
"NDF-UI/.var/.sys/.keys/.usernames"
}
local httpUrl = {
"http://downloads.ndfjay.co.uk/ndfui/startup";
"http://downloads.ndfjay.co.uk/ndfui/.boot/.bios";
"http://downloads.ndfjay.co.uk/ndfui/.boot/.conf";
"http://downloads.ndfjay.co.uk/ndfui/.boot/boot";
"http://downloads.ndfjay.co.uk/ndfui/.boot/CCatch";
"http://downloads.ndfjay.co.uk/ndfui/.boot/leftos.ico";
"http://downloads.ndfjay.co.uk/ndfui/.boot/ndfui.ico";
"http://downloads.ndfjay.co.uk/ndfui/.boot/none.ico";
"http://downloads.ndfjay.co.uk/ndfui/.boot/rightos.ico";
"http://downloads.ndfjay.co.uk/ndfui/.boot/.err/.errImg";
"http://downloads.ndfjay.co.uk/ndfui/.ccYouTube/apis/box";
"http://downloads.ndfjay.co.uk/ndfui/.ccYouTube/apis/extStrLib";
"http://downloads.ndfjay.co.uk/ndfui/.ccYouTube/apis/loadingScreen";
"http://downloads.ndfjay.co.uk/ndfui/.ccYouTube/logos/ccytLogo";
"http://downloads.ndfjay.co.uk/ndfui/.ccYouTube/logos/home";
"http://downloads.ndfjay.co.uk/ndfui/.numinesData/bg1.nfa";
"http://downloads.ndfjay.co.uk/ndfui/.numinesData/bg2.nfa";
"http://downloads.ndfjay.co.uk/ndfui/.numinesData/gameutils";
"http://downloads.ndfjay.co.uk/ndfui/Documents/Pictures/Wallpapers/ComputerCraft.bg";
"http://downloads.ndfjay.co.uk/ndfui/Documents/Pictures/Wallpapers/GrassBlock.bg";
"http://downloads.ndfjay.co.uk/ndfui/Documents/Pictures/Wallpapers/NDFJay.bg";
"http://downloads.ndfjay.co.uk/ndfui/Documents/Pictures/Wallpapers/NDF-UI.bg";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.DS_draw";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/games/numines/bg1.nfp";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/games/numines/gameutils";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/games/numines/numines";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/games/speeder/sprites/1.nfp";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/games/speeder/sprites/2.nfp";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/games/speeder/sprites/3.nfp";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/games/speeder/sprites/4.nfp";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/games/speeder/sprites/5.nfp";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/games/speeder/sprites/player.nfp";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/games/speeder/world/road.nfp";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/games/speeder/speeder";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/games/speeder/Speeder.ico";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/programs/ccYouTube";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/programs/firewolf";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/programs/lightshot";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/programs/npaintpro.nse";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/programs/overlay.nfp";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/programs/word.nse";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/system/explorer";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.apps/system/mouse.cfg";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.boot/login/login.nfp";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.boot/login/logo.nfp";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.boot/login/overlay.nfp";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.boot/login/userPic.nfp";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.err/.errImg";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.img/.DS_data/.DS_Backgrounds";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.img/.icons/.iconlink";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.img/.icons/.iconloc";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.img/.icons/.iconname";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.img/.icons/.iconrun";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.img/.icons/icon.nfp";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.install/install";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.sys/kernal";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.sys/.DS_data/.config";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.sys/.DS_data/.DS_login";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.sys/.keys/.passwords";
"http://downloads.ndfjay.co.uk/ndfui/NDF-UI/.var/.sys/.keys/.usernames"
}
local draw = function()
term.setBackgroundColour(1)
term.clear()
term.setTextColour(8)
term.setCursorPos(math.floor(w-#"Installing NDF-UI")/2,math.floor(h)/2-2)
write("Installing NDF-UI")
term.setCursorPos(math.floor(w-#"Please wait...")/2,math.floor(h)/2)
write("Please wait...")
end

draw()

if fs.exists("startup") then
term.setBackgroundColour(1)
term.setTextColour(8)
term.setCursorPos(math.floor(w-#"A Startup file has been found")/2,math.floor(h)/2+2)
write("A Startup file has been found")
sleep(2)
term.setCursorPos(math.floor(w-#"Renamed to   startup.old")/2,math.floor(h)/2+3)
write('Renamed to "startup.old"')
fs.copy("startup","startup.old")
fs.delete("startup")
sleep(2)
draw()
end

for i,v in pairs(folders) do
if fs.exists(v) then
fs.delete(v)
end
end

for i,v in pairs(folders) do
if not fs.exists(v) then
	fs.makeDir(v)
end
end

for i,v in pairs(files) do
download = http.get(httpUrl[i])
fwrite(v, download.readAll())
end

term.setBackgroundColour(1)
term.clear()
term.setTextColour(8)
term.setCursorPos(math.floor(w-#"Installation complete")/2,math.floor(h)/2-2)
write("Installation complete")
term.setCursorPos(math.floor(w-#"Press any key to reboot...")/2,math.floor(h)/2)
write("Press any key to reboot...")
while true do
e = {os.pullEventRaw()}
if e[1] == "key" then
	fs.delete("install")
	os.reboot()
end
end