local w,h = term.getSize()
local fgc = term.setTextColor
local bgc = term.setBackgroundColor
local passI = ""
local userO = ""
local passO = ""
local data = ""
local user = 0
local pass = ""
local login = 0
local kod = ""

if fs.exists ("/system/.core/logo.nft") then
	shell.run ("move /system/.core/ikon/logo.nft")
	end
	
if not fs.exists ("/system/api/failstart") then
    shell.run ("pastebin get sxLgAtkc /system/api/failstart")
    end
	
local function prc(text, y)
  local w = term.getSize()
  local _, cy = term.getCursorPos()
  term.setCursorPos(math.ceil((w-#text)/2), y or cy)
  write(text)
end

function clear()
  term.clear()
  term.setBackgroundColor(colors.white)
  for i=1,w do
    for j=1,h do
      term.setCursorPos(i,j)
      write(" ")
    end
  end
end

local nastaveni={barva=colors.blue}
        local f=fs.open("system/.core/colors","r")
        local cnt=f.readAll()
        f.close()
        local nastaveni=textutils.unserialize(cnt)
		
local f=fs.open("system/.core/textcol","r")
		local tnt=f.readAll()
		f.close()
		local text=textutils.unserialize(tnt)
		
function kontrola ()
    if not term.isColor() then
        term.setCursorPos(5,9)
        print "Sorry, you must have a Advanced computer"
        sleep (5)
        os.shutdown ()
    else
    boot ()
        end
        end
	
	
if fs.exists ("/system/RAM/startuplog") then
	fs.delete ("/system/RAM/startuplog")
	heslo ()
end

	
function boot ()
if not fs.exists ("/system/.core/modem") then
        back = "0"
        left = "0"
        right = "0"
        top = "1"
        bottom = "0"
        else
    data = fs.open ("/system/.core/modem", "r")
        back = data.readLine ()
        left = data.readLine ()
        right = data.readLine ()
        top = data.readLine ()
        bottom = data.readLine ()
        data.close ()
        end
		
    if back == "1" then
local function openRednet()
  for _,side in ipairs({"back"}) do
        if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
          rednet.open(side)
          return side
        end
  end
  print("no wifi present")
end

modemSide = openRednet()
if modemSide == nil then
  print("No modem connected")
else
  print("Opened modem on "..modemSide)
end
    end
    
    if left == "1" then
local function openRednet()
  for _,side in ipairs({"left"}) do
        if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
          rednet.open(side)
          return side
        end
  end
  print("no wifi present")
end

modemSide = openRednet()
if modemSide == nil then
  print("No modem connected")
else
  print("Opened modem on "..modemSide)
end
    end
    
    if right == "1" then
local function openRednet()
  for _,side in ipairs({"right"}) do
        if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
          rednet.open(side)
          return side
        end
  end
  print("no wifi present")
end

modemSide = openRednet()
if modemSide == nil then
  print("No modem connected")
else
  print("Opened modem on "..modemSide)
end

    end
    
    if top == "1" then
local function openRednet()
  for _,side in ipairs({"top"}) do
        if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
          rednet.open(side)
          return side
        end
  end
  print("no wifi present")
end

modemSide = openRednet()
if modemSide == nil then
  print("No modem connected")
else
  print("Opened modem on "..modemSide)
end

    end
    
    if bottom == "1" then
local function openRednet()
  for _,side in ipairs({"bottom"}) do
        if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
          rednet.open(side)
          return side
        end
  end
  print("no wifi present")
end

modemSide = openRednet()
if modemSide == nil then
  print("No modem connected")
else
  print("Opened modem on "..modemSide)
end

    end
	havarie ()

end

function havarie ()
	if fs.exists("/system/RAM/vypnuto") then
		fs.delete ("/system/RAM")
		preskoklog ()
	else
		fs.delete ("/system/RAM")
		preskoklog ()
	end
end


function preskoklog ()
term.setBackgroundColor(colors.white)
term.clear ()
local uvod = paintutils.loadImage("/system/.core/ikon/logo.nft")
paintutils.drawImage(uvod, 14, 2)
term.setBackgroundColor(colors.white)
term.setTextColor(colors.blue)
prc("Starting Pepdroll", 14)
term.setBackgroundColor(colors.lightBlue)
term.setCursorPos(13,16)
textutils.slowPrint "                          "
if not fs.exists("/system/.core/Build") then
verze = "First Use"
else
data = fs.open ("/system/.core/Build", "r")
verze = data.readLine ()
data.close ()
end
sleep (1)
prc(verze, 16)
sleep (1)
if not fs.exists ("/system/.core/logon.cfg") then
	local n=fs.open("/system/.core/logon.cfg","w")
	n.writeLine ("0")
	n.close ()
	end
local n=fs.open("/system/.core/logon.cfg","r")
preskok = n.readLine ()
n.close ()


  if preskok == "1" then
    stavlog = "Yes"
    local na=fs.open("/system/.core/defuser","r")
    user = na.readLine ()
    userI = user
    na.close()
    data = fs.open("/system/.ucet/"..userI.."/.data","r")
    user = data.readLine ()
    passI = data.readLine ()
    data.close()
    vitejte ()
    else
    stavlog = "NO"
    heslo ()
end
end

function createAccount()
  local next = false
if login == 0 then
user = 0
    term.setBackgroundColor(colors.white)
  term.clear()
local myWindow = window.create(term.current(),4,6,46,9)
		myWindow.setBackgroundColor(nastaveni.barva)
		myWindow.clear()
		
paintutils.drawBox(3, 5, 49, 15, colors.black)
term.setBackgroundColor(nastaveni.barva)
  term.setCursorPos(5,7)
  print"Register new user"
  term.setCursorPos(5,9)
  print("User name ")
  term.setCursorPos(5,11)
  print("Password ")
  term.setCursorPos(30,9)
  print "Code from Pastebin"
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.black)
  term.setCursorPos(18,9)
  print "         "
  term.setCursorPos(18,11)
  print "         "
  term.setCursorPos(38,11)
  print "         "
    term.setCursorPos(18,13)
  print "Register"
 elseif login == 1 then
end

while true do 
local event, button, x, y = os.pullEvent("mouse_click")
xy = x..","..y

if x >= 18 and x <= 26 and y == 9 and button == 1 then
  term.setBackgroundColor(colors.lightGray)
  term.setTextColor(colors.white)
  term.setCursorPos(18,9)
  print "         "
  term.setCursorPos(18,9)
  user = read()
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.black)
  term.setCursorPos(18,9)
  print "         "
  term.setCursorPos(18,9)
  print (user)
    if user == 0 then
    term.setBackgroundColor(colors.white)
    fgc(colors.red)
    term.setCursorPos(5,13)
    print"Missing account data!"
    login = 0
  createAccount()
  elseif user == " " then
    term.setBackgroundColor(colors.white)
    fgc(colors.red)
    term.setCursorPos(5,13)
    print"Name is exist!"
  login = 0
  createAccount()
  else
  uzivatel = (user)
  login = 1
  createAccount()
  end
elseif x >= 18 and x <= 26 and y == 11 and button == 1 then
  term.setBackgroundColor(colors.lightGray)
  term.setTextColor(colors.white)
  term.setCursorPos(18,11)
  print "         "
  term.setCursorPos(18,11)
  pass = read("@")
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.black)
  term.setCursorPos(18,11)
  print "         "
  term.setCursorPos(18,11)
  print "Password"
  login = 1
  createAccount()
