--Copyright Simlor (http://www.computercraft.info/forums2/index.php?/user/55655-simlor/) -  Alle Rechte an diesen Programm sind Simlor vorbehalten.


axit = 0

file = fs.open("SimSoft/Data/Config","r")
local fileData = {}
local line = file.readLine()
repeat
table.insert(fileData,line)
line = file.readLine()
until line == nil
file.close()
PassSystem = fileData[1]
Hintergrund = fileData[2]
versionOS = fileData[3]
Sprache = fileData[4]

--SappS Touch Function

function SappSL(xaa, xba, yaa, yba, numm)

 if x >= xaa and x <= xba and y >= yaa and y <= yba then
 
  file = fs.open("SimSoft/Daten/SappS/"..numm,"r") 
  local fileData = {}
  local line = file.readLine()
  repeat
  table.insert(fileData,line)
  line = file.readLine()
  until line == nil
  file.close()
 
  nameTEST = fileData[1]
  
  if nameTEST == "" then
   side = 1
  end
 
  if side == 2 then
   
   posNR = fileData[2]
   
   posNR = (posNR+1)
   posNR = (posNR-1)
   
   ver = fileData[5]
   verD = fileData[6]
   if ver == "n" then
   else
    
	namesappS = fileData[1]
	
	if posNR == 5 or posNR == 10 or posNR == 15 then
	 xaa = (xaa-22)
	end
	
    BC(128)
    TC(1)
    CP(xaa+6, yaa)
    print("               ")
	CP(xaa+6, yaa)
    print(" "..namesappS)
    BC(1)
	TC(256)
    CP(xaa+6, yaa+1)
    print("               ")
    CP(xaa+6, yaa+2)
    print(" Run           ")
    CP(xaa+6, yaa+3)
    print(" Delete        ")
    CP(xaa+6, yaa+4)
    print(" Run with Arg. ")
	CP(xaa+6, yaa+5)
    print("               ")
	
	while true do
	 event,side,xx,yy = os.pullEvent()
	 if event == "mouse_click" then
	  
	  if xx >= (xaa+6) and xx <= (xaa+20) and yy >= yaa and yy <= (yaa+5) then
	   if xx >= (xaa+7) and xx <= (xaa+9) and yy == (yaa+2) then
	    if ver == "sv" then
         ver = fileData[7]
         shell.run(ver)
         axit = 1
		 break
        elseif ver == "n" then
		 break
        else
         shell.run("SimSoft/SappS/"..ver)
         axit = 1
		 break
        end
		
	   elseif xx >= (xaa+7) and xx <= (xaa+13) and yy == (yaa+3) then
	    
		file = fs.open("SimSoft/Daten/SappS/A","r")
        local fileData = {}
        local line = file.readLine()
        repeat
        table.insert(fileData,line)
        line = file.readLine()
        until line == nil
        file.close()

        sc = fileData[1]
		
		sc = (sc+1)
        sc = (sc-1)
		
		fs.delete("SimSoft/Daten/SappS/"..numm)
		
		if verD == "" then
		else
		 fs.delete("SimSoft/SappS/"..verD)
        end
	
   		local file = fs.open("SimSoft/Daten/SappS/"..numm,"w")
    	file.close()
	
		local file = fs.open("SimSoft/Daten/SappS/"..numm,"a")
    	file.writeLine()
    	file.writeLine()
    	file.writeLine()
    	file.writeLine()
    	file.writeLine("n")
    	file.writeLine("false")
    	file.close()
	
		sc = (sc-1)
		local file = fs.open("SimSoft/Daten/SappS/A","w")
    	file.writeLine(sc)
    	file.close()
	
		--Schieben im Falls das x sc nicht letzte Nr war.
	
		sc = (sc+1)
	
		if numm == sc then
		 axit = 1
		 break
		else
	
		 file = fs.open("SimSoft/Daten/SappS/"..sc,"r") 
    	 local fileData = {}
    	 local line = file.readLine()
    	 repeat
    	 table.insert(fileData,line)
    	 line = file.readLine()
    	 until line == nil
    	 file.close()
     
		 iia = fileData[1]
		 iib = numm
		 iic = fileData[3]
		 iid = fileData[4]
		 iie = fileData[5]
		 iif = fileData[6]
	 
		 local file = fs.open("SimSoft/Daten/SappS/"..sc,"w")
    	 file.close()
	
		 local file = fs.open("SimSoft/Daten/SappS/"..sc,"a")
    	 file.writeLine()
    	 file.writeLine()
    	 file.writeLine()
    	 file.writeLine()
    	 file.writeLine("n")
    	 file.writeLine("false")
    	 file.close()
	 
	 
		 local file = fs.open("SimSoft/Daten/SappS/"..numm,"w")
    	 file.close()
	
		 local file = fs.open("SimSoft/Daten/SappS/"..numm,"a")
    	 file.writeLine(iia)
    	 file.writeLine(iib)
    	 file.writeLine(iic)
    	 file.writeLine(iid)
    	 file.writeLine(iie)
		 file.writeLine(iif)
    	 file.close()
		 
		 axit = 1
		 break
		end
	   elseif xx >= (xaa+7) and xx <= (xaa+19) and yy == (yaa+4) then
	    CP(xaa+7, yaa+4)
		print("              ")
		CP(xaa+7, yaa+4)
		TC(128)
		arg = read()
		TC(256)
		
		if ver == "sv" then
         ver = fileData[7]
         shell.run(ver.." "..arg)
         axit = 1
		 break
        elseif ver == "n" then
		 break
        else
         shell.run("SimSoft/SappS/"..ver.." "..arg)
         axit = 1
		 break
        end
	   end
	  else
	   axit = 1
	   break
	  end
	 end
    end
   end
   
  elseif side == 1 then
   ver = fileData[5]
   if ver == "sv" then
    ver = fileData[7]
    shell.run(ver)
    axit = 1
   elseif ver == "n" then
   else
    shell.run("SimSoft/SappS/"..ver)
    axit = 1
   end
  end
 end
end


--Sapps Function

function AppG(name,pos,farbe)

 --Pos

 if pos == 1.0 then
  x = 4
  y = 5
 elseif pos == 2.0 then
  x = 12
  y = 5
 elseif pos == 3.0 then
  x = 20
  y = 5
 elseif pos == 4.0 then
  x = 28
  y = 5
 elseif pos == 5.0 then
  x = 36
  y = 5
 elseif pos == 6.0 then
  x = 4
  y = 9
 elseif pos == 7.0 then
  x = 12
  y = 9
 elseif pos == 8.0 then
  x = 20
  y = 9
 elseif pos == 9.0 then
  x = 28
  y = 9
 elseif pos == 10.0 then
  x = 36
  y = 9
 elseif pos == 11.0 then
  x = 4
  y = 13
 elseif pos == 12.0 then
  x = 12
  y = 13
 elseif pos == 13.0 then
  x = 20
  y = 13
 elseif pos == 14.0 then
  x = 28
  y = 13
 elseif pos == 15.0 then
  x = 36
  y = 13
 end
 
 --Farbe

 TC(256)
 
 if farbe == 1.0 then
  BC(1)
 elseif farbe == 2.0 then
  BC(2048)
 elseif farbe == 3.0 then
  BC(2048)
 elseif farbe == 4.0 then
  BC(8)
 elseif farbe == 5.0 then
  BC(32)
 elseif farbe == 6.0 then
  BC(4)
 elseif farbe == 7.0 then
  BC(2048)
 elseif farbe == 8.0 then
  BC(16)
 elseif farbe == 9.0 then
  BC(1024)
 elseif farbe == 10.0 then
  BC(4096)
 elseif farbe == 11.0 then
  BC(2)
 elseif farbe == 12.0 then
  BC(1024)
 elseif farbe == 13.0 then
  BC(16384)
 elseif farbe == 14.0 then
  BC(8192)
 elseif farbe == 15.0 then
  BC(2)
 elseif farbe == 16.0 then
  BC(4)
 elseif farbe == 17.0 then
  BC(8)
 elseif farbe == 18.0 then
  BC(16)
 elseif farbe == 19.0 then
  BC(32)
 elseif farbe == 20.0 then
  BC(64)
 
 end
 
 --Pos
 
 CP(x,y) --4,6
 print(" ")
 x = (x+1)
 y = (y+1)
 CP(x,y) --5,7
 print(" ")
 y = (y-1)
 
 --Farbe
 
 if farbe == 1.0 then
  BC(32768)
 elseif farbe == 2.0 then
  BC(1)
 elseif farbe == 3.0 then
  BC(32768)
 elseif farbe == 4.0 then
  BC(1)
 elseif farbe == 5.0 then
  BC(32768)
 elseif farbe == 6.0 then
  BC(1)
 elseif farbe == 7.0 then
  BC(16384)
 elseif farbe == 8.0 then
  BC(32)
 elseif farbe == 9.0 then
  BC(32768)
 elseif farbe == 10.0 then
  BC(8192)
 elseif farbe == 11.0 then
  BC(32768)
 elseif farbe == 12.0 then
  BC(4)
 elseif farbe == 13.0 then
  BC(16)
 elseif farbe == 14.0 then
  BC(32)
 elseif farbe == 15.0 then
  BC(64)
 elseif farbe == 16.0 then
  BC(256)
 elseif farbe == 17.0 then
  BC(256)
 elseif farbe == 18.0 then
  BC(512)
 elseif farbe == 19.0 then
  BC(1024)
 elseif farbe == 20.0 then
  BC(8192)
 end
 
 --Pos
 
 CP(x,y) --5,6
 print(" ")
 x = (x-1)
 y = (y+1)
 CP(x,y) --4,7
 print(" ")
 x = (x-2)
 y = (y+1)
 CP(x,y) --2,8
 TC(128)
 BC(256)
 print(name)
end


--Hintergrund erstellung

BC(256)
Clear()

file = fs.open("SimSoft/Data/Config","r")
local fileData = {}
local line = file.readLine()
repeat
table.insert(fileData,line)
line = file.readLine()
until line == nil
file.close()

Hintergrund = fileData[2]

if Hintergrund == "true" then
 
 w, h = term.getSize()
 
 h = (h-17)
 
 _Background = paintutils.loadImage("SimSoft/Data/DesktopBG")
 paintutils.drawImage(_Background,1,h) --1,2

elseif Hintergrund == "false" then
end


--Interface und Icons

SimSoftBalken("SimSoft 4 - Desktop", 19)
 
CP(2,2)
TC(256)
print("=")
 
w, h = term.getSize()
nr = 3
w = (w-2)
 
while true do
 BC(8)
 nr = (nr+1)
 CP(w,nr)
 write("   ")
 if nr == h then
  w = (w+1)
  CP(w,5)
  TC(1)
  print("@")
  h = (h-1)
  CP(w,h)
  print("@")
  break
 end
end

file = fs.open("SimSoft/Daten/SappS/A","r")
local fileData = {}
local line = file.readLine()
repeat
table.insert(fileData,line)
line = file.readLine()
until line == nil
file.close()

sc = fileData[1]

sc = (sc+1)
sc = (sc-1)

if sc >= 16 then
 sc = 15
elseif sc <= -1 then
 sc = 0
end

if sc == 0 then
else

 anz = 0

 while true do

  anz = (anz+1)

  file = fs.open("SimSoft/Daten/SappS/"..anz,"r")
  local fileData = {}
  local line = file.readLine()
  repeat
  table.insert(fileData,line)
  line = file.readLine()
  until line == nil
  file.close()

  a = fileData[1]
  b = fileData[2]
  c = fileData[3]

  b = (b+1)
  b = (b-1)
  
  c = (c+1)
  c = (c-1)
  
  AppG(a,b,c)
 
  if anz == sc then
   break
  end
 end
end

--Touch

while true do

 event,side,x,y = os.pullEvent()
 if event == "mouse_click" then
  
  --SappS
  
  SappSL(2, 7, 5, 7, 1)
  SappSL(10, 15, 5, 7, 2)
  SappSL(18, 23, 5, 7, 3)
  SappSL(26, 31, 5, 7, 4)
  SappSL(34, 39, 5, 7, 5)
  SappSL(2, 7, 9, 11, 6)
  SappSL(10, 15, 9, 11, 7)
  SappSL(18, 23, 9, 11, 8)
  SappSL(26, 31, 9, 11, 9)
  SappSL(34, 39, 9, 11, 10)
  SappSL(2, 7, 13, 15, 11)
  SappSL(10, 15, 13, 15, 12)
  SappSL(18, 23, 13, 15, 13)
  SappSL(26, 31, 13, 15, 14)
  SappSL(34, 39, 13, 15, 15)
  
  if axit == 1 then
   break
  end
  
  --Balkenleisten
  
  w, h = term.getSize()
  
  if x >= 1 and x <= w and y >= 1 and y <= 3 then

   BC(256)
   Clear()

   w, h = term.getSize()
   BC(128)
   TC(1)
 
   laenge = 19
   titel = "SimSoft 4 - Desktop"
 
   anz = 0
   hig = 1
   while true do
    anz = (anz+1)
    term.setCursorPos(anz, hig)
    write(" ")
    if anz == w then
     if hig == 3 then
      laenge = (laenge/2)
      w = (w/2)
      w = (w-laenge)
	
      term.setCursorPos(w,2)
      print(titel)
      break
     else
      hig = (hig+1)
      anz = 0
     end
    end
   end
   
   CP(2,2)
   TC(256)
   print(">")
 
   w, h = term.getSize()
   nr = 3
   w = (w-2)
 
   w, h = term.getSize()
   nr = 3
   w = (w-2)
 
   while true do
    BC(8)
    nr = (nr+1)
    CP(w,nr)
    write("   ")
    if nr == h then
     w = (w+1)
     CP(w,5)
     TC(1)
     print("@")
     h = (h-1)
     CP(w,h)
     print("@")
     break
    end
   end
   
   BC(256)
   
   TC(1)
   CP(6,5)
   print("System")
   
   VerSpr()
   
   --Version
   TC(32768)
   CP(4,8)
   write("OS-Version - ")
   TC(128)
   print(SS2V)
   
   --ID
   CP(4,10)
   TC(32768)
   write("PC-ID - ")
   TC(128)
   print(os.getComputerID())
   
   --by
   CP(4,12)
   TC(32768)
   write("Copyright - ")
   TC(128)
   print("Simlor")
   
   --Hintergrund
   BC(256)
   CP(4,14)
   TC(1)
   print("Edit Background")
   
   --Acounts
   BC(256)
   CP(4,16)
   TC(1)
   print("Account Manager")
   
   while true do
   
    file = fs.open("SimSoft/Data/Config","r")
    local fileData = {}
    local line = file.readLine()
    repeat
    table.insert(fileData,line)
    line = file.readLine()
    until line == nil
    file.close()
    PassSystem = fileData[1]
    Hintergrund = fileData[2]
	versionOS = fileData[3]
	Sprache = fileData[4]
   
    if Hintergrund == "true" then
     CP(22,14)
	 BC(8)
	 write("  ")
	 BC(32768)
	 print(" ")
    else
     CP(22,14)
	 BC(32768)
	 write(" ")
	 BC(128)
	 print("  ")
    end
	
	BC(256)
   
    event,side,x,y = os.pullEvent()
	if event == "mouse_click" then
	
	 if x >= 4 and x <= 18 and y == 14 then
	  shell.run("paint SimSoft/Data/DesktopBG")
	  break
	 end
	 
	 if x >= 4 and x <= 18 and y == 16 then
	  shell.run("SimSoft/System/AcManager")
      break
	 end
	 
	 if x >= 22 and x <= 24 and y == 14 then
   
      local file = fs.open("SimSoft/Data/Config","w")
      file.close()
	  
	  if Hintergrund == "true" then
	   Hintergrund = "false"
	  elseif Hintergrund == "false" then
	   Hintergrund = "true"
	  end
   
      local file = fs.open("SimSoft/Data/Config","a") 
      file.writeLine(PassSystem)
      file.writeLine(Hintergrund)
      file.writeLine(SS2V)
	  file.writeLine(Sprache)
      file.close()
	 end
	 
	 w, h = term.getSize()
	 
	 if x >= 1 and x <= w and y >= 1 and y <= 3 then
	  break
	 end
	 
	 wxb = (w-2)
	 
	 if x >= wxb and x <= w and y >= 4 and y <= h then
	  break
	 end
    end
   end
  break
  end 
  
  w, h = term.getSize()
  
  wxa = (w-2)
  
  if x >= wxa and x <= w and y >= 4 and y <= h then
   
   w, h = term.getSize()
   nr = 3
   wa = (w-14)
 
   while true do
    BC(8)
    nr = (nr+1)
    CP(wa,nr)
    write("               ") --3
    if nr == h then
     wb = (w-13)
     CP(wb,5)
     TC(1)
     print("@")
     h = (h-1)
     CP(wb,h)
     print("@")
     break
    end
   end
   
   ha = h
   wc = (w-11)
   CP(wc,ha) --40,18 --51
   TC(1)
   print("by Simlor")
   
   wc = (w-11)
   CP(wc,5)
   TC(32768)
   print("SimSoft 4")
   
   wc = (w-14)
   CP(wc,7)
   TC(8)
   BC(1)
   print("   Restart     ")
   
   wc = (w-14)
   CP(wc,8)
   TC(1)
   BC(8)
   print("   Shutdown    ")
   
   wc = (w-14)
   CP(wc,9)
   TC(8)
   BC(1)
   print("   Shell       ")
   
   wc = (w-14)
   CP(wc,10)
   TC(1)
   BC(8)
   print("   SappS Store ")
   
   UpdateC()
   
   wc = (w-14)
   
   if UpdateCV == true then
    CP(wc,11)
    TC(8)
    BC(1)
    print("   OS-Updates  ")
   else
    CP(wc,11)
    TC(16384)
    BC(1)
    print("   OS-Updates  ")
   end
   
   while true do
    event,side,x,y = os.pullEvent()
    if event == "mouse_click" then
     
	 w, h = term.getSize()
	 
	 wa = (w-14)
	 
     if x >= wa and x <= w and y == 7 then
      os.reboot()
      break
	 end
	  
     if x >= wa and x <= w and y == 8 then
      os.shutdown()
      break
	 end
      
     if x >= wa and x <= w and y == 9 then
      term.setBackgroundColor(32768)

      term.clear()
      term.setCursorPos(1,1)
      term.setTextColor(1)
      print("Back to SimSoft? 'exit'")
      shell.run("rom/programs/shell")
      break
	 end
     
     if x >= wa and x <= w and y == 10 then
      shell.run("SimSoft/System/SappS")
      break
	 end
	 
	 if x >= wa and x <= w and y == 11 then
      shell.run("SimSoft/System/SystemUpdate")
      break
	 end
	  
     --Seitenleiste aus
     
	 wb = (w-15)
	 
     if x >= 1 and x <= wb and y >= 4 and y <= h then
      break   	  
     end
	 if x >= 1 and x <= w and y >= 1 and y <= 3 then
      break   	  
     end
	end
   end
   break
  end
 end
end

shell.run("SimSoft/System/Desktop")