term.clear()
term.setCursorPos(1,1)

boucle = true
start = false
slc = 0

os.loadAPI("GPnet/net")

function Updater()
fs.delete("srv/fond")
shell.run("pastebin","get","S4FK8pCr","srv/fond")
end

function Fond()
fd1 = paintutils.loadImage("srv/fond")
paintutils.drawImage(fd1,1,1)
term.setBackgroundColor(colors.blue)
term.setTextColor(colors.white)
term.setCursorPos(15,1)
print("G-Power Telecom Serveur")
term.setBackgroundColor(colors.red)
term.setCursorPos(50,1)
print("X")
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.setCursorPos(2,3)
if start then print("> Eteindre le site internet")
else print("> Demarrer le site internet")
end
term.setCursorPos(2,4)
print("> Importer une page d'accueil")
term.setCursorPos(3,5)
write("=> ")
file = fs.open("srv/default","r")
website = file.readAll()
if not fs.exists(website) or website == "" then
  print("aucun")
  website = "srv/defaultpage"
else print(website)
end
file.close()
term.setCursorPos(2,18)
write("Etat : ")
if start then term.setBackgroundColor(colors.lime)
else term.setBackgroundColor(colors.red)
end
print("  ")
term.setBackgroundColor(colors.white)
end

function limitRead(nLength, cReplaceChar)
term.setCursorBlink(true)
nLegth = nLength or -1
sReturnString = ""
xPos, yPos = term.getCursorPos()

while true do
event, char = os.pullEvent()
if nLength ~= -1 and string.len(sReturnString) >= nLength then term.setCursorBlink(false); return sReturnString end
if event == "char" then sReturnString = sReturnString .. char
elseif event == "key" and char == 28 then term.setCursorBlink(false); return sReturnString
elseif event == "key" and char == 14 then
  term.setCursorPos(xPos,yPos)
  term.write(string.rep(" ", string.len(sReturnString)))
  sReturnString = string.sub(sReturnString, 1, string.len(sReturnString)-1)
  term.setCursorPos(xPos,yPos)
  if not cReplaceChar then term.write(sReturnString)
  else term.write(string.rep(cReplaceChar, string.len(sReturnString))) end
end
term.setCursorPos(xPos,yPos)
term.write(string.rep(" ", string.len(sReturnString)))
term.setCursorPos(xPos,yPos)
if not cReplaceChar then term.write(sReturnString)
else term.write(string.rep(cReplaceChar, string.len(sReturnString))) end
end
end

Updater()
Fond()

while boucle do
local event, p1, p2, p3, p4 = os.pullEvent()
if event == "mouse_click" then
  button = p1
  X = p2
  Y = p3
  if button == 1 then
    if slc == 0 then
      if X >= 2 and X <= 28 and Y == 3 then
        if start then
          start = false
          net.disconnect()
        else
          start = true
          net.connectModem(2)
        end
        Fond()
      elseif X >= 2 and X <= 29 and Y == 4 then
        term.setCursorPos(3,5)
        term.clearLine()
        write("=> ")
        website = limitRead(44)
        file = fs.open("srv/default","w")
        file.write(website)
        file.close()
        Fond()
      elseif X >= 49 and X <= 51 and Y == 1 then
        boucle = false
        term.setBackgroundColor(colors.black)
        term.setTextColor(colors.white)
        term.clear()
        term.setCursorPos(1,1)
        error()
      end
    end
  end
elseif event == "modem_message" then
  side = p1
  freq = p2
  origine = p3
  requete = p4
  if start then
    if freq == 2 then
      uRequete = textutils.unserialise(requete)
      if uRequete.arg == "initConnect" then
        file = fs.open(website,"r")
        inputArg = file.readAll()
        net.transmitData(tostring(origine),inputArg,2)
        file.close()
      elseif string.find(uRequete.arg,"include") then
        tableau = {}
        
        for sub_string in string.gmatch(uRequete.arg,'([^->]+)') do
        table.insert(tableau,sub_string)
        end
		      
        if fs.exists(tableau[2]) then
          file = fs.open(tableau[2],"r")
          inputArg = file.readAll()
          net.transmitData(tostring(origine),inputArg,2)
        end
      end
    end
  end
end
end