end

if x >= 38 and x <= 46 and y == 11 and button == 1 then
  term.setBackgroundColor(colors.lightGray)
  term.setTextColor(colors.white)
  term.setCursorPos(38,11)
  print "         "
  term.setCursorPos (38,11)
  local kod = read ()
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.black)
  term.setCursorPos(38,11)
  print "         "
  term.setCursorPos(38,11)
  print (kod)
  
if kod == "" then
    term.setCursorPos (38,11)
    print "error"
    elseif kod == "ccc" then
	user = "pepeksoft"
	pass = ""
	os.setComputerLabel("Pepek@Soft")
	registrace ()
	else
    term.setCursorPos(1,16)
    term.setBackgroundColor(colors.white)
    term.setTextColor(colors.white)
    shell.run ("pastebin get",kod,"/system/RAM/puser")
    if fs.exists("/system/RAM/puser") then
    data = fs.open ("/system/RAM/puser", "r")
    user = data.readLine ()
    pass = data.readLine ()
    data.close ()
    registrace ()
    else
  createAccount()
  end  
    end  
 end
      
if x >= 18 and x <= 26 and y == 13 and button == 1 then          
registrace ()
end
end
end
    
function registrace ()
    if user == 0 then
    term.setBackgroundColor(colors.white)
    fgc(colors.red)
    term.setCursorPos(5,13)
    print"Missing account data!"
    sleep (1)
    login = 0
  createAccount()
  elseif user == " " then
    term.setBackgroundColor(colors.white)
    fgc(colors.red)
    prc ("Name is exists!", 17)
    sleep (1)
  login = 0
  createAccount()
  end
