lastVer = "5.5.5"
local os18 = true

if os.version() < "CraftOS 1.8" then
	os18 = false
end

if lastVer > version then
 noty = {
{["app"]="Settings", ["text"]="You have an update", ["button"]=" Update ", ['ico']=string.char(24)}
}
end

local ico = false
local file = fs.list("System/accounts")
if #file == 0 then
 l_login = nil
end
os.loadAPI("/System/API/image")
local test = fs.list('/.ds')
local find = false
for i=1,#test do
	find = false
	if test[i] ~= 'sys' then
		for ii=1,#file do
			if test[i] == file[ii] then
				find = true
				break
			end
		end
		if find == false then
			shell.run('rm /.ds/'..test[i])
		end
	end
end

local run = true
local xSize, ySize = term.getSize()
local xCenter = math.floor(xSize/2)
local yCenter = math.floor(ySize/2)
local intro = "Hello!"
local p_password = ""
local pword = ""
local pull = false

local pastebin = function(paste,filename)
 local file = http.get("http://pastebin.com/raw.php?i="..paste)
 if file then
  file = file.readAll()
  h=fs.open(filename,"w")
  h.write(file)
  h.close()
 end
end


local clear = function(color)
 term.setBackgroundColor(color)
 term.clear()
 term.setCursorPos(1,1)
end

local text = function(string,xPos,yPos)
 term.setCursorPos(xPos,yPos)
 term.write(string)
end

local redraw = function()
 clear(colors.lightGray)
 term.setTextColor(colors.white)
 file = fs.list('/System/accounts')
 for i=1,#file do
	if file[i] == "$test" then
		for j=i,#file-1 do
			file[j] = file[j+1]
		end
		file[#file] = nil
		break
	end
 end
 if #file ~= 0 then
  if log == false then
   if #file == 1 then
	l_login = file[1]
    if os118 then
	    os.run(env,"/System/accounts/"..file[1])
    else
      shell.run("/System/accounts/"..file[1])
    end
    if autoLogin == 1 then
     p_password = password
     clear(colors.lightGray)
     log = true
     run = false
    end
   end
   intro = "Enter account"
   paintutils.drawLine(6,5,xSize-5,5,colors.white)
   term.setBackgroundColor(colors.white)
   term.setTextColor(colors.black)
   if l_login ~= nil then
    text(l_login,xCenter-#l_login/2+1,5)
   end
   term.setTextColor(colors.lightGray)
   text(string.char(31),xSize-5,5)
   if l_login~=nil and p_password~="" then
    term.setBackgroundColor(colors.lime)
    term.setTextColor(colors.white)
    text(" Log In ",xCenter-3,16)
   end
  else
   intro = "Verify password"
   term.setBackgroundColor(colors.lightGray)
   text(l_login,xCenter-#l_login/2+1,5)
   if p_password ~= "" then
    term.setBackgroundColor(colors.lime)
    term.setTextColor(colors.white)
    text(" Enter ",xCenter-3,16)
   end
   if p_password == password and autoPassEnter == 1 then
    clear(colors.lightGray)
    log = true
    run = false
   end
  end
 elseif #file == 0 and l_login ~= '...' then
  pastebin("6VWUw78T","/System/FirstStart")
  if os18 then
    os.run(env,"/System/FirstStart")
  else
    shell.run("/System/FirstStart")
  end
  shell.run("rm /System/FirstStart")
  if #file==0 then
   run=false
  end
 end
 for i=1,5 do
  paintutils.drawLine(1,6+i,xSize,6+i,colors.white)
 end
 if l_login~=nil then
  local icon = fs.list("/System")
  for i=1,#icon do
   if icon[i]==l_login..".png" then
    image.draw(xCenter-3,7,"/System/"..l_login..".png")
   end
  end
 end
 term.setBackgroundColor(colors.lightGray)
 term.setTextColor(colors.white)
 text("Password:",4,14)
 paintutils.drawLine(13,14,xSize-3,14,colors.white)
 term.setTextColor(colors.black)
 text(pword,6+(xCenter-#p_password/2),14)
 term.setTextColor(colors.gray)
 term.setBackgroundColor(colors.lightGray)
 text(intro,xCenter-#intro/2+1,3)
end

local pullEvent = function()
	local event, side, x, y = os.pullEventRaw()
	if event == "terminate" then
		windows.error('You can`t use "Ctrl+T"')
		redraw()
	else
		return event, side, x, y
	end
end

local running = function()
 while run do
  event, side, x, y = pullEvent()
  if event == "mouse_click" then
   if y>5 and y-5<=#file and pull==true then
    l_login = file[y-5]
    if os18 then
      os.run(env,"/System/accounts/"..file[y-5])
    else
      shell.run("/System/accounts/"..file[y-5])
    end
    pull = false
    redraw()
    if autoLogin == 1 then
     p_password = password
     clear(colors.lightGray)
     log = true
     run = false
    end
   elseif y==14 and #file~=0 then
    paintutils.drawLine(3,14,xSize-3,14,colors.white)
    pword = ""
    term.setCursorPos(3,14)
    term.setTextColor(colors.black)
    p_password = read(string.char(7))
    for i=1, #p_password do
     pword = pword..string.char(7)
    end
    redraw()
    if autoPassEnter == 1 and p_password == password then
     clear(colors.lightGray)
     log = true
     run = false
    end
   elseif y==16 and #file~=0 and p_password~="" then
    if p_password == password then
     clear(colors.lightGray)
     log = true
     run = false
    else
     term.setTextColor(colors.red)
     text("Password:",4,14)
     paintutils.drawLine(13,14,xSize-3,14,colors.red)
     term.setTextColor(colors.white)
     text("Wrong",6+(xCenter-3),14)
     sleep(1)
     redraw()
    end
   elseif x==xSize-5 and y==5 then
    if pull == true then
     pull = false
     redraw()
    elseif pull == false then
     file = fs.list("/System/accounts")
     for i=1,#file do
      paintutils.drawLine(6,5,xSize-5,5,colors.white)
      term.setTextColor(colors.lightGray)
      text(string.char(30),xSize-5,5)
      text("...",xCenter-3/2+1,5)
      paintutils.drawLine(6,5+i,xSize-5,5+i,colors.white)
      term.setTextColor(colors.black)
      text(file[i],xCenter-#file[i]/2+1,5+i)
     end
     pull = true
    end
   end
  end
 end
end

redraw()
running()