#!/bin/flua
--A bittorrent wrapper for DSL, by J.P. Nimmo, aka SaidinUnleashed
--1/23/05

function data_check()
	
   if url.value == "" then return nil end
   if uploadrate.value == "" then return nil end
   return ""
   
end

function get_target()
	t = Window{320,150,"Save to folder..."}
	q = Box{10,10,300,105; box=Frametype.engraved, label=
	[[Enter the full path to the folder you
	want to dave the downloaded files.
	Ex. = /home/dsl/downloads]]}
	target = Input{70,85,230,25,"Target: "}
	cancel2 = Button{40,120,80,25,"Cancel"}
	
	start = Button{200,120,80,25,"Start"}
	
	t:hotspot(cancel2)
	End{}
	t:set_modal()
	t:show()
	
	while 1 do
		Fl:wait()
		
		function start:callback()
			if cursesbtn.value == 1 then
				execute('rxvt -rv -fn fixed -T "Bittorrent" -e btcurses ' .. url.value .. ' ' .. target.value .. ' ' .. uploadrate.value .. ' &')
			end
			if manycursesbtn.value == 1 then
				execute('rxvt -rv -fn fixed -T "Btmanycurses" -e btmanycurses ' .. url.value .. ' ' .. target.value .. ' ' .. uploadrate.value .. ' &')
			end
			t:delete()
			exit(0)
		end
		
		function cancel2:callback()
		print("Cancelled") 
		exit(0)
		end
	end
end



function get_data()
	
	w = Window{530,350, "Bittorrent Wrapper"}
	b = Box{10,10,510,300; box=Frametype.engraved, label=
	[[Enter the URL or location of the .torrent file you are wanting to use,
	like this, for example.
	
	http://a.scarywater.net/triad/%5BTriad%5D_Air_01.avi.torrent
	
	For Many Curses, enter the path to the folder that contains the torrent 
	files, i.e. /home/dsl/torrents
	
	Then enter the max rate to upload in Kb/s. Default is 0 (infinite).
	It's usually best to set this to 2/3 of your bandwidth.
	
	Curses or Many Curses Mode will download the file(s) to /home/dsl/]]}
	
	url = Input{50,280,320,25,"URL:"}
	
	uploadrate = Input{470,280,40,25,"Max Upload:"}
	if uploadrate.value == "" then
		uploadrate.value = "0"
	end
	
	guibtn = Round_Button{20,20,100,25,"Gui Mode"; value = 1,
	type = Buttontype.radio, callback = type_callback}
	
	cursesbtn = Round_Button{130,20,100,25,"Curses Mode";
	type = Buttontype.radio, callback = type_callback}
	
	manycursesbtn = Round_Button{260,20,100,25,"Many Curses";
	type = Buttontype.radio, callback = type_callback}

	
	startbtn = Button{360,315,100,30,"Start"}
	function startbtn:callback()
		if data_check() then
			if guibtn.value == 1 then
				execute('btdownloadgui.bittorrent --max_upload_rate ' .. uploadrate.value .. ' --url ' .. url.value .. ' &')				
			else
				get_target()
			end
			exit(0)
		end
	end
	
	cancelbtn = Button{70,315,100,30,"Cancel"}
	function cancelbtn:callback()
		print("Cancelled") 
		exit(0)
	end
	
	
	w:end_layout()
	w:show()
	
end

get_data()