mainColor = colors.lightGray
mainTextCol = colors.gray
mainSwitchCol = colors.lightBlue

os.loadAPI("/System/API/image")
os.loadAPI("/System/API/windows")
os.loadAPI("/System/API/zip")
os.loadAPI("/System/API/sysbar")
os.loadAPI("/System/API/contexts")

osLogo = '| '..string.char(7)..' ^ '..string.char(7)..' |'
osName = 'VedroidOS'
osexit = false
home = false
wallp = false
local xSize, ySize = term.getSize()
local centerX = math.floor(xSize/2)
local centerY = math.floor(ySize/2)
local comp = 0
if xSize > 30 then comp = 1 end
appHistory = {}
version = "5.5.5"
noty = {}
wifi = "off"
log = false
local appH = {}
local aph = "0"
local run = true
local obj = {}
local screen = 1
local maxScreen = 1
local deskS = 1
local maxDeskS = 1
local topM = "0"
local background = colors.lightBlue
local xp = 3
local yp = 3 - comp
local wallpapers = "wallp.png"
local notyPos = 1
local set = false
if style == nil then style = colors.lightBlue end
if colored == nil then colored = false end
local files = {}
local Napp = 0
local appMenu = false
local desk = {}
local editMode = false
local sear = nil
local addMode = false
local adds = 0
local draw = true

function getColors()
	return mainColor, mainTextCol, mainSwitchCol
end

function os.run( _tEnv, _sPath, ... )
    local tArgs = { ... }
    local tEnv = _tEnv
    setmetatable( tEnv, { __index = _G } )
    local fnFile, err = loadfile( _sPath, tEnv )
    if fnFile then
        local ok, err = pcall( function()
            fnFile( table.unpack( tArgs ) )
        end )
        if not ok then
            if err and err ~= "" then
                printError( err )
            end
            return err
        end
        return nil
    end
    if err and err ~= "" then
        printError( err )
    end
    return err
end

