local tArgs = { ... }
server = "http://www.lozengia.com/tmas/Downloads/Minecraft/firebox/cctunes.com/"

-- UPLOAD MUSIC

-- SELECT SONG TO UPLOAD
if tArgs[1] == "upload" and #tArgs < 2 then
   shell.run("clear")
   print( "Use the arrows and enter to chose.  " )
   songlist = fs.list("Music")
   i = 0
   max = -1
   while true do
   	   i = i + 1
   	   max = max + 1
           if songlist[i] == nil then break end	
   end
           pointer = 1
           term.setCursorPos(1, 10)
	   term.clearLine(none)
           print( "->" .. pointer .. ": " .. songlist[pointer])
     		while true do
     			 
          	         list = 1
          	         while list < 9 do
          	         	term.setCursorPos(1, 10 - list)
          	         	term.clearLine(none)
          	         	if songlist[pointer - (list)] then
          	         		write( "  " .. pointer - (list) .. ": " .. songlist[pointer - (list)])  
          	         	end
          	         	list = list + 1
          	         end        	     
          	         term.setCursorPos(1, 10)
          	         term.clearLine(none)
          	         print( "->" .. pointer .. ": " .. songlist[pointer])
			 list = 1
			 while list < 10 do
          	         	term.setCursorPos(1, 10 + list)
          	         	term.clearLine(none)
          	         	if songlist[pointer + (list)] then
          	         		write( "  " .. pointer + (list) .. ": " .. songlist[pointer + (list)])  
          	         	end
          	         	list = list + 1
          	         end
			local evt, arg1, arg2 = os.pullEvent()
          	         if arg1 == 28 then break end
          	         if arg1 == 200 and pointer > 1 then
          	                 pointer = pointer - 1
          	         elseif arg1 == 208 and pointer < max then
          	                 pointer = pointer + 1
          	         end
          	         
    		end
   	   
selectedsong = songlist[pointer]
else
selectedsong = tArgs[2]
end 

-- UPLOAD THE SONG
if tArgs[1] == "upload" and selectedsong then
	musicexists = fs.exists("Music/" .. selectedsong)
	instermentexists = fs.exists("Instrument/" .. selectedsong)
	
	if musicexists then
		file = io.open( "Music/" .. selectedsong )
		fmusicdata = file:read()
		file:close()
		musicdata = textutils.unserialize(fmusicdata)
		if musicdata["delay"] and musicdata["lenght"] and musicdata["original_author"] and musicdata["author"] and musicdata["name"] then
			if musicdata["name"] == "Unknown" then
				songname = selectedsong
			else
				songname = musicdata["name"]
			end
			
			uploadque = "filename=" .. textutils.urlEncode(selectedsong) .. "&title=" .. textutils.urlEncode(musicdata["name"]) .. "&author=" .. textutils.urlEncode(musicdata["author"]) .. "&originalauthor=" .. textutils.urlEncode(musicdata["original_author"]) .. "&musicdata=" .. textutils.urlEncode(fmusicdata)
			if instermentexists then
				file = io.open( "Instrument/" .. selectedsong )
				imusicdata = ""
				while true do
					filedata = file:read()
					if filedata then
						imusicdata = imusicdata .. '\n' .. filedata		
					else break end
				end
				file:close()
				uploadque = uploadque .. "&instermentdata=" .. textutils.urlEncode(imusicdata)
			end
			
				local response = http.post(server .. "upload.php", uploadque)
			        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
			        else
			        -- if page is not reseved
			           print( "Error, Something went wrong with the HTTP request." )
			            cache = shell.resolve( "temp" )
				    local file = fs.open( cache, "w" )
				    file.write( uploadque )
			            file.close()
			           
        			end
		end
	end
	

else
-- Upload portion of code ends here
-- Download songs
	local response = http.get(server .. "list.php")
        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

	end
end

shell.run("clear")