--[[ Add back-port initialization before startup ]]--
if (fs.exists("/startup")) then
  local oldStart = fs.open("/startup",'r')
end
local newStart = fs.open("/startup",'w')
local init = http.get("https://raw.githubusercontent.com/Selim042/CC-Backports/master/init")
newStart.write(init.readAll())
if (oldStart ~= nil) then
  newStart.write(oldStart.readAll())
  oldStart.close()
end
newStart.close()
init.close()

--[[ Get updated version of programs that will use auto-completion and settings ]]--
local progs = {
  ['lua'] = "https://raw.githubusercontent.com/alekso56/ComputercraftLua/master/rom/programs/lua",
  ['edit'] = "https://raw.githubusercontent.com/alekso56/ComputercraftLua/master/rom/programs/edit",
  ['shell'] = "https://raw.githubusercontent.com/alekso56/ComputercraftLua/master/rom/programs/shell",
  ['list'] = "https://raw.githubusercontent.com/alekso56/ComputercraftLua/master/rom/programs/list",
  ['set'] = "https://raw.githubusercontent.com/alekso56/ComputercraftLua/master/rom/programs/set",
}
if (not fs.exists("/progs")) then
  fs.makeDir("/progs")
end
for k,v in pairs(progs) do
  local file = fs.open("/progs/"..k,'w')
  local data = http.get(v)
  if (data == nil) then
    error("could not download "..k)
  else
    file.write(data.readAll())
    data.close()
  end
  file.close()
end

local settingsFile = fs.open("/.settings",'w')
settingsFile.write(textutils.serialise({
  [ "bios.use_multishell" ] = true,
  [ "shell.autocomplete" ] = true,
  [ "shell.allow_disk_startup" ] = not command,
  [ "shell.allow_startup" ] = true,
  [ "lua.autocomplete" ] = true,
  [ "list.show_hidden" ] = false,
  [ "edit.autocomplete" ] = true,
}))
settingsFile.close()

os.reboot()