local function refreshFiles()
	local sys = fs.list('/.ds/sys')
	local filis = {}
	for i=1,#sys do
		filis[i] = 'sys/'..sys[i]
	end
	if l_login ~= '...' and l_login~='$test' then
		local file = fs.list('/.ds/'..l_login)
		if file ~= nil then
			for i=#sys+1,#file+#sys do
				filis[i] = l_login..'/'..file[i-#sys]
			end
		end
	end
	return filis
end

local function saveDesk(table)
	local file = fs.open('/System/desks/'..l_login,"w")
	file.write(textutils.serialize(table))
	file.close()
end

local function refreshDesk()
	local function load(name)
		local file = fs.open(name,"r")
		local data = file.readAll()
		file.close()
		return textutils.unserialize(data)
	end
	desk = load('/System/desks/'..l_login)
	if editMode == false then
		maxDeskS = #desk
	end
end

local newObj = function(name,x1,y1,x2,y2)
 obj[name] = {}
 obj[name]["x1"] = x1
 obj[name]["y1"] = y1
 obj[name]["x2"] = x2
 obj[name]["y2"] = y2
end

local pastebin = function(paste,filename)
	local file = http.get("https://pastebin.com/raw/"..paste)
	if file then
		file = file.readAll()
		h=fs.open(filename,"w")
		h.write(file)
		h.close()
		return true
	else
		windows.error("Unable to connect to the Internet")
		return false
	end
end

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

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

local fadeX = function()
 clear(colors.gray)
 sleep(0.05)
 clear(colors.lightGray)
 sleep(0.05)
 clear(colors.lightBlue)
 sleep(0.05)
end

local drawBox = function(x1,y1,x2,y2,color1,color2,text)
	term.setTextColor(color2)
	term.setBackgroundColor(color1)
	if text == nil then
		text = " "
	end
	for i=x1, x2 do
		for ii=y1, y2 do
			usualText(text,i,ii)
		end
	end
end

local app = function(name,ico,x,y)
	image.draw(x,y,ico,6,4)
	newObj(name,x,y,x+5,y+3)
end 

local drawn = function(app,text,button,y)
	for i=0,3 do
		paintutils.drawLine(xSize-25,y+i,xSize,y+i,colors.lightGray)
	end
	if app == 'Settings' or app == 'Store@' then 
	 image.draw(xSize-25,y,"/.ds/sys/"..app..".apk/Resources/icon.png",6,4)
	else
	 image.draw(xSize-25,y,"/.ds/"..l_login..'/'..app..".apk/Resources/icon.png",6,4)
	end
	term.setBackgroundColor(colors.lightGray)
	term.setTextColor(colors.gray)
	usualText("x",xSize,y)
	usualText(button,xSize-#button+1, y+3)
	term.setTextColor(colors.white)
	usualText(text,xSize-26+8,y+1)
	newObj(app,xSize,y,xSize,y)
end

local getStyle = function()
 shell.run("style")
end

local shutAnimation = function()
	fadeX()
	clear(colors.white)
	term.setTextColor(colors.lime)
	usualText(osLogo,centerX-math.floor(#osLogo/2),centerY+1)
	sleep(0.3)
	usualText(osLogo,centerX-math.floor(#osLogo/2)-3,centerY+1)
	term.setTextColor(colors.lightGray)
	usualText('Goodbye',centerX-math.floor(#osLogo/2)+6,centerY+1)
	sleep(0.5)
end

local nativeReboot = os.reboot
os.reboot = function()
	shutAnimation()
	nativeReboot()
	while true do
        coroutine.yield()
    end
end

local nativeShutdown = os.shutdown
os.shutdown = function()
	shutAnimation()
	nativeShutdown()
    while true do
        coroutine.yield()
    end
end

local topMenu = function()
	local time = textutils.formatTime(os.time(),true)
	files = refreshFiles()
	for i=1,#files do
		newObj(files[i],-1,-1,-1,-1)
	end
	if comp == 0 then
		if noty ~= nil then
			for i=1,#noty do
				drawn(noty[i]["app"],noty[i]["text"],noty[i]["button"],4*i+notyPos)
			end
		end
		paintutils.drawLine(0,2,xSize,2,colors.gray)
		term.setTextColor(colors.lightGray)
		for i=1,4 do
			paintutils.drawLine(0,i,xSize,i,colors.gray)
			term.setTextColor(colors.white)
		end
		usualText(time,xSize-#time,1)
		usualText("Settings",2,3)
		term.setTextColor(colors.lightGray)
		usualText(string.char(31),11,3)
		usualText(l_login,xSize-#l_login,3)
		term.setTextColor(colors.white)
		paintutils.drawLine(0,ySize,xSize,ySize,style)
		usualText("^^",xSize/2,ySize)
		if #noty ~= 0 then
			usualText("Clear",xSize-4,ySize)
		end
		newObj("sett",2,3,9,3)
		if set==true then
			for I=4,11 do
				paintutils.drawLine(1,I,11,I,colors.white)
			end
			term.setTextColor(colors.gray)
			usualText(" "..string.char(30),10,3)
			usualText("Apps",2,5)
			usualText("Customize",2,6)
			usualText("Security",2,7)
			usualText("Storage",2,8)
			usualText("Update",2,9)
			usualText("About",2,10)
		end
	else
		if noty ~= nil then
			for i=1,#noty do
				drawn(noty[i]["app"],noty[i]["text"],noty[i]["button"],ySize-4*i-notyPos-1)
			end
		end
		term.setTextColor(colors.lightGray)
		for i=0,2 do
			paintutils.drawLine(xSize-25,ySize-i,xSize,ySize-i,colors.gray)
		end
		term.setTextColor(colors.white)
		usualText(time,xSize-#time-1,ySize)
		usualText("Settings",xSize-24,ySize-1)
		term.setTextColor(colors.lightGray)
		usualText(string.char(30),xSize-15,ySize-1)
		usualText(l_login,xSize-#l_login,ySize-1)
		newObj("sett",xSize-24,ySize-1,xSize-17,ySize-1)
		if set==true then
			for I=2,9 do
				paintutils.drawLine(xSize-25,ySize-I,xSize-15,ySize-I,colors.white)
			end
			term.setTextColor(colors.gray)
			usualText(" "..string.char(31),xSize-16,ySize-1)
			usualText("Apps",xSize-24,ySize-8)
			usualText("Customize",xSize-24,ySize-7)
			usualText("Security",xSize-24,ySize-6)
			usualText("Storage",xSize-24,ySize-5)
			usualText("Update",xSize-24,ySize-4)
			usualText("About",xSize-24,ySize-3)
		end
	end
end

local sysBar = function()
	while true do
		local noti = #noty
		if draw and topM == "0" then
			sysbar.draw(style,colors.white,noty,false,true)
		end
		if topM == "1" then
			term.setTextColor(colors.white)
			term.setBackgroundColor(colors.gray)
			local time = textutils.formatTime(os.time(),true)
			if comp == 0 then
				usualText(time,xSize-#time,1)
			else
				usualText(time,xSize-#time-1,ySize)
			end
		end
		sleep(1)
		if noti < #noty then
			local b = term.current()
			local n = window.create(term.current(),xSize-25,1+(ySize-6)*comp+comp,26,4)
			term.redirect(n)
			clear(colors.lightGray)
			if noty[#noty]["app"] == 'Settings' or noty[#noty]["app"] == 'Store@' then 
				image.draw(11,1,"/.ds/sys/"..noty[#noty]["app"]..".apk/Resources/icon.png",6,4)
			else
				image.draw(1,1,"/.ds/"..l_login..'/'..noty[#noty]["app"]..".apk/Resources/icon.png",6,4)
			end
			term.setBackgroundColor(colors.lightGray)
			term.setTextColor(colors.white)
			usualText(noty[#noty]["text"],8,2)
			newObj(noty[#noty]["app"],25,1,25,1)
			term.redirect(b)
			sleep(1)
			n.setVisible(false)
			n.redraw()
			n = nil
		end
	end
end

local appHis = function()
 clear(style)
 sysBar()
 if appHistory == {} then usualText("No recent apps",xCenter-7,yCenter) end
 usualText("App History",1,1)
 files = refreshFiles()
  for i=1,#files do
   newObj(files[i],-1,-1,-1,-1)
  end
 term.setTextColor(colors.black)
 term.setBackgroundColor(colors.white)
 for i=1,#appHistory do
  if appHistory[i] == appHistory[i+1] then
   for ii=i,#appHistory do
    appHistory[ii] = appHistory[ii+1]
   end
  end
 end
 for i=1,#appHistory do
  paintutils.drawLine(1,i*2+1,xSize,i*2+1,colors.white)
  usualText("x",xSize,i*2+1)
  usualText(appHistory[i],1,i*2+1)
  usualText("     ",#appHistory[i]-3,i*2+1)
  newObj(appHistory[i],1,i*2+1,#appHistory[i],i*2+1)
 end
end

local function redraw()
	if l_login ~= '...' and (l_login == nil or l_login == '') then
		pastebin("AH3k8DMa","ver")
		shell.run('ver')
	end
	if colored == 1 then
		mainColor = style
		mainSwitchCol = style
		mainTextCol = colors.white
	else
		mainColor = colors.lightGray
		mainSwitchCol = colors.lightBlue
		mainTextCol = colors.gray
	end
	set=false
	clear(style)
	wallp = false
	local file = fs.list("/System")
	for i=1,#file do
		if file[i] == wallpapers then
			wallp = true
		end
	end
		
	if wallp then
		image.draw(1,2-comp,"/System/"..wallpapers)
	end
	
	newObj("sett",-2,-2,-2,-2)
	
	if appMenu then
	
		paintutils.drawLine(3,2-comp,xSize-2,2-comp,colors.white)
 
		for i=1,maxScreen do
			term.setTextColor(colors.lightGray)
			usualText(string.char(7),xSize+i-maxScreen-4,2-comp)
			term.setTextColor(style)
			usualText(string.char(7),xSize+screen-maxScreen-4,2-comp) 
		end
		usualText(string.char(30),xSize-2,2-comp)
		
		drawBox(3,3-comp,xSize-2,math.floor(ySize/(6-comp))*6-comp,colors.white,style,string.char(127))
		if sear == nil then
			files = refreshFiles()
		end
		maxScreen = 1 + math.floor(#files/(math.floor(xSize/8)*math.floor((ySize)/(6-comp))))
		xp=3
		yp=4 - comp
		Napp = (screen-1)*(math.floor(xSize/8)*math.floor((ySize)/(6-comp)))+1
 
		for i=1,math.floor(ySize/(6-comp)) do
			for ii=1,math.floor(xSize/(8)) do
				if files[Napp] ~= nil then
					if fs.exists("/.ds/"..files[Napp].."/Resources/icon.png") then
						app(files[Napp],"/.ds/"..files[Napp].."/Resources/icon.png",xp,yp)
					else
						app(files[Napp],"/System/noico.png",xp,yp)
					end
					if addMode == true then
						usualText('+',xp,yp)
					end
					Napp = Napp + 1
					xp = xp + 8
				else
					break
				end
			end
			yp = yp + 5
			xp = 3
		end
	else
		addMode = false
		adds = 0
		sear = nil
		if  l_login~='$test' then
			refreshDesk()
			if desk[deskS] ~= nil then
				for i=1,#desk[deskS] do
					if desk[deskS][i] ~= nil then
						if fs.exists("/.ds/"..desk[deskS][i]['name'].."/Resources/icon.png") then
							app(desk[deskS][i]['name'],"/.ds/"..desk[deskS][i]['name'].."/Resources/icon.png",desk[deskS][i]['x'],desk[deskS][i]['y'])
						else
							if fs.exists("/.ds/"..desk[deskS][i]['name']) then
								app(desk[deskS][i]['name'],"/System/noico.png",desk[deskS][i]['x'],desk[deskS][i]['y'])
							else
								for ii=i,#desk[deskS]-1 do
									desk[deskS][ii]=desk[deskS][ii+1]
								end
								desk[deskS][#desk[deskS]] = nil
								saveDesk(desk)
								refreshDesk()
								redraw()
								break
							end
						end
					end
				end
			end
		end
		paintutils.drawLine(3,2-comp,xSize-2,2-comp,colors.white)
		term.setTextColor(colors.lightGray)
		usualText(string.char(31),xSize-2,2-comp)
		for i=1,maxDeskS do
			term.setTextColor(colors.lightGray)
			usualText(string.char(7),xSize+i-maxDeskS-4,2-comp)
			term.setTextColor(style)
			usualText(string.char(7),xSize+deskS-maxDeskS-4,2-comp) 
		end
	end
	term.setTextColor(colors.lightGray)
	term.setBackgroundColor(colors.white)
	if editMode == false then
		if sear then
			usualText(sear,4,2-comp)
		else
			usualText('Search',4,2-comp)
		end
	else
		usualText('Apply',4,2-comp)
		usualText('+',xSize-2,2-comp)
		term.setTextColor(colors.red)
		if maxDeskS > 0 then
			usualText('x',xSize-maxDeskS-5,2-comp)
		end
		if deskS > 1 then
			drawBox(1,3-comp,1,ySize-2,style,colors.white,string.char(127))
		end
		drawBox(xSize,3-comp,xSize,ySize-2,style,colors.white,string.char(127))
	end
end

function runApp(app)
	local error = shell.run(app)
	term.redirect(term.native())
	if error ~= nil then
		redraw()
		windows.error(error)
	end
end

os.pullEvent = function()
	local event, side, x, y = os.pullEventRaw()
	if event ~= "terminate" then
		return event, side, x, y
	end
end

local running = function()
 while run == true and osexit == false do
  if home == true then
   home = false
  end
  
  if l_login ~= '...' and (l_login == nil or l_login == '') then
	pastebin("AH3k8DMa","ver")
	shell.run('ver')
  end
  
  event, side, x, y = os.pullEvent()
  
  if event == "mouse_scroll" then
   if side == 1 then
    if topM=="0" then
     if appMenu == true then
      screen = screen+1
      if screen > maxScreen then
       screen = maxScreen
      else
       clear(colors.lightBlue)
       redraw()
      end
	 else
	  deskS = deskS+1
      if deskS > maxDeskS then
       deskS = maxDeskS
      else
       clear(colors.lightBlue)
       redraw()
      end
	 end
    elseif topM=="1" and #noty*4+4+notyPos-comp*2>ySize then
     notyPos=notyPos-1
     topMenu()
    end
   elseif side == -1 then
    if topM=="0" then
     if appMenu == true then
      screen = screen-1
      if screen < 1 then
       screen = 1
      else
       clear(colors.lightBlue)
       redraw()
      end
	 else
	  deskS = deskS-1
      if deskS < 1 then
       deskS = 1
      else
       clear(colors.lightBlue)
       redraw()
      end
	 end
    elseif topM=="1" and #noty*4+5>ySize and notyPos<1 then
     notyPos=notyPos+1
     topMenu()
    end
   end

  elseif event == "key" then 
   if side == 205 and topM=="0" then
    if appMenu == true then
     screen = screen+1
     if screen > maxScreen then
      screen = maxScreen
     else
      clear(colors.lightBlue)
      redraw()
     end
	else
	 deskS = deskS+1
     if deskS > maxDeskS then
      deskS = maxDeskS
     else
      clear(colors.lightBlue)
      redraw()
     end
	end
   elseif side == 203 and topM=="0" then
    if appMenu == true then
     screen = screen-1
     if screen < 1 then
      screen = 1
     else
      clear(colors.lightBlue)
      redraw()
     end
	else
	 deskS = deskS-1
     if deskS < 1 then
      deskS = 1
     else
      clear(colors.lightBlue)
      redraw()
     end
	end
   end

  elseif event == "mouse_click" then
   draw = false
   if sysbar.notifyCheck(x,y) and topM == "0" then
    topM = "1"
    topMenu()
   elseif (sysbar.back(x,y) or (x>=3 and x <=9 and y == 2-comp)) and editMode and topM == '0' then
	editMode = false
	redraw()
   elseif sysbar.back(x,y) and editMode == false and topM == '0' then
    if sear then
     sear = nil
	elseif addMode == true then
	 addMode = false
	elseif appMenu then
	 appMenu = false
	end
	redraw()
   elseif x>=xSize-3 and x<=xSize-2 and editMode and y == 2-comp then
    maxDeskS = maxDeskS + 1
	desk[maxDeskS] = {}
	deskS = maxDeskS
	saveDesk(desk)
	redraw()
   elseif x==xSize-maxDeskS-5 and editMode and y == 2-comp then
	if deskS < maxDeskS then
	 for i=deskS,maxDeskS do
	  desk[i]=desk[i+1]
	 end
	end
	desk[maxDeskS] = nil
	maxDeskS = maxDeskS - 1
	if deskS>maxDeskS then
	 deskS = deskS-1
	end
	saveDesk(desk)
	redraw()
   elseif sysbar.home(x,y) and topM == '0' then
	editMode = false
	deskS = 1
	appMenu = false
	sear = nil
	redraw()
   elseif editMode == true then
    local select = 0
	local bx = 0
	local by = 0
    refreshDesk()
	if #desk > 0 then
	 for i=1,#desk[deskS] do
	  if x>=obj[desk[deskS][i]['name']]["x1"] and x<=obj[desk[deskS][i]['name']]["x2"] and y>=obj[desk[deskS][i]['name']]["y1"] and y<=obj[desk[deskS][i]['name']]["y2"] then
	   select = i
	   bx = desk[deskS][select]['x']
	   by = desk[deskS][select]['y']
	  end
	 end
	end
	if select ~= 0 then
	 while true do
	  local eve, si, xt, yt = os.pullEvent()
	  if eve == "mouse_drag" then
	   if yt>=3 and yt<=ySize-6+comp then
	    desk[deskS][select]['x']=xt
	    desk[deskS][select]['y']=yt
		if xt>=xSize-1 then
	     deskS = deskS+1
         if deskS > maxDeskS then
		  maxDeskS = maxDeskS + 1
          deskS = maxDeskS
		  desk[maxDeskS] = {}
		  desk[deskS-1][select]['x']=4
		  desk[deskS][#desk[deskS]+1] = desk[deskS-1][select]
		  desk[deskS-1][select] = nil
		  saveDesk(desk)
		  redraw()
		  break
         else
		  desk[deskS-1][select]['x']=3
		  desk[deskS][#desk[deskS]+1] = desk[deskS-1][select]
		  desk[deskS-1][select] = nil
          clear(colors.lightBlue)
		  select = #desk[deskS]
		  saveDesk(desk)
		  redraw()
		  break
         end
		elseif xt<=2 then
		 deskS = deskS-1
         if deskS < 1 then
          deskS = 1
         else
		  desk[deskS+1][select]['x']=math.floor((xSize/8)*8-8)
		  desk[deskS][#desk[deskS]+1] = desk[deskS+1][select]
		  desk[deskS+1][select] = nil
          clear(colors.lightBlue)
		  select = #desk[deskS]
		  saveDesk(desk)
		  redraw()
		  break
         end
		end
		saveDesk(desk)
	    redraw()
	   end
	  elseif eve == "mouse_scroll" then
       if si == 1 then
	    deskS = deskS+1
        if deskS > maxDeskS then
         deskS = maxDeskS
        else
		 desk[deskS][#desk[deskS]+1] = desk[deskS-1][select]
		 saveDesk(desk)
		 select = #desk[deskS]
         clear(colors.lightBlue)
         redraw()
        end
       elseif si == -1 then
	    deskS = deskS-1
        if deskS < 1 then
         deskS = 1
        else
		 desk[deskS][#desk[deskS]+1] = desk[deskS+1][select]
		 saveDesk(desk)
		 select = #desk[deskS]
         clear(colors.lightBlue)
         redraw()
        end
	   end
	  elseif eve == "mouse_up" then
	   for i=1,math.floor(xSize/8) do
		if (xt-(i*8-5) > 0 and xt-(i*8-5) < 7) or (xt-(i*8-5) < 0 and xt-(i*8-5) > -7) then
		 desk[deskS][select]['x']=i*8-5
		end
	   end
	   for i=1,math.floor(ySize/6) do
		if (yt-(i*5-(6-comp*4)) > 0 and yt-(i*5-(6-comp*4)) < 5) or (yt-(i*5-(6-comp*4)) < 0 and yt-(i*5-(6-comp*4)) > -5) then
		 desk[deskS][select]['y']=i*5-(6-comp*4)
		end
	   end
	   for i=1,#desk[deskS] do
	    if desk[deskS][select]['x']==desk[deskS][i]['x'] and desk[deskS][select]['y']==desk[deskS][i]['y'] and i~=select then
		 desk[deskS][i]['x'] = bx
		 desk[deskS][i]['y'] = by
		end
	   end
	   saveDesk(desk)
	   redraw()
	   select = 0
	   break
	  end
	 end
	end
   elseif x==xSize and y==ySize and topM == '0' then
    term.setBackgroundColor(colors.gray)
    term.setTextColor(colors.lightGray)
    usualText("|",xSize,ySize)
    term.setTextColor(colors.white)
	local budka = ''
	if appMenu == true then
	 local a = "Add to desktop"
	 if addMode then a = "Apply" end
	 budka = contexts.main(x,y-1,a,"Shutdown","Reboot","Wallpapers")
	else
	 budka = contexts.main(x,y-1,"Edit desktop","Shutdown","Reboot","Wallpapers")
	end
    if budka == "Shutdown" then
	 os.shutdown()
	elseif budka == "Reboot" then
	 os.reboot()
	elseif budka == "Wallpapers" then
	 redraw()
	 files = refreshFiles()
	 local find = false
      for i=1,#files do if files[i]==l_login.."/ImageViewer.apk" then find = true end end
	 if find then
	  shell.run("/.ds/"..l_login.."/ImageViewer.apk/main")
	 else
	  local but = windows.select({"ImageViewer not found"},{"Install it?"},{"Yes",colors.lightBlue,colors.white},{"No",colors.gray,colors.white})
	  if but == 'Yes' then
	   shell.run("/.ds/sys/Store@.apk/main")
	  end
	  redraw()
	 end
     redraw() 
     topM="0"
	elseif budka == "Add to desktop" and l_login~='$test' then
	 addMode = true
	 redraw()
	elseif budka == "Apply" then
	 addMode = false
	 redraw()
	elseif budka == "Edit desktop" and l_login~='$test' then
	 editMode = true
	 redraw()
	elseif budka == nil then
	 redraw()
	end
   elseif x>=obj["sett"]["x1"] and x<=obj["sett"]["x2"] and y>=obj["sett"]["y1"] and y<=obj["sett"]["y2"] and topM == "1" then
    topM = "0"
    shell.run("/System/prog/settings")
    redraw()
   elseif x>=xSize-#l_login and ((y==3 and comp == 0 ) or (y==ySize-1 and comp == 1))and topM=="1" then
    log = false
    l_login = "..."
    shell.run("/ver")
    topM="0"
	files = refreshFiles()
    redraw()
   elseif (((x>=10 and x<=11 and y==3) and comp == 0) or ((x>=xSize-16 and x<=xSize-15 and y==ySize-1) and comp == 1)) and topM=="1" and set==false then
    set=true
    topMenu()
   elseif topM=="1" and set==true then
    if ((y==5 and comp == 0) or (y==ySize-8 and comp == 1)) and ((x<=11 and comp == 0) or (x<=xSize-15 and x>=xSize-25 and comp == 1)) then
		topM = '0'
		paintutils.drawLine(1,1,xSize,1,colors.lightGray)
		shell.run("/System/prog/applications")
		redraw()
    elseif ((y==6 and comp == 0) or (y==ySize-7 and comp == 1)) and ((x<=11 and comp == 0) or (x<=xSize-15 and x>=xSize-25 and comp == 1)) then
		topM = '0'
		paintutils.drawLine(1,1,xSize,1,colors.lightGray)
		shell.run("/System/prog/style")
		redraw()
    elseif ((y==7 and comp == 0) or (y==ySize-6 and comp == 1)) and ((x<=11 and comp == 0) or (x<=xSize-15 and x>=xSize-25 and comp == 1)) then
		topM = '0'
		paintutils.drawLine(1,1,xSize,1,colors.lightGray)
		shell.run("/System/prog/security")
		redraw()
    elseif ((y==8 and comp == 0) or (y==ySize-5 and comp == 1)) and ((x<=11 and comp == 0) or (x<=xSize-15 and x>=xSize-25 and comp == 1)) then
		topM = '0'
		paintutils.drawLine(1,1,xSize,1,colors.lightGray)
		shell.run("/System/prog/storage")
		redraw()
    elseif ((y==9 and comp == 0) or (y==ySize-4 and comp == 1)) and ((x<=11 and comp == 0) or (x<=xSize-15 and x>=xSize-25 and comp == 1)) then
		topM = '0'
		paintutils.drawLine(1,1,xSize,1,colors.lightGray)
		shell.run("/System/prog/update")
		redraw()
    elseif ((y==10 and comp == 0) or (y==ySize-3 and comp == 1)) and ((x<=11 and comp == 0) or (x<=xSize-15 and x>=xSize-25 and comp == 1)) then
		topM = '0'
		paintutils.drawLine(1,1,xSize,1,colors.lightGray)
		shell.run("/System/prog/about")
		redraw()
    else
		redraw()
		topM="1"
		topMenu()
    end
    if ((y>=5 or y<=10) and comp == 0) or ((y>=ySize-2 or y<=ySize-10) and comp == 1) and x<=11 then
     topM="0"
     redraw()
    end
   elseif x>=xSize-4 and x<=xSize and y==ySize and topM=="1" then
    noty = {}
    redraw()
    topM="0"
   elseif x>=1 and x<=xSize and y==ySize and topM=="1" then
    redraw()
    topM = "0"
    notyPos=1
   elseif x>=3 and x<=xSize-4 and y==2-comp and topM=="0" then
    if appMenu then
		screen = 1
		sear = nil
		sear = sysbar.search("*",false,true)
		if sear then
			local file = refreshFiles()
			files = {}
			local ii=1
			for i=1,#file do
				if (string.find(file[i],sear) ~= nil) and (string.find(file[i],sear) < #file[i]-3) then
					files[ii]=file[i]
					ii=ii+1
				end
			end
		end
	else
		local op = sysbar.search("*",true)
		if op~=nil and fs.isDir(op)==false then
			shell.run(op)
		end
	end
	redraw()
   elseif x==xSize-2 and y==2-comp then
    if appMenu == true then
	 appMenu = false
	 redraw()
	else
	 appMenu = true
	 redraw()
	end
   else
    if side == 1 then
	 if appMenu then
      for i=(screen-1)*(math.floor(xSize/8)*math.floor((ySize)/(6-comp)))+1,Napp-1 do
       if x>=obj[files[i]]["x1"] and x<=obj[files[i]]["x2"] and y>=obj[files[i]]["y1"] and y<=obj[files[i]]["y2"] then
        if appHistory ~= nil then
         appHistory[#appHistory+1]=files[i]
        else
         appHistory[1]=files[i]
        end
        for ii=1,#files do
         newObj(files[ii],-1,-1,-1,-1)
        end
		if addMode == true then
		 if #desk < 1 then
		  desk[1] = {}
		  deskS = 1
		 end
		 desk[deskS][#desk[deskS]+1] = {}
		 desk[deskS][#desk[deskS]]['name']=files[i]
		 desk[deskS][#desk[deskS]]['x']=3+adds
		 desk[deskS][#desk[deskS]]['y']=ySize-6
		 adds = adds + 2
		 saveDesk(desk)
		 if math.floor(adds/2) > math.floor(xSize/8)-1 then
		  adds = 0
		  appMenu = false
		  addMode = false
		 end
		 redraw()
		else
		 topM="0"
		 screen = 1
		 appMenu = false
		 clear(colors.black)
         runApp("/.ds/"..files[i].."/main")
		 break
		end
       end
      end
	  redraw()
	 elseif appMenu == false and #desk > 0 and l_login~='$test' then
	  refreshDesk()
	  for i=1,#desk[deskS] do
	   if x>=obj[desk[deskS][i]['name']]["x1"] and x<=obj[desk[deskS][i]['name']]["x2"] and y>=obj[desk[deskS][i]['name']]["y1"] and y<=obj[desk[deskS][i]['name']]["y2"] then
        clear(colors.black)
        if appHistory ~= nil then
         appHistory[#appHistory+1]=desk[deskS][i]['name']
        else
         appHistory[1]=desk[deskS][i]['name']
        end
        topM="0"
		clear(colors.black)
        runApp("/.ds/"..desk[deskS][i]['name'].."/main")
		break
       end
	  end
	  redraw()
     end
     for i=1,#noty do
      if x>=xSize-#noty[i]["button"]+1 and x<=xSize-1 and ((y==4*i+notyPos+3 and comp == 0) or (y==ySize-(4*i+notyPos)+2 and comp == 1)) and topM == "1" then
       clear(colors.black)
	   topM="0"
	   if noty[i]["app"] == 'Settings' or noty[i]["app"] == 'Store@' then
	    shell.run("/.ds/sys/"..noty[i]["app"]..".apk/main")
	   else
        shell.run("/.ds/"..l_login.."/"..noty[i]["app"]..".apk/main")
	   end
       if noty[i+1] ~= nil then
        for i=i,#noty do
         noty[i] = noty[i+1]
        end
       else
        noty[i] = nil
       end
       redraw()
       break
      elseif x==xSize and ((y==4*i+notyPos and comp == 0) or (y==ySize-(4*i+notyPos+1) and comp == 1)) and topM == "1" then
       if noty[i+1] ~= nil then
        for i=i,#noty do
         noty[i] = noty[i+1]
        end
       else
        noty[i] = nil
       end
       if notyPos<-3 then
        notyPos=notyPos+4
       else
        notyPos=1
        redraw()
       end
       topMenu()
       break
      end
     end
     for i=1,#appHistory do
      if x==xSize and y==i*2+1 and aph == "1" then
       if appHistory[i+1] ~= nil then
        for ii=i,#appHistory do
         appHistory[ii] = appHistory[ii+1]
        end
       else
        appHistory[i] = nil
       end
       appHis()
       break
      end
     end
    end
   end
   draw = true
  end
  sysbar.draw(style,colors.white,noty,false,true)
  if topM == "1" then
   topMenu()
  end
 end
end

local start = function()
 redraw()
 running()
end

fadeX()
clear(colors.white)
term.setTextColor(colors.lightGray)
usualText('Vedroid',centerX-4,centerY-1)
term.setTextColor(colors.lime)
usualText('OS',centerX+3,centerY-1)
usualText(osLogo,centerX-math.floor(#osLogo/2),centerY+6)
usualText(".",centerX,ySize-1)
local web = pastebin("AH3k8DMa","ver")
if not web then
	term.setBackgroundColor(colors.white)
end
sleep(0.1)
usualText("..",centerX,ySize-1)
sleep(0.1)
usualText("...",centerX-1,ySize-1)
sleep(0.1)
shell.run("ver")
if l_login == '...' then l_login = '$test' end
local f = fs.list('/.ds')
local find = false
if #f ~= nil then
	for i=1, #f do
		if f[i] ~= nil and f[i] == l_login then find = true end
	end
end
if find == false and l_login ~= '...' then
	shell.run('mkdir /.ds/'..l_login)
else
	shell.run("/System/accounts/"..l_login)
end
find = false
local f = fs.list('/System/desks')
for i=1, #f do
	if f[i] == l_login then find = true end
end
if find == false then
	shell.run('mkdir /System/desks')
	local desk = {}
	desk[1] = {}
	desk[1][1] = {}
	desk[1][1]['name']='sys/Store@.apk'
	desk[1][1]['x']=3
	desk[1][1]['y']=ySize-6
	local file = fs.open('/System/desks/'..l_login,"w")
	file.write(textutils.serialize(desk))
	file.close()
end
f = nil
find = nil

clear(colors.white)
local workPath = "/.ds/"..l_login
parallel.waitForAny(sysBar,start)
fadeX()
clear(colors.black)
