local tArgs = { ... }
local favorits = { }

--configrable variables
Firebox = shell.getRunningProgram()
edit = "edit" -- textviewing programe for http;
server = nil -- for redbook
install = "" -- for all downloads
dns = 202 -- id of DNS server
DNSids = {201}
DNSdomains = {"www.test.com"}
-- end

if #tArgs < 1 then
   print( "Do you want to 1 enter a url, 2:select from 'firebox_fave'" )
   input = read()
   shell.run( "clear" )
   if input == "2" then
   	if(fs.exists("firebox_fave")) then
   		local fave = fs.open("firebox_fave", "r")
           max = 0
           while true do
         local line = fave.readLine()
         if line == nil then --if reached end of file
  break end
           table.insert(favorits, line)
           max = max + 1
  end
fave.close()
                i = 0
           print( "Use the arrows and enter to chose.  " )
           while true do
           i = i + 1
           if favorits[i] == nil then break end
           print( "  " .. i .. ": " .. favorits[i])
           end
           pointer = 1
           term.setCursorPos(1, pointer + 1)
           print( "->" )
           while true do
                   local evt, arg1, arg2 = os.pullEvent()
                   term.setCursorPos(1, pointer + 1)
                   print( "  " )
                   if arg1 == 200 and pointer > 1 then
                           pointer = pointer - 1
                   elseif arg1 == 208 and pointer < max then
                           pointer = pointer + 1
                   end
                   term.setCursorPos(1, pointer + 1)
                   print( "->" )
                   if arg1 == 28 then break end
           end
           url = favorits[pointer]
          
   else
   print("'firebox_fave' dose not exist")
   print("To create a favorits list then store each url on its own line in firebox/fave")
   print("")
   print( "Enter a url or type host to host a server." )
   url = read()
   end
   else
           print( "Enter a url or type host to host a server." )
           url = read()
   end
else
  url = tArgs[1]
end
if string.find(url, "192.168.1.") then
-- Connect to rednet
rednetip = string.gsub(url, "192.168.1.", "") * 1
rednet.open("right")
shell.run( "clear" )
rednet.send(rednetip, "http/index")
file = io.open("temp", "w")
id, msg = rednet.receive(20)
if msg == nil then
        print( "The server did not sent a message back. It must not like you." )
else
        file:write(msg)
        file:close()
        response = nil
        shell.run( "temp" )
end
elseif url == "host" then
shell.run( "clear" )
print ( "Server ready to serve." )
rednet.open("right")
print ( "Opening the portholes" )
while true do
  id, message  = rednet.receive()
  if fs.exists(message) then
          file = io.open( message )
          filedata = file:read()
          file:close()
          rednet.send(id, filedata)
          print ( "Send that old trash to 192.168.1." .. id )
  elseif fs.exists("http/error") then
          file = io.open("http/error")
          filedata = file:read()
          file:close()
          rednet.send(id, filedata)
          print ( "Send that old trash to 192.168.1." .. id )
  else
          rednet.send(id, "print( \"Error, This server (192.168.1." .. id .. ") could not sent you your request.\")")
          print ( "Failed to give 192.168.1." .. id  .. " his present." )
  end
end
elseif string.find(url, "http://") then
--Connect to internet
       	        local response = http.get(url)
        	if response then
            	  while response do
            	   local sResponse = response.readAll()
                   response.close()
                   shell.run( "clear" )
                   cache = shell.resolve( "temp" )
                   local file = fs.open( cache, "w" )
                   file.write( sResponse )
                   file.close()
                   response = nil
                   shell.run( edit, "temp" )
                  end
                else
       		 -- if page is not reseved
        	   print( "Error, Could not load page." )
                end
elseif string.find(url, "rdnt://") then
--Connect to Rednet DNS
rednetDomain = string.gsub(url, "rdnt://", "")
rednet.open("right")
shell.run( "clear" )
	rednet.send(dns, rednetDomain)
	file = io.open("temp", "w")
	id, msg = rednet.receive(20)
	if msg == nil then
	        print( "The Domain Name Server did not sent a message back. It must have got it self in a fight with the website you are trying to connect to." )
	else
	        file:write(msg)
	        file:close()
	        response = nil
	        shell.run( "temp" )
	end
elseif string.find(url, "hostDNS") then
--Host a DNS server
	rednet.open("right")
	print ( "DNS ready to serve." )
	while true do
	client, message  = rednet.receive()
       	print( "192.168.1." .. client .. " whats to connect to " .. message )
	i = 1
	
	devidingCharater = string.find(message, "/")
	length  = string.len(message)
	message = string.lower(message)
	Domainname = string.sub(message, devidingCharater, length - devidingCharater)
	subDomain = string.sub(message, devidingCharater, length)
	
	print("request:" .. message)
	print("Domainname:" .. Domainname)
	
	print("subDomain:" .. subDomain)
	while DNSids[i] do
		if(message == DNSdomains[i]) then
			rednet.send(DNSids[i], "http/" .. subDomain)
		        print( "Trying to find 192.168.1." .. client .. " to 192.168.1." .. DNSids[i])
		        id, msg = rednet.receive(20)
		        if msg == nil then
		                print( "All as been lost." )
		        else
		                rednet.send(client, msg)  
		                print( "Found him. Matching up." )
        		end
		end
	i = i + 1
	end
	end

else
-- Connect to Craftnet
         --  root = "http://localhost/minecraft/"
           root = "http://www.lozengia.com/tmas/Downloads/Minecraft/firebox/"
        local response = http.get(root .. url)
        if response then
        -- if page is reseved
           while response do
                   local sResponse = response.readAll()
                   response.close()
                   shell.run( "clear" )
                   cache = shell.resolve( "temp" )
                   local file = fs.open( cache, "w" )
                   file.write( sResponse )
                   file.close()
                   response = nil
                   shell.run( "temp" )
           end
           shell.run( "clear" )
        else
        -- if page is not reseved
           print( "Error, Could not load page." )
        end
end