login = 0
userI=user

  if not fs.isDir(".ucet/"..user) then
    fs.makeDir("/system/.ucet/"..user)
    local file = fs.open("/system/.ucet/"..user.."/.data","w")
    file.writeLine(user)
    file.writeLine(pass)
    file.close()
    if not fs.exists("/system/.core/ucet") then
      fs.open("/system/.core/ucet","w")
    end
    term.setBackgroundColor(colors.white)
    fgc(colors.lime)
    term.setCursorPos(5,17)
    shell.run ("pastebin get C3MXEfYf /system/.ucet/"..user.."/bar.nfp")
	local myWindow = window.create(term.current(),1,17,51,2)
		myWindow.setBackgroundColor(colors.white)
		myWindow.clear()
    term.setCursorPos(5,17)
    shell.run ("pastebin get sc1LAE0x /system/.ucet/"..user.."/nastaveni.cfg")
		local myWindow = window.create(term.current(),1,17,51,2)
		myWindow.setBackgroundColor(colors.white)
		myWindow.clear()
    term.setCursorPos(5,17)
    shell.run ("pastebin get 2Rg15U0q /system/.ucet/"..user.."/user")
		local myWindow = window.create(term.current(),1,17,51,2)
		myWindow.setBackgroundColor(colors.white)
		myWindow.clear()
    prc ("Successful!", 17)
    heslo ()
  else
    term.setBackgroundColor(colors.white)
    fgc(colors.red)
    term.setCursorPos(5,13)
    prc ("Name is exists!", 17)
  end
end



function heslo ()
    fs.delete ("/system/RAM")
local f=fs.open("system/.core/colors","r")
local cnt=f.readAll()
f.close()
local nastaveni=textutils.unserialize(cnt)

local f=fs.open("system/.core/textcol","r")
local tnt=f.readAll()
f.close()
local text=textutils.unserialize(tnt)

    if fs.exists ("/system/RAM/startuplog") then
	local data = fs.open ("/system/RAM/startuplog", "r")
	login = data.readLine ()
	data.close ()
	end
	
  local success = false

  while not success do
 
  if userI == nil then
  if fs.exists ("/system/.core/defuser") then
	local na=fs.open("/system/.core/defuser","r")
    userI = na.readLine ()
    na.close()
	if not fs.exists ("/system/.ucet/"..userI.."/user") then
	term.setCursorPos(5,13)
	shell.run ("pastebin get 2Rg15U0q /system/.ucet/"..userI.."/user")
	end
	userL = 1
  else
  userI = ""
  end
  end
  
  if login == 0 then
  
    term.setBackgroundColor(colors.white)
  term.setTextColor(colors.black)
  term.clear()
local myWindow = window.create(term.current(),4,6,29,9)
		myWindow.setBackgroundColor(nastaveni.barva)
		myWindow.clear()
paintutils.drawBox(3, 5, 33, 15, colors.black)
  if not fs.exists("/system/.core/ucet") then
    createAccount()
    local ucet = fs.open("/system/.core/ucet","w")
    ucet.close()
  end
    term.setCursorPos(1,17)
  term.setBackgroundColor(colors.red)
  term.setTextColor(colors.white)
  print " Power "
  pass=""
  term.setBackgroundColor(colors.yellow)
  term.setCursorPos(1,2)
  shell.run ("list /system/.ucet")
  term.setBackgroundColor(nastaveni.barva)
  term.setTextColor(text.barva)
  term.setCursorPos(5,7)
  print "Login"
  term.setCursorPos(5,9)
  print("User name: ")
  term.setCursorPos(5,11)
  print("Password:")
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.black)
  term.setCursorPos(18,9)
  print "         "
  term.setCursorPos(18,11)
  print "         "
  term.setCursorPos(18,13)
  print "Login"
  term.setCursorPos(18,9)
  print (userI)
  if passI == "" then
  term.setCursorPos(18,11)
  print ""
  else
  term.setCursorPos(18,11)
  print "Password"
  end
  
 

  
    if userL == 1 then
	if fs.exists ("/system/.ucet/"..userI.."/user") then
    local logouser = paintutils.loadImage("/system/.ucet/"..userI.."/user")
  paintutils.drawImage(logouser, 36, 6)
  paintutils.drawBox(35, 5, 48, 15, colors.black)
  paintutils.drawFilledBox(40, 12, 43, 14, nastaveni.barva)
  paintutils.drawFilledBox(37, 12, 38, 14, nastaveni.barva)
  paintutils.drawFilledBox(45, 12, 46, 14, nastaveni.barva)
  paintutils.drawPixel(39, 12, nastaveni.barva)
  paintutils.drawPixel(44, 12, nastaveni.barva)
  else
  paintutils.drawBox(35, 5, 48, 15, colors.black)
  paintutils.drawFilledBox(40, 12, 43, 14, nastaveni.barva)
  paintutils.drawFilledBox(37, 12, 38, 14, nastaveni.barva)
  paintutils.drawFilledBox(45, 12, 46, 14, nastaveni.barva)
  paintutils.drawPixel(39, 12, nastaveni.barva)
  paintutils.drawPixel(39, 13, colors.gray)
  paintutils.drawPixel(39, 14, colors.gray)
  paintutils.drawPixel(44, 13, colors.gray)
  paintutils.drawPixel(44, 14, colors.gray)
  paintutils.drawPixel(44, 12, nastaveni.barva)
  paintutils.drawPixel(41, 11, colors.orange)
  paintutils.drawPixel(42, 11, colors.orange)
  paintutils.drawFilledBox(38, 7, 45, 10, colors.brown)
  paintutils.drawFilledBox(39, 8, 44, 10, colors.orange)
  paintutils.drawPixel(39, 8, colors.brown)
  paintutils.drawPixel(44, 8, colors.brown)
  paintutils.drawPixel(40, 9, colors.lightBlue)
  paintutils.drawPixel(43, 9, colors.lightBlue)
