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


term.setBackgroundColor(1)
term.setTextColor(256)
term.clear()

--Functions

--Clear
function Clear()
 term.clear()
 term.setCursorPos(1,1)
end

--Set Cursorposition
function CP(x,y)
 term.setCursorPos(x,y)
end

--Set Background Color
function BC(farbe)
 term.setBackgroundColor(farbe)
end

--Set Text Color
function TC(farbe)
 term.setTextColor(farbe)
end

--Download Function

function Download(CodePB, Pfad)
 local cacheBuster = ("%x"):format(math.random(0, 2 ^ 30))
 local datei = http.get("https://pastebin.com/raw/" .. textutils.urlEncode(CodePB) .. "?cb=" .. cacheBuster)
 datei = datei.readAll()
 
 local file = fs.open(Pfad, "w")
 file.write(datei)
 file.close()
end

--SimSoft-Balken setzten
function SimSoftBalken(titel, laenge)
 w, h = term.getSize()
 BC(128)
 TC(1)
 
 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
end

--OS Version und Sprache

function VerSpr()
 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()
 SS2V = fileData[3]
 Sprache = fileData[4]
end

--OS Update Function

function OSUpdateCheck()
 local cacheBuster = ("%x"):format(math.random(0, 2 ^ 30))
 local datei = http.get("https://pastebin.com/raw/5dPkiCxH".. "?cb=" .. cacheBuster)
 if not datei then
  function UpdateC()
   UpdateCV = true
  end
 else

  datei = datei.readAll()
 
  local file = fs.open("SimSoft/Data/UpdateCheck/S","w")
  file.writeLine(datei)
  file.close()
  
  file = fs.open("SimSoft/Data/UpdateCheck/S","r")
  local fileData = {}
  local line = file.readLine()
  repeat
  table.insert(fileData,line)
  line = file.readLine()
  until line == nil
  file.close()
 
  v = fileData[1]
  
  VerSpr()
 
  if v == SS2V then
   function UpdateC()
    UpdateCV = true
   end
  else
   function UpdateC()
    UpdateCV = false
   end
  end
 
  fs.delete("SimSoft/Data/UpdateCheck/S")
 end
end

OSUpdateCheck()

--Ende Functionen

--Rednet Check

if peripheral.getType("back") == "modem" then
 rednet.open("back")
end

if peripheral.getType("left") == "modem" then
 rednet.open("left")
end

if peripheral.getType("right") == "modem" then
 rednet.open("right")
end

if peripheral.getType("top") == "modem" then
 rednet.open("top")
end

if peripheral.getType("front") == "modem" then
 rednet.open("front")
end

if peripheral.getType("bottom") == "modem" then
 rednet.open("bottom")
end

BC(256)
Clear()
sleep(0.2)
BC(1)
Clear()
sleep(0.1)
BC(256)
Clear()

--Start System (Passwort System)

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]

if PassSystem == "true" then

SimSoftBalken("SimSoft - Login", 15)

BC(256)
TC(128)

CP(4,8)
write("Name -")
CP(4,10)
write("Password -")

TC(1)
CP(4,8)
write("Name -")
TC(32768)
CP(11,8)
namee = read()

TC(128)
CP(4,8)
write("Name -")
TC(1)
CP(4,10)
write("Password -")
TC(32768)
CP(15,10)
passe = read("#")
TC(128)
CP(4,10)
write("Password -")

 function PassSS(nr)
 
  file = fs.open("SimSoft/Data/User/User"..nr,"r")
  local fileData = {}
  local line = file.readLine()
  repeat
  table.insert(fileData,line)
  line = file.readLine()
  until line == nil
  file.close()
 
  name = fileData[1]
  pass = fileData[2]
  ATF = fileData[3]
  
 end

 PassSS(1)
 if namee == name then
  if passe == pass then
   function Rechte()
	Recht = "Admin"
	RechtH = 1
   end
    TC(1)
    CP(3,14)
    print("Welcome "..namee.."!")
	sleep(1)
   shell.run("SimSoft/System/Desktop")
  else
   TC(1)
   CP(3,14)
   print("Wrong name or password")
   sleep(1.5)
   os.reboot()
  end
 else
 
  PassSS(2)
  if namee == name then
   if ATF == "false" then
    TC(1)
    CP(3,14)
    print("This account is offline")
    sleep(1.5)
    os.reboot()
   elseif passe == pass then
    function Rechte()
	 Recht = "Guest"
	 RechtH = 2
	end
	TC(1)
    CP(3,14)
    print("Welcome "..namee.."!")
	sleep(1)
    shell.run("SimSoft/System/Desktop")
   else
    TC(1)
    CP(3,14)
    print("Wrong name or password")
    sleep(1.5)
    os.reboot()
   end
  else
 
   PassSS(3)
   if namee == name then
    if ATF == "false" then
    TC(1)
    CP(3,14)
    print("This account is offline")
    sleep(1.5)
    os.reboot()
   elseif passe == pass then
     function Rechte()
	  Recht = "Guest"
	  RechtH = 3
	 end
	 TC(1)
     CP(3,14)
     print("Welcome "..namee.."!")
	 sleep(1)
     shell.run("SimSoft/System/Desktop")
    else
     TC(1)
     CP(3,14)
     print("Wrong name or password")
     sleep(1.5)
     os.reboot()
    end
   else
    TC(1)
    CP(3,14)
    print("Wrong name or password")
    sleep(1.5)
    os.reboot()
   end
  end
 end
 
else
 function Rechte()
  Recht = "Admin"
  RechtH = 0
 end
 shell.run("SimSoft/System/Desktop")
end

--end