-- CraftOS+ Installer
local error = 0

fs.makeDir("code")
fs.makeDir("programs")
version = "recommended"
if term.isColor() then
	color = true
else
	color = false
end
rootFolder = "https://raw.github.com/Sirharry0077/ComputerCraft/master/"..version.."/"
		
function download(url, path)
 http.request(url)
 local requesting = true
 while requesting do
  local event, url, sourceText = os.pullEvent()
  if event == "http_success" then
   local respondedText = sourceText.readAll()
   requesting = false
   getFile = http.get(url)
   text = getFile.readAll()
   getFile.close()
   file = fs.open(path, "w")
   file.write(text)
   file.close()
   success = true
  elseif event == "http_failure" then
   requesting = false
   success = false
  end
 end
end

local function openRednet()
local listOfSides = rs.getSides()
local listofPossibles = {}
local counter1 = 0
while true do
  counter1 = counter1 +1

  if peripheral.isPresent(tostring(listOfSides[counter1])) and peripheral.getType(listOfSides[counter1]) == "modem" then
   table.insert(listofPossibles,tostring(listOfSides[counter1]))
  end

  if counter1 == 6 and table.maxn(listofPossibles) == 0 then
   print("no wifi present")
   return nil
  end

  if counter1 == 6 and table.maxn(listofPossibles) ~= 0 then
   rednet.open(listofPossibles[1])
   return listofPossibles[1]
  end
end
end
modemOn = openRednet()

openRednet()
shell.run("clear")
if color == true then term.setTextColor(colors.yellow) end
print("CraftOS+ Updater/Installer")
print("--------------------------")
if color == true then term.setTextColor(colors.red) end
if version == "latest" then
 print("Latest Version Download Active")
end
print("Press ENTER To Proceed")
read()
x = 1
error = 0
errors = "PLACEHOLDER"
repeat
 if x == 1 then
  program = "Startup"
  location = "startup"
 elseif x == 2 then
  program = "Server"
  location = "server"
 elseif x == 3 then
  program = "Firewolf"
  location = "firewolf"
 elseif x == 4 then
  program = "Help"
  location = "help"
 elseif x == 5 then
  program = "Download"
  location = "code/download"
 elseif x == 6 then
  program = "Stop"
  location = "code/stop"
 elseif x == 7 then
  program = "Test Connection"
  location = "code/testconnection"
 elseif x == 8 then
  program = "Upload"
  location = "code/upload"
 elseif x == 9 then
  program = "Auth"
  location = "programs/auth"
 elseif x == 10 then
  program = "Changelog"
  location = "programs/changelog"
 elseif x == 11 then
  program = "Help1"
  location = "programs/help"
 elseif x == 12 then
  program = "Help2"
  location = "programs/help2"
 elseif x == 13 then
  program = "Server Install"
  location = "programs/serverinstall"
 elseif x == 14 then
  program = "Version"
  location = "programs/version"
 end

 url1 = rootFolder..location
 
 download(url1, location)

 x = x + 1
until x == 15

if error == 0 or error > 1 then
 errors = " Errors."
elseif x == 1 then
 erros = " Error."
end

print("Installation Complete With ", error, errors)
print("If There Was Any Errors, Please Restart The Installer.")
print("Press ENTER To Continue.")
read()