local function update(text)
	term.setBackgroundColor(colors.black)
	term.setTextColor(colors.white)
	term.setCursorPos(1, 9)
	term.clearLine()
	term.setCursorPos(math.floor(51/2 - string.len(text)/2), 9)
	write(text)
end

local function bar(ratio)
	term.setBackgroundColor(colors.gray)
	term.setTextColor(colors.lime)
	term.setCursorPos(1, 11)
	
	for i = 1, 51 do
		if (i/51 < ratio) then
			write("]")
		else
			write(" ")
		end
	end
end

local function download(downloadPath, savePath)
	term.setBackgroundColor(colors.black)
	term.setTextColor(colors.white)
	term.setCursorPos(1, 13)
	term.clearLine()
	term.setCursorPos(1, 14)
	term.clearLine()
	term.setCursorPos(1, 15)
	term.clearLine()
	term.setCursorPos(1, 16)
	term.clearLine()
	term.setCursorPos(1, 17)
	term.clearLine()
	term.setCursorPos(1, 13)
	shell.run("pastebin run 3PBKGR4k gh Xelostar/CCDoom/master/"..downloadPath.." "..savePath)
end

local function pastebinD(code, savePath)
	term.setBackgroundColor(colors.black)
	term.setTextColor(colors.white)
	term.setCursorPos(1, 13)
	term.clearLine()
	term.setCursorPos(1, 14)
	term.clearLine()
	term.setCursorPos(1, 15)
	term.clearLine()
	term.setCursorPos(1, 16)
	term.clearLine()
	term.setCursorPos(1, 17)
	term.clearLine()
	term.setCursorPos(1, 13)
	shell.run("pastebin get "..code.." "..savePath)
end

term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.clear()

term.setCursorPos(1, 8)
write("Doom by Xelostar installer. Where do you want Doom to be installed? Path:")

term.setCursorPos(1, 11)
write("Path: ")
local path = read()

term.clear()
term.setCursorPos(13, 2)
term.setTextColor(colors.yellow)
write("Doom by Xelostar installer")

local oldpath = shell.dir()
shell.setDir("/")
local installpath = "/"..fs.combine(path, "Doom")
update("Installing...")
bar(0)

update("Creating directories...")
fs.makeDir(installpath)
bar(0.02)
fs.makeDir(fs.combine(installpath, "images"))
bar(0.04)
fs.makeDir(fs.combine(installpath, "levels"))
bar(0.06)
fs.makeDir(fs.combine(installpath, "models"))
bar(0.08)
update("Downloading license...")
download("LICENSE", "/"..fs.combine(installpath, "LICENSE"))
bar(0.10)
update("Downloading game...")
download("Doom/Doom", "/"..fs.combine(installpath, "Doom"))
bar(0.12)

update("Downloading APIs...")
download("Doom/ThreeD", "/"..fs.combine(installpath, "ThreeD"))
bar(0.17)
download("Doom/bufferAPI", "/"..fs.combine(installpath, "bufferAPI"))
bar(0.22)
pastebinD("ujchRSnU", "/"..fs.combine(installpath, "blittle"))
bar(0.27)

update("Downloading images...")
download("Doom/images/bfire", "/"..fs.combine(fs.combine(installpath, "images"), "bfire"))
bar(0.30)
download("Doom/images/bgun", "/"..fs.combine(fs.combine(installpath, "images"), "bgun"))
bar(0.33)
download("Doom/images/bgunf", "/"..fs.combine(fs.combine(installpath, "images"), "bgunf"))
bar(0.36)
download("Doom/images/bheart", "/"..fs.combine(fs.combine(installpath, "images"), "bheart"))
bar(0.39)
download("Doom/images/fire", "/"..fs.combine(fs.combine(installpath, "images"), "fire"))
bar(0.42)
download("Doom/images/gun", "/"..fs.combine(fs.combine(installpath, "images"), "gun"))
bar(0.45)
download("Doom/images/gunf", "/"..fs.combine(fs.combine(installpath, "images"), "gunf"))
bar(0.48)
download("Doom/images/heart", "/"..fs.combine(fs.combine(installpath, "images"), "heart"))
bar(0.51)
download("Doom/images/logo", "/"..fs.combine(fs.combine(installpath, "images"), "logo"))
bar(0.54)

update("Downloading levels...")
download("Doom/levels/level1", "/"..fs.combine(fs.combine(installpath, "levels"), "level1"))
bar(0.57)
download("Doom/levels/level2", "/"..fs.combine(fs.combine(installpath, "levels"), "level2"))
bar(0.60)
download("Doom/levels/level3", "/"..fs.combine(fs.combine(installpath, "levels"), "level3"))
bar(0.63)
download("Doom/levels/level4", "/"..fs.combine(fs.combine(installpath, "levels"), "level4"))
bar(0.66)
download("Doom/levels/level5", "/"..fs.combine(fs.combine(installpath, "levels"), "level5"))
bar(0.69)
download("Doom/levels/level6", "/"..fs.combine(fs.combine(installpath, "levels"), "level6"))
bar(0.72)
download("Doom/levels/level7", "/"..fs.combine(fs.combine(installpath, "levels"), "level7"))
bar(0.75)
download("Doom/levels/level8", "/"..fs.combine(fs.combine(installpath, "levels"), "level8"))
bar(0.78)
download("Doom/levels/level9", "/"..fs.combine(fs.combine(installpath, "levels"), "level9"))
bar(0.81)

update("Downloading models...")
download("Doom/models/corpse", "/"..fs.combine(fs.combine(installpath, "models"), "corpse"))
bar(0.84)
download("Doom/models/doorx", "/"..fs.combine(fs.combine(installpath, "models"), "doorx"))
bar(0.86)
download("Doom/models/doorz", "/"..fs.combine(fs.combine(installpath, "models"), "doorz"))
bar(0.88)
download("Doom/models/wallx", "/"..fs.combine(fs.combine(installpath, "models"), "wallx"))
bar(0.90)
download("Doom/models/wallz", "/"..fs.combine(fs.combine(installpath, "models"), "wallz"))
bar(0.92)
download("Doom/models/wallxz", "/"..fs.combine(fs.combine(installpath, "models"), "wallxz"))
bar(0.93)
download("Doom/models/emerald", "/"..fs.combine(fs.combine(installpath, "models"), "emerald"))
bar(0.95)
download("Doom/models/enemy1", "/"..fs.combine(fs.combine(installpath, "models"), "enemy1"))
bar(0.97)
download("Doom/models/enemy2", "/"..fs.combine(fs.combine(installpath, "models"), "enemy2"))
bar(0.99)

update("Finishing installation...")
fs.delete("/.std_list")
fs.delete("/.std_websites")

update("Installation finished!")
bar(1)
shell.setDir(oldpath)

sleep(1)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)

term.clear()
term.setCursorPos(1, 1)
write("Finished installation!\nPress any key to close...")
os.pullEventRaw()

term.clear()
term.setCursorPos(1, 1)