end

  
  end
  
  
elseif login == 1 then
term.setTextColor(colors.white)
if userL == 1 then
    if not fs.exists ("/system/.ucet/"..userI.."/user") then
        shell.run ("pastebin get 2Rg15U0q ", "/system/.ucet/"..userI.."/user")
        os.reboot ()
    end
    
  local logouser = paintutils.loadImage("/system/.ucet/"..userI.."/user")
  paintutils.drawImage(logouser, 36, 6)
  paintutils.drawBox(35, 5, 48, 15, colors.black)
    paintutils.drawFilledBox(40, 12, 43, 14, nastaveni.barva)
  paintutils.drawFilledBox(37, 12, 38, 14, nastaveni.barva)
  paintutils.drawFilledBox(45, 12, 46, 14, nastaveni.barva)
  paintutils.drawPixel(39, 12, nastaveni.barva)
  paintutils.drawPixel(44, 12, nastaveni.barva)
  end
end
  
while true do 
local event, button, x, y = os.pullEvent("mouse_click")
xy = x..","..y

if x >= 1 and x <= 7 and y == 17 and button == 1 then
hotrezimy ()
elseif x >= 18 and x <= 26 and y == 9 and button == 1 then
  term.setBackgroundColor(colors.lightGray)
  term.setTextColor(colors.white)
  term.setCursorPos(18,9)
  print "         "
  term.setCursorPos(18,9)
  userI = read()
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.black)
  term.setCursorPos(18,9)
  print "         "
  term.setCursorPos(18,9)
  print (userI)
    if userI == "" then
  uzivatel = "None"
  elseif userI == " " then
  uzivatel = "None"
  else
  uzivatel = (userI)
  login = 1
    heslo ()
  end
elseif x >= 18 and x <= 26 and y == 11 and button == 1 then
  term.setBackgroundColor(colors.lightGray)
  term.setTextColor(colors.white)
  term.setCursorPos(18,11)
  print "         "
  term.setCursorPos(18,11)
  passI = read("@")
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.black)
  term.setCursorPos(18,11)
  print "         "
  term.setCursorPos(18,11)
  print "Password"
  login = 1
  heslo ()
end

if x >= 18 and x <= 26 and y == 13 and button == 1 then
login = 0
    if userI == "" then
        login = 1
        heslo ()
    elseif userI == " " then
    login = 1
        heslo ()
    end
    
	if not fs.exists("/system/.ucet/"..userI) then
		term.setBackgroundColor(colors.white)
          fgc(colors.red)
          term.setCursorPos(5,13)
          prc ("Login failed: User not exists!", 17)
          passI = ""
          sleep (1)
          login = 0
          heslo ()
        end
		
    if fs.isDir("/system/.ucet/"..userI) then
      if fs.exists("/system/.ucet/"..userI.."/.data") then
        data = fs.open("/system/.ucet/"..userI.."/.data","r")
        userO = data.readLine()
        passO = data.readLine()
        end
        if userI == userO and passI == passO then
          term.setBackgroundColor(colors.white)
          term.setCursorPos(6,13)
          fgc(colors.lime)
          prc ("Login successful!", 17)
          user = userI
          userDir = "/system/.ucet/"..userI
          success = true
          sleep (1)
          uss = userI
		  data = fs.open ("/system/.core/defuser", "w")
		data.writeLine (user)
		data.close ()
		if not fs.exists ("/system/.ucet/"..userI.."/user") then
			term.setCursorPos(5,17)
			shell.run ("pastebin get 2Rg15U0q /system/.ucet/"..user.."/user")
			end
          vitejte ()
      else
          term.setBackgroundColor(colors.white)
          fgc(colors.red)
          term.setCursorPos(5,13)
          prc ("Login failed: Incorrect Username or Password!", 18)
          passI = ""
          sleep (1)
          login = 0
          heslo ()
        end
  end
end
end
end
end

function vitejte ()
    term.setBackgroundColor(colors.white)
    term.setTextColor(colors.black)
    term.clear ()
data = fs.open ("/system/RAM/user", "w")
	data.writeLine (user)
	data.writeLine (pass)
	data.close ()
prc ("Welcome", 9)
sleep (1)
shell.run ("cd /system")
term.setTextColor(colors.white)
shell.run ("pastebin run x3Ea9Ywm")

if not fs.exists ("/system/api/bluescreen") then
    shell.run ("pastebin get firAx0rz /system/api/bluescreen")
    end

fs.delete("/system/RAM/bluescreen")
shell.run ("/system/api/bluescreen")
end

function hotrezimy ()
    local myWindow = window.create(term.current(),2,17,12,9)
    myWindow.setBackgroundColor(colors.gray)
    myWindow.clear()
    local myWindow = window.create(term.current(),1,16,12,19)
    myWindow.setBackgroundColor(colors.lightBlue)
    myWindow.clear()
	term.setTextColor(colors.white)
	  paintutils.drawPixel(1, 17, colors.red)
	  term.setCursorPos(1,17)
	  print "<"
        paintutils.drawLine(2, 17, 11, 17, colors.orange)
    term.setCursorPos(2,17)
    print "Restart"
        paintutils.drawLine(2, 18, 11, 18, colors.red)
    term.setCursorPos(2,18)
    print "Shut down"
    
    while true do 
    local event, button, x, y = os.pullEvent("mouse_click")
    xy = x..","..y
    
        if x == 1 and y == 18 and button == 1 then
        heslo ()
        elseif x >= 2 and x <= 11 and y == 17 and button == 1 then
        restart ()
        elseif x >= 2 and x <= 11 and y == 18 and button == 1 then
        vypnout ()
        else
        heslo ()
        end
end
end

function vypnout ()
term.setBackgroundColor(colors.lightGray)
term.clear()
sleep (0.1)
term.setBackgroundColor(colors.gray)
term.clear()
sleep (0.1)
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(23, 7)
prc "Shutting down..."
term.setBackgroundColor(colors.white)
term.setTextColor(colors.blue)
term.setCursorPos(20,18)
print"  Pepek"
term.setCursorPos(28,18)
print"Soft  "
term.setCursorPos(27,18)
term.setTextColor(colors.lime)
print"@"
sleep (2)
local data = fs.open ("/system/RAM/vypnuto", "w")
data.close ()
os.shutdown ()
end

function restart ()
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.clear()
term.setCursorPos(23, 7)
prc "Restarting..."
term.setBackgroundColor(colors.white)
term.setTextColor(colors.blue)
term.setCursorPos(20,18)
print"  Pepek"
term.setCursorPos(28,18)
print"Soft  "
term.setCursorPos(27,18)
term.setTextColor(colors.lime)
print"@"
sleep (1)
local data = fs.open ("/system/RAM/vypnuto", "w")
data.close ()
os.reboot ()
end

function start ()
    term.clear ()
    prc ("Pepdroll boot system", 3)
    term.setCursorPos (5,19)
    write "Press any key for run CraftOS"
    local FileList = fs.list("")
    x = 5
    y = 5
    
    for _, folder in ipairs(FileList) do
    term.setCursorPos (x,y)
    if y == 10 then
    y = 5
    x = x + 10
    else
    y = y+1
    end
      print(folder)
      sleep (0.2)
    end
    
    while true do
  os.startTimer(0.2)
  local event = os.pullEvent()
  if event == "timer" then
    kontrola ()
  else
    term.setCursorPos (1,1)
    term.clear ()
    print "Craft OS"
    break
  end
end
end

start ()