-- PSPInstaller is coded by Richard Sparrow (AKA Spike_132000)
-- No reproduction of this code may be used without permission

img_logo = pge.texture.load("S/LOGO.PNG")
pge.gfx.startdrawing()
pge.gfx.clearscreen()
img_logo:activate()
img_logo:draweasy(0,0)
pge.gfx.enddrawing()
pge.gfx.swapbuffers()




arial = pge.font.load("S/ARIAL.TTF", 12, PGE_RAM)
white = pge.gfx.createcolor(255,255,255)
red  = pge.gfx.createcolor(255,0,0)
psp_inst_host = ""
searchstr = ""
img_brow = pge.texture.load("S/BROW.PNG")
img_sett = pge.texture.load("S/SETT.PNG")
img_sear = pge.texture.load("S/SEAR.PNG")
img_exit = pge.texture.load("S/EXIT.PNG")
img_down = pge.texture.load("S/DOWN.PNG")
img_logo = pge.texture.load("S/LOGO.PNG")
srclist = {}
gl={}
al={}
tl={}
ol={}
state = -1
fload = 2

for bout = 255,0,-5 do
	pge.gfx.startdrawing()
	pge.gfx.clearscreen()
	img_logo:activate()
	img_logo:draweasy(0,0,0,bout)
	pge.gfx.enddrawing()
	pge.gfx.swapbuffers()
end
for bout = 0,255,5 do
	pge.gfx.startdrawing()
	pge.gfx.clearscreen()
	img_down:activate()
	img_down:draweasy(0,0,0,bout)
	pge.gfx.enddrawing()
	pge.gfx.swapbuffers()
end

if pge.file.exists("INI.LUA") then
	dofile("INI.LUA")
	dofile("LANG.LUA")
else
	pge.gfx.startdrawing()
	pge.gfx.clearscreen()
	img_down:activate()
	img_down:draweasy(0,0)
	arial:activate()
	arial:print(10,40,white,"INI.LUA"..txt_aaaa)
	arial:print(10,55,white,aaab)
	pge.gfx.enddrawing()
	pge.gfx.swapbuffers()
	while not pge.controls.pressed(PGE_CTRL_CROSS) do
		pge.controls.update()
	end
	INI = pge.file.open("INI.LUA" ,PGE_FILE_CREATE + PGE_FILE_WRONLY)
	INI:write("psp_inst_host = \""..psp_inst_host.."\"\r\ndofile(\"L/en.lng\")")
	INI:close()
	dofile("INI.LUA")
end

pge.controls.update()
if psp_inst_host == "" or pge.controls.held(PGE_CTRL_TRIANGLE) then
	pge.gfx.startdrawing()
	pge.gfx.clearscreen()
	img_down:activate()
	img_down:draweasy(0,0)
	arial:activate()
	if psp_inst_host=="" then
		arial:print(10,40,white,txt_aaac)
		arial:print(10,70,white,txt_aaad)
		arial:print(10,100,white,txt_cros)
	else
		arial:print(10,40,white,txt_aaae)
		arial:print(10,70,white,txt_aaaf)
		arial:print(10,100,white,txt_cros)
	end
	pge.gfx.enddrawing()
	pge.gfx.swapbuffers()
	psp_inst_host=""
	while not pge.controls.pressed(PGE_CTRL_CROSS) do
		pge.controls.update()
	end
	if not pge.utils.oskinit(txt_aaag, "http://srclist.rsparrow.co.uk/list.php") then
			error("Error on osk init.")
	end

	while pge.running() do
		pge.controls.update()
		pge.gfx.startdrawing()
		img_down:activate()
		img_down:draweasy(0,0)
		pge.gfx.enddrawing()
		result, psp_inst_host = pge.utils.oskupdate()
		if result then
			break
		end
		pge.gfx.swapbuffers()
	end
	pge.file.remove("INI.LUA")
	HOS = pge.file.open("INI.LUA" ,PGE_FILE_CREATE + PGE_FILE_WRONLY)
	HOS:write("psp_inst_host = \""..psp_inst_host.."\"\r\n\r\ndofile(\"L/en.lng\")")
	HOS:close()
	dofile("INI.LUA")
end

if pge.file.exists("SOURCELIST.LUA") then
	dofile("SOURCELIST.LUA")
else
	pge.gfx.startdrawing()
	pge.gfx.clearscreen()
	img_down:activate()
	img_down:draweasy(0,0)
	arial:activate()
	arial:print(10,40,white,txt_aaah)
	arial:print(10,55,white,txt_aaai)
	arial:print(10,80,white,txt_cros)
	pge.gfx.enddrawing()
	pge.gfx.swapbuffers()
	while not pge.controls.pressed(PGE_CTRL_CROSS) do
		pge.controls.update()
	end
	SOU_WRIT = pge.file.open("SOURCELIST.LUA",PGE_FILE_CREATE + PGE_FILE_WRONLY)
	SOU_WRIT:write("srclist = {}\r\n\r\nsrclist_update=0\r\n\r\nsrclist_news_title=\"Please Update Sourcelist!\"\r\n\r\nsrclist_news=\"\"")
	SOU_WRIT:close()
	dofile("SOURCELIST.LUA")
end		

pge.net.init()
pge.utils.netinit()

while pge.running() do
	pge.gfx.startdrawing()
	pge.gfx.clearscreen()
	img_down:activate()
	img_down:draweasy(0,0)
	pge.gfx.enddrawing()
	state = pge.utils.netupdate()
	if state == 0 then
		break
	elseif state == 1 then
		pge.net.shutdown()
		break
	elseif state ~= PGE_UTILS_DIALOG_RUNNING then
		pge.net.shutdown()
		break
	end
	pge.gfx.swapbuffers()
end

slo = 1
lse = 1
lto = 1
sto = 1
sco = 1
glo = 1	
gto = 1
alo = 1
ato = 1
tlo = 1
tto = 1
olo = 1
oto = 1
blo = 1
ssl = 1
sst = 1
img_logo = nil
meno=1
state=1

while pge.running() do
	pge.controls.update()
	pge.gfx.startdrawing()
	pge.gfx.clearscreen()
	
	if state == 1 then
		err_msg=""
		img_sett:activate()
		img_sett:draweasy(0,0)
		arial:activate()
		arial:print(120,55,white,txt_aaaj.." 3.11")
		arial:print(120,70,white,txt_aaak)
		arial:print(120,85,white,txt_aaal)
		arial:print(147,125,white,txt_aaam)
		arial:print(120,10,white,srclist_news_title)
		arial:print(130,25,red,srclist_news)
		arial:print(147,155,white,"Language / Langue / Lengua / Sprache")
		arial:print(120,190,white,"Thanks go to : dan369, cmbeke, kartracer12, PL3x, _df_,")
		arial:print(120,205,white,"pspuser1690, Anonymous Blah, Joomla12, Red X, psphkkr4u15,")
		arial:print(120,220,white,"conrados1, Hurarah, cluth1215, ibby123456789, yammata684,")
		arial:print(120,235,white,"Bolt, leo9891, GMT and eFlare for their help and comments!")
		pge.gfx.drawrectoutline(128, 115, 300, 30, white)
		pge.gfx.drawrectoutline(128, 145, 300, 30, white)
		if meno == 1 then
			pge.gfx.drawrect(129,116,3,29,red)
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				pge.gfx.startdrawing()
				pge.gfx.clearscreen()
				img_down:activate()
				img_down:draweasy(0,0)
				arial:activate()
				arial:print(10, 40,white, txt_aaan)
				arial:print(10, 55,white, txt_ples)
				pge.gfx.enddrawing()
				pge.gfx.swapbuffers()
				while pge.running() do
					pge.controls.update()
					pge.gfx.startdrawing()
					pge.gfx.clearscreen()
					img_down:activate()
					img_down:draweasy(0,0)
					dofile("SOURCELIST.LUA")
					pge.file.rename("SOURCELIST.LUA","SOURCELIST.BAK.LUA")
										
					if pge.net.isconnected() then
						if pge.net.getfile(psp_inst_host,"SOURCELIST.LUA") then
							src_mess = txt_aaao
							pge.file.remove("SOURCELIST.BAK.LUA")	
							check=1
							while srclist[check] do
								pge.file.remove(srclist[check].hf)
								check=check+1
							end
							dofile("SOURCELIST.LUA")
							src_mest = srclist_news_title
							src_mesu = srclist_news
							check=1
							while srclist[check] do
								pge.net.getfile(srclist[check].hd,srclist[check].hf)
								check=check+1
							end
						else
							pge.file.rename("SOURCELIST.BAK.LUA","SOURCELIST.LUA")					
							src_mess = txt_aaap
							src_mest = ""
							src_mesu = ""
						end	
					end
									
					if pge.net.isconnected() then
						arial:activate()
						arial:print(10,40,white, src_mess)
						arial:print(10,55,white, txt_star)
						arial:print(10,90,white, src_mest)
						arial:print(10,115,white,src_mesu)
					else
						arial:activate()
						arial:print(10,40,white, txt_aaaq)
						arial:print(10,55,white, txt_star)
					end
					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
					while not pge.controls.pressed(PGE_CTRL_START) do
						pge.controls.update()
					end
					break
				end
			end
		end
		if meno==2 then
			pge.gfx.drawrect(129,146,3,29,red)
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				state=11
			end
		end
		if pge.controls.pressed(PGE_CTRL_RTRIGGER) then
			state=2
		end
		if pge.controls.pressed(PGE_CTRL_UP) then
			meno = 1
		end
		if pge.controls.pressed(PGE_CTRL_DOWN) then
			meno = 2
		end
	end
	if state == 11 then
		pge.gfx.startdrawing()
		pge.gfx.clearscreen()
		img_sett:activate()
		img_sett:draweasy(0,0)
		arial:activate()
		langu = #lang
		if langu == 0 then
			state=1
		else
			arial:print(132,55,white,lang[lse].n)
			arial:print(132,70,white,lang[lse].b)
			pge.gfx.drawrectoutline(124, 50, 336, 48, white)
			if lang[lse+1] then
				arial:print(132,103,white,lang[lse+1].n)
				arial:print(132,118,white,lang[lse+1].b)
				pge.gfx.drawrectoutline(124, 98, 336, 48, white)
			end
			if lang[lse+2] then
				arial:print(132,151,white,lang[lse+2].n)
				arial:print(132,166,white,lang[lse+2].b)
				pge.gfx.drawrectoutline(124, 146, 336, 48, white)
			end
			if lang[lse+3] then
				arial:print(132,199,white,lang[lse+3].n)
				arial:print(132,214,white,lang[lse+3].b)
				pge.gfx.drawrectoutline(124, 194, 336, 48, white)
			end			
			if lto==1 then
				pge.gfx.drawrect(125,51,5,47,red)
			end
			if lto==2 then
				pge.gfx.drawrect(125,99,5,47,red)
			end
			if lto==3 then
				pge.gfx.drawrect(125,147,5,47,red)
			end
			if lto==4 then
				pge.gfx.drawrect(125,195,5,47,red)
			end
			cur_lan_sel = lse + lto - 1
			if pge.controls.pressed(PGE_CTRL_DOWN) or pge.controls.held(PGE_CTRL_RIGHT) then
				
				if cur_lan_sel == langu then
				else
					if lto > 3 then
						lse = lse + 1
						lto = 4
						if lse == slm+1 then
							lse = slm
						end
					elseif lto < 4 then
						lto = lto + 1
					end
				end
			end
			if pge.controls.pressed(PGE_CTRL_UP) or pge.controls.held(PGE_CTRL_LEFT) then 
				
				if lto < 2 then
					lse = lse - 1
					lto = 1
					if lse == 0 then
						lse = 1
					end	
				elseif lto > 1 then
					lto = lto - 1
				end
			end
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				cur_lan_use = lse + lto - 1
				pge.file.remove("INI.LUA")
				INI = pge.file.open("INI.LUA" ,PGE_FILE_CREATE + PGE_FILE_WRONLY)
				INI:write("psp_inst_host = \""..psp_inst_host.."\"\r\ndofile(\""..lang[cur_lan_use].f.."\")")
				INI:close()
				dofile("INI.LUA")
				state=1
			end
		end
		if pge.controls.pressed(PGE_CTRL_CIRCLE) then
			state=1
		end

	end
	if state == 2 then
		img_brow:activate()
		img_brow:draweasy(0,0)
		arial:activate()
		slm = #srclist
		if slm == 0 then
			arial:print(132,55,white,txt_aaar)
		else
			arial:print(120,255,red,err_msg)
			arial:print(132,55,white,srclist[slo].ho)
			arial:print(132,70,white,srclist[slo].ha)
			pge.gfx.drawrectoutline(124, 50, 336, 48, white)
			if srclist[slo+1] then
				arial:print(132,103,white,srclist[slo+1].ho)
				arial:print(132,118,white,srclist[slo+1].ha)
				pge.gfx.drawrectoutline(124, 98, 336, 48, white)
			end
			if srclist[slo+2] then
				arial:print(132,151,white,srclist[slo+2].ho)
				arial:print(132,166,white,srclist[slo+2].ha)
				pge.gfx.drawrectoutline(124, 146, 336, 48, white)
			end
			if srclist[slo+3] then
				arial:print(132,199,white,srclist[slo+3].ho)
				arial:print(132,214,white,srclist[slo+3].ha)
				pge.gfx.drawrectoutline(124, 194, 336, 48, white)
			end			
			if sto==1 then
				pge.gfx.drawrect(125,51,5,47,red)
			end
			if sto==2 then
				pge.gfx.drawrect(125,99,5,47,red)
			end
			if sto==3 then
				pge.gfx.drawrect(125,147,5,47,red)
			end
			if sto==4 then
				pge.gfx.drawrect(125,195,5,47,red)
			end
			cur_src_sel = slo + sto - 1
			if pge.controls.pressed(PGE_CTRL_DOWN) or pge.controls.held(PGE_CTRL_RIGHT) then
				err_msg = ""
				if cur_src_sel == slm then
				else
					if sto > 3 then
						slo = slo + 1
						sto = 4
						if slo == slm+1 then
							slo = slm
						end
					elseif sto < 4 then
						sto = sto + 1
					end
				end
			end
			if pge.controls.pressed(PGE_CTRL_UP) or pge.controls.held(PGE_CTRL_LEFT) then
				err_msg="" 
				if sto < 2 then
					slo = slo - 1
					sto = 1
					if slo == 0 then
						slo = 1
					end	
				elseif sto > 1 then
					sto = sto - 1
				end
			end
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				cur_src_use = slo + sto - 1
				glo = 1
				gto = 1	
				alo = 1
				ato = 1
				tlo = 1
				tto = 1
				olo = 1
				oto = 1
				blo = 1
				cur_gam_sel = 1
				cur_app_sel = 1
				cur_the_sel = 1
				cur_oth_sel = 1
				if pge.file.exists(srclist[cur_src_use].hf) then

					if fload==1 then
					gl = nil
					al = nil
					tl = nil
					ol = nil
					collectgarbage("collect")
					gl={}
					al={}
					tl={}
					ol={}
					end
					dofile(srclist[cur_src_use].hf)
					
					fload = 1
					state=21
				else
					err_msg = srclist[cur_src_use].hf.." - Not Found!"
					state=2
				end
			end
		end
		if pge.controls.pressed(PGE_CTRL_LTRIGGER) then
			state=1
		end
		if pge.controls.pressed(PGE_CTRL_RTRIGGER) then
			state=3
		end
	end
	
	if state == 21 then
		err_msg=""
		img_brow:activate()
		img_brow:draweasy(0,0)
		arial:activate()
		arial:print(132,70,white,txt_aaas)
		arial:print(132,118,white,txt_aaat)
		arial:print(132,166,white,txt_aaau)
		arial:print(132,214,white,txt_aaav)
		pge.gfx.drawrectoutline(124, 50, 336, 48, white)
		pge.gfx.drawrectoutline(124, 98, 336, 48, white)
		pge.gfx.drawrectoutline(124, 146, 336, 48, white)
		pge.gfx.drawrectoutline(124, 194, 336, 48, white)
		if pge.controls.pressed(PGE_CTRL_DOWN) then	
			blo = blo + 1
			if blo > 4 then
				blo = 1
			end
		end
		if pge.controls.pressed(PGE_CTRL_UP) then	
			blo = blo - 1
			if blo < 1 then
				blo = 4
			end
		end
		if pge.controls.pressed(PGE_CTRL_CIRCLE) then
			state=2
		end
		if blo == 1 then
			pge.gfx.drawrect(125,51,5,47,red)
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				state=22		
			end
		end
		if blo == 2 then
			pge.gfx.drawrect(125,99,5,47,red)
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				state=23
			end
		end
		if blo == 3 then
			pge.gfx.drawrect(125,147,5,47,red)
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				state=24
			end
		end
		if blo == 4 then
			pge.gfx.drawrect(125,195,5,47,red)
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				state=25
			end
		end
		if pge.controls.pressed(PGE_CTRL_LTRIGGER) then
			state=1
		end
		if pge.controls.pressed(PGE_CTRL_RTRIGGER) then
			state=3
		end
	end

	if state == 22 then
		if not gl[1] then
			state=21
		end
		img_brow:activate()
		img_brow:draweasy(0,0)
		arial:activate()
		if gl[glo] then
			arial:print(132,55,white,gl[glo].n)
			arial:print(132,70,white,gl[glo].d)
			pge.gfx.drawrectoutline(124, 50, 336, 48, white)
		end
		if gl[glo+1] then
			arial:print(132,103,white,gl[glo+1].n)
			arial:print(132,118,white,gl[glo+1].d)
			pge.gfx.drawrectoutline(124, 98, 336, 48, white)
		end
		if gl[glo+2] then
			arial:print(132,151,white,gl[glo+2].n)
			arial:print(132,166,white,gl[glo+2].d)
			pge.gfx.drawrectoutline(124, 146, 336, 48, white)
		end
		if gl[glo+3] then
			arial:print(132,199,white,gl[glo+3].n)
			arial:print(132,214,white,gl[glo+3].d)
			pge.gfx.drawrectoutline(124, 194, 336, 48, white)
		end			
		if gto==1 then
			pge.gfx.drawrect(125,51,5,47,red)
		end
		if gto==2 then
			pge.gfx.drawrect(125,99,5,47,red)
		end
		if gto==3 then
			pge.gfx.drawrect(125,147,5,47,red)
		end
		if gto==4 then
			pge.gfx.drawrect(125,195,5,47,red)
		end
		cur_gam_sel = glo + gto - 1
		if pge.controls.pressed(PGE_CTRL_DOWN) or pge.controls.held(PGE_CTRL_RIGHT) then
			glm = #gl
			if cur_gam_sel == glm then
			else
				if gto > 3 then
					glo = glo + 1
					gto = 4
					if glo == glm+1 then
						glo = glm
					end
				elseif gto < 4 then
					gto = gto + 1
				end
			end
		end
		if pge.controls.pressed(PGE_CTRL_UP) or pge.controls.held(PGE_CTRL_LEFT) then 
			if gto < 2 then
				glo = glo - 1
				gto = 1
				if glo == 0 then
					glo = 1
				end	
			elseif gto > 1 then
				gto = gto - 1
			end
		end
		if pge.controls.pressed(PGE_CTRL_CROSS) then
			pge.gfx.startdrawing()
			pge.gfx.clearscreen()
			img_down:activate()
			img_down:draweasy(0,0)
			arial:activate()
			arial:print(10, 40,white, txt_baaa.." "..gl[cur_gam_sel].f.."...")
			arial:print(10, 55,white, txt_baab)
			pge.gfx.enddrawing()
			pge.gfx.swapbuffers()
			if pge.net.isconnected() then			
				pge.net.getfile(directory_to_repo..gl[cur_gam_sel].i,"ms0:/"..gl[cur_gam_sel].f)
				pge.gfx.startdrawing()
				pge.gfx.clearscreen()
				img_down:activate()
				img_down:draweasy(0,0)
				arial:activate()
				arial:print(10, 40,white, gl[cur_gam_sel].n.." "..txt_baac)
				arial:print(10, 55,white, txt_star)
				currentwd = pge.dir.getcwd()
				myzipfile = pge.zip.open("ms0:/"..gl[cur_gam_sel].f)
				pge.dir.chdir("ms0:/")
				if myzipfile then

					myzipfile:extract()

					pge.zip.close(myzipfile)
					myzipfile = nil

					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
			
	else

					myzipfile = nil

					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
					pge.gfx.startdrawing()
					pge.gfx.clearscreen()			
					img_down:activate()
					img_down:draweasy(0,0)
					arial:activate()
					arial:print(10, 40,white, gl[cur_gam_sel].n.." "..txt_baad)
					
					arial:print(10, 60,white, txt_baae.." "..srclist[cur_src_use].ho)
					arial:print(10, 80,white, txt_aaaw.." "..srclist[cur_src_use].hc)
					arial:print(10, 120,white, txt_star)
					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
				end
				pge.dir.chdir(currentwd)
				collectgarbage("collect")
				pge.file.remove("ms0:/"..gl[cur_gam_sel].f)
			end
			while not pge.controls.pressed(PGE_CTRL_START) do
				pge.controls.update()
			end
		end
		if pge.controls.pressed(PGE_CTRL_CIRCLE) then
			state=21
		end
		if pge.controls.pressed(PGE_CTRL_LTRIGGER) then
			state=1
		end
		if pge.controls.pressed(PGE_CTRL_RTRIGGER) then
			state=3
		end
	end

	if state == 23 then
		if not al[1] then
			state=21
		end
		img_brow:activate()
		img_brow:draweasy(0,0)
		arial:activate()
		if al[alo] then
			arial:print(132,55,white,al[alo].n)
			arial:print(132,70,white,al[alo].d)
			pge.gfx.drawrectoutline(124, 50, 336, 48, white)
		end
		if al[alo+1] then
			arial:print(132,103,white,al[alo+1].n)
			arial:print(132,118,white,al[alo+1].d)
			pge.gfx.drawrectoutline(124, 98, 336, 48, white)
		end
		if al[alo+2] then
			arial:print(132,151,white,al[alo+2].n)
			arial:print(132,166,white,al[alo+2].d)
			pge.gfx.drawrectoutline(124, 146, 336, 48, white)
		end
		if al[alo+3] then
			arial:print(132,199,white,al[alo+3].n)
			arial:print(132,214,white,al[alo+3].d)
			pge.gfx.drawrectoutline(124, 194, 336, 48, white)
		end			
		if ato==1 then
			pge.gfx.drawrect(125,51,5,47,red)
		end
		if ato==2 then
			pge.gfx.drawrect(125,99,5,47,red)
		end
		if ato==3 then
			pge.gfx.drawrect(125,147,5,47,red)
		end
		if ato==4 then
			pge.gfx.drawrect(125,195,5,47,red)
		end
		cur_app_sel = alo + ato - 1
		if pge.controls.pressed(PGE_CTRL_DOWN) or pge.controls.held(PGE_CTRL_RIGHT) then
			alm = #al
			if cur_app_sel == alm then
			else
				if ato > 3 then
					alo = alo + 1
					ato = 4
					if alo == alm+1 then
						alo = alm
					end
				elseif ato < 4 then
					ato = ato + 1
				end
			end
		end
		if pge.controls.pressed(PGE_CTRL_UP) or pge.controls.held(PGE_CTRL_LEFT) then 
			if ato < 2 then
				alo = alo - 1
				ato = 1
				if alo == 0 then
					alo = 1
				end	
			elseif ato > 1 then
				ato = ato - 1
			end
		end
		if pge.controls.pressed(PGE_CTRL_CROSS) then
			pge.gfx.startdrawing()
			pge.gfx.clearscreen()
			img_down:activate()
			img_down:draweasy(0,0)
			arial:activate()
			arial:print(10, 40,white, txt_baaa.." "..al[cur_app_sel].f.."...")
			arial:print(10, 55,white, txt_baab)
			pge.gfx.enddrawing()
			pge.gfx.swapbuffers()
			if pge.net.isconnected() then			
				pge.net.getfile(directory_to_repo..al[cur_app_sel].i,"ms0:/"..al[cur_app_sel].f)
				pge.gfx.startdrawing()
				pge.gfx.clearscreen()			
				img_down:activate()
				img_down:draweasy(0,0)
				arial:activate()
				arial:print(10, 40,white, al[cur_app_sel].n.." "..txt_baac)
				arial:print(10, 55,white, txt_star)
				currentwd = pge.dir.getcwd()
				myzipfile = pge.zip.open("ms0:/"..al[cur_app_sel].f)
				pge.dir.chdir("ms0:/")
				if myzipfile then

					myzipfile:extract()

					pge.zip.close(myzipfile)
					myzipfile = nil
					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
				else

					myzipfile = nil

					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
					pge.gfx.startdrawing()
					pge.gfx.clearscreen()				
					img_down:activate()
					img_down:draweasy(0,0)
					arial:activate()
					arial:print(10, 40,white, al[cur_app_sel].n.." "..txt_baad)
					
					arial:print(10, 60,white, txt_baae.." "..srclist[cur_src_use].ho)
					arial:print(10, 80,white, txt_aaaw.." "..srclist[cur_src_use].hc)
					arial:print(10, 120,white, txt_star)

					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
				end
				pge.dir.chdir(currentwd)
				collectgarbage("collect")
				pge.file.remove("ms0:/"..al[cur_app_sel].f)
			end
			while not pge.controls.pressed(PGE_CTRL_START) do
				pge.controls.update()
			end
		end
		if pge.controls.pressed(PGE_CTRL_CIRCLE) then
			state=21
		end
		if pge.controls.pressed(PGE_CTRL_LTRIGGER) then
			state=1
		end
		if pge.controls.pressed(PGE_CTRL_RTRIGGER) then
			state=3
		end
	end
	
	if state == 24 then
		if not tl[1] then
			state=21
		end
		img_brow:activate()
		img_brow:draweasy(0,0)
		arial:activate()
		if tl[tlo] then
			arial:print(132,55,white,tl[tlo].n)
			arial:print(132,70,white,tl[tlo].d)
			pge.gfx.drawrectoutline(124, 50, 336, 48, white)
		end
		if tl[tlo+1] then
			arial:print(132,103,white,tl[tlo+1].n)
			arial:print(132,118,white,tl[tlo+1].d)
			pge.gfx.drawrectoutline(124, 98, 336, 48, white)
		end
		if tl[tlo+2] then
			arial:print(132,151,white,tl[tlo+2].n)
			arial:print(132,166,white,tl[tlo+2].d)
			pge.gfx.drawrectoutline(124, 146, 336, 48, white)
		end
		if tl[tlo+3] then
			arial:print(132,199,white,tl[tlo+3].n)
			arial:print(132,214,white,tl[tlo+3].d)
			pge.gfx.drawrectoutline(124, 194, 336, 48, white)
		end					
		if tto==1 then
			pge.gfx.drawrect(125,51,5,47,red)
		end
		if tto==2 then
			pge.gfx.drawrect(125,99,5,47,red)
		end
		if tto==3 then
			pge.gfx.drawrect(125,147,5,47,red)
		end
		if tto==4 then
			pge.gfx.drawrect(125,195,5,47,red)
		end
		cur_the_sel = tlo + tto - 1
		if pge.controls.pressed(PGE_CTRL_DOWN) or pge.controls.held(PGE_CTRL_RIGHT) then
			tlm = #tl
			if cur_the_sel == tlm then
			else
				if tto > 3 then
					tlo = tlo + 1
					tto = 4
					if tlo == tlm+1 then
						tlo = tlm
					end
				elseif tto < 4 then
					tto = tto + 1
				end
			end
		end
		if pge.controls.pressed(PGE_CTRL_UP) or pge.controls.held(PGE_CTRL_LEFT) then 
			if tto < 2 then
				tlo = tlo - 1
				tto = 1
				if tlo == 0 then
					tlo = 1
				end	
			elseif tto > 1 then
				tto = tto - 1
			end
		end
		if pge.controls.pressed(PGE_CTRL_CROSS) then
			pge.gfx.startdrawing()
			pge.gfx.clearscreen()
			img_down:activate()
			img_down:draweasy(0,0)
			arial:activate()
			arial:print(10, 40,white, txt_baaa.." "..tl[cur_the_sel].f.."...")
			arial:print(10, 55,white, txt_baab)
			pge.gfx.enddrawing()
			pge.gfx.swapbuffers()	
			if pge.net.isconnected() then	
				pge.net.getfile(directory_to_repo..tl[cur_the_sel].i,"ms0:/"..tl[cur_the_sel].f)
				pge.gfx.startdrawing()
				pge.gfx.clearscreen()
				img_down:activate()
				img_down:draweasy(0,0)
				arial:activate()
				arial:print(10, 40,white, tl[cur_the_sel].f.." "..txt_baac)
				arial:print(10, 55,white, txt_star)
				currentwd = pge.dir.getcwd()
				myzipfile = pge.zip.open("ms0:/"..tl[cur_the_sel].f)
				pge.dir.chdir("ms0:/")
				if myzipfile then

					myzipfile:extract()

					pge.zip.close(myzipfile)
					myzipfile = nil
					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
				
else

					myzipfile = nil

					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()

					pge.gfx.startdrawing()
					pge.gfx.clearscreen()
					img_down:activate()
					img_down:draweasy(0,0)
					arial:activate()
					arial:print(10, 40,white, tl[cur_the_sel].n.." "..txt_baad)

					arial:print(10, 60,white, txt_baae.." "..srclist[cur_src_use].ho)
					arial:print(10, 80,white, txt_aaaw.." "..srclist[cur_src_use].hc)
					arial:print(10, 120,white, txt_star)

					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
				end
				pge.dir.chdir(currentwd)
				collectgarbage("collect")
				pge.file.remove("ms0:/"..tl[cur_the_sel].f)
			end
			while not pge.controls.pressed(PGE_CTRL_START) do
				pge.controls.update()
			end
		end
		if pge.controls.pressed(PGE_CTRL_CIRCLE) then
			state=21
		end
		if pge.controls.pressed(PGE_CTRL_LTRIGGER) then
			state=1
		end
		if pge.controls.pressed(PGE_CTRL_RTRIGGER) then
			state=3
		end
	end

	if state == 25 then
		if not ol[1] then
			state=21
		end
		img_brow:activate()
		img_brow:draweasy(0,0)
		arial:activate()
		if ol[olo] then
			arial:print(132,55,white,ol[olo].n)
			arial:print(132,70,white,ol[olo].d)
			pge.gfx.drawrectoutline(124, 50, 336, 48, white)
		end
		if ol[olo+1] then
			arial:print(132,103,white,ol[olo+1].n)
			arial:print(132,118,white,ol[olo+1].d)
			pge.gfx.drawrectoutline(124, 98, 336, 48, white)
		end
		if ol[olo+2] then
			arial:print(132,151,white,ol[olo+2].n)
			arial:print(132,166,white,ol[olo+2].d)
			pge.gfx.drawrectoutline(124, 146, 336, 48, white)
		end
		if ol[olo+3] then
			arial:print(132,199,white,ol[olo+3].n)
			arial:print(132,214,white,ol[olo+3].d)
			pge.gfx.drawrectoutline(124, 194, 336, 48, white)
		end			
		if oto==1 then
			pge.gfx.drawrect(125,51,5,47,red)
		end
		if oto==2 then
			pge.gfx.drawrect(125,99,5,47,red)
		end
		if oto==3 then
			pge.gfx.drawrect(125,147,5,47,red)
		end
		if oto==4 then
			pge.gfx.drawrect(125,195,5,47,red)
		end
		cur_oth_sel = olo + oto - 1
		if pge.controls.pressed(PGE_CTRL_DOWN) or pge.controls.held(PGE_CTRL_RIGHT) then
			olm = #ol
			if cur_oth_sel == olm then
			else
				if oto > 3 then
					olo = olo + 1
					oto = 4
					if olo == olm+1 then
						olo = olm
					end
				elseif oto < 4 then
					oto = oto + 1
				end
			end
		end
		if pge.controls.pressed(PGE_CTRL_UP) or pge.controls.held(PGE_CTRL_LEFT) then 
			if oto < 2 then
				olo = olo - 1
				oto = 1
				if olo == 0 then
					olo = 1
				end	
			elseif oto > 1 then
				oto = oto - 1
			end
		end
		if pge.controls.pressed(PGE_CTRL_CROSS) then
			pge.gfx.startdrawing()
			pge.gfx.clearscreen()
			img_down:activate()
			img_down:draweasy(0,0)
			arial:activate()
			arial:print(10, 40,white, txt_baaa.." "..ol[cur_oth_sel].f.."...")
			arial:print(10, 55,white, txt_baab)
			pge.gfx.enddrawing()
			pge.gfx.swapbuffers()
			if pge.net.isconnected() then	
				pge.net.getfile(directory_to_repo..ol[cur_oth_sel].i,"ms0:/"..ol[cur_oth_sel].f)
				pge.gfx.startdrawing()
				pge.gfx.clearscreen()
				img_down:activate()
				img_down:draweasy(0,0)
				arial:activate()
				arial:print(10, 40,white, ol[cur_oth_sel].f.." "..txt_baac)
				arial:print(10, 55,white, txt_star)
				currentwd = pge.dir.getcwd()
				myzipfile = pge.zip.open("ms0:/"..ol[cur_oth_sel].f)
				pge.dir.chdir("ms0:/")
				if myzipfile then

					myzipfile:extract()

					pge.zip.close(myzipfile)
					myzipfile = nil
					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
				
else

					myzipfile = nil

					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
					pge.gfx.startdrawing()
					pge.gfx.clearscreen()
					img_down:activate()
					img_down:draweasy(0,0)
					arial:activate()
					arial:print(10, 40,white, ol[cur_oth_sel].n.." "..txt_baad)

					arial:print(10, 60,white, txt_baae.." "..srclist[cur_src_use].ho)
					arial:print(10, 80,white, txt_aaaw.." "..srclist[cur_src_use].hc)
					arial:print(10, 120,white, txt_star)

					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
				end
				pge.dir.chdir(currentwd)
				collectgarbage("collect")
				pge.file.remove("ms0:/"..ol[cur_oth_sel].f)
			end
			while not pge.controls.pressed(PGE_CTRL_START) do
				pge.controls.update()
			end
		end
		if pge.controls.pressed(PGE_CTRL_CIRCLE) then
			state=21
		end
		if pge.controls.pressed(PGE_CTRL_LTRIGGER) then
			state=1
		end
		if pge.controls.pressed(PGE_CTRL_RTRIGGER) then
			state=3
		end
	end

	if state == 3 then
		err_msg=""
		srcl = #srclist
		if srcl == 0 then
			img_sear:activate()
			img_sear:draweasy(0,0)
			arial:activate()
			arial:print(132,55,white,txt_aaax)
			arial:print(132,70,white,txt_aaay)
		else
			sr = 0
			ssl = 1
			sst = 1
			cur_sea_sel = 1
			img_sear:activate()
			img_sear:draweasy(0,0)
			arial:activate()
			arial:print(120,55,white,txt_aaaz)
			if not pge.controls.pressed(PGE_CTRL_CROSS) then
			else
	
				if not pge.utils.oskinit(txt_aaba, searchstr) then
					error("Error on osk init.")
				end

			
				while pge.running() do
					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
					pge.controls.update()
					pge.gfx.startdrawing()
					img_down:activate()
					img_down:draweasy(0,0)
					pge.gfx.enddrawing()
					result, searchstr = pge.utils.oskupdate()
					if result then
						cur_sea_sel = 1
						state = 31
						break
					else
						state = 3
					end
					
				end
				pge.gfx.swapbuffers()
				pge.gfx.startdrawing()
			end
		end
		if pge.controls.pressed(PGE_CTRL_LTRIGGER) then
			state=2
		end
		if pge.controls.pressed(PGE_CTRL_RTRIGGER) then
			state=4
		end
		
	end

	if state == 31 then
		searchstr = string.lower(searchstr)
		sr = {}
		q=1
		
		for ss=1,srcl do
			if pge.file.exists(srclist[ss].hf) then
				dofile(srclist[ss].hf)
				sglm = #gl
				if sglm==0 then
				else
					for sg=1,sglm do
						sgn = gl[sg].n
						sgd = gl[sg].d
						sgf = gl[sg].f
						sgi = gl[sg].i
						if string.find(string.lower(sgn), searchstr) or string.find(string.lower(sgd), searchstr) or string.find(string.lower(sgf), searchstr) then
							sr[q] = { i = gl[sg].i, n = gl[sg].n, d = gl[sg].d, f = gl[sg].f , l = directory_to_repo, h = srclist[ss].ho }
							q = q + 1 
						else
						end
					end
				end
				salm = #al
				if salm==0 then
				else
					for sa=1,salm do
						san = al[sa].n
						sad = al[sa].d
						saf = al[sa].f
						sai = al[sa].i
						if string.find(string.lower(san), searchstr) or string.find(string.lower(sad), searchstr) or string.find(string.lower(saf), searchstr) then
							sr[q] = { i = al[sa].i, n = al[sa].n, d = al[sa].d, f = al[sa].f , l = directory_to_repo, h = srclist[ss].ho }
							q = q + 1
						else
						end
					end
				end
				stlm = #tl
				if stlm==0 then
				else
					for st=1,stlm do
						stn = tl[st].n
						std = tl[st].d
						stf = tl[st].f
						sti = tl[st].i
						if string.find(string.lower(stn), searchstr) or string.find(string.lower(std), searchstr) or string.find(string.lower(stf), searchstr) then
							sr[q] = { i = tl[st].i, n = tl[st].n, d = tl[st].d, f = tl[st].f , l = directory_to_repo, h = srclist[ss].ho }
							q = q + 1
						else
						end
					end
				end
				solm = #ol
				if solm==0 then
				else
					for so=1,solm do
						son = ol[so].n
						sod = ol[so].d
						sof = ol[so].f
						soi = ol[so].i
						if string.find(string.lower(son), searchstr) or string.find(string.lower(sod), searchstr) or string.find(string.lower(sof), searchstr) then
							sr[q] = { i = ol[so].i, n = ol[so].n, d = ol[so].d, f = ol[so].f , l = directory_to_repo, h = srclist[ss].ho }
							q = q + 1
						else
						end
					end
				end
				else
			end
	
		end
		if sr[1] then
			state = 32
		else
			state = 33
		end
	end

	if state == 32 then
		img_sear:activate()
		img_sear:draweasy(0,0)
		arial:activate()
		if sr[ssl] then
			arial:print(132,55,white,sr[ssl].n.." - "..sr[ssl].h)
			arial:print(132,70,white,sr[ssl].d)
			pge.gfx.drawrectoutline(124, 50, 336, 48, white)

		end
		if sr[ssl+1] then
			arial:print(132,103,white,sr[ssl+1].n.." - "..sr[ssl+1].h)
			arial:print(132,118,white,sr[ssl+1].d)
			pge.gfx.drawrectoutline(124, 98, 336, 48, white)
		end
		if sr[ssl+2] then
			arial:print(132,151,white,sr[ssl+2].n.." - "..sr[ssl+2].h)
			arial:print(132,166,white,sr[ssl+2].d)
			pge.gfx.drawrectoutline(124, 146, 336, 48, white)
		end
		if sr[ssl+3] then
			arial:print(132,199,white,sr[ssl+3].n.." - "..sr[ssl+3].h)
			arial:print(132,214,white,sr[ssl+3].d)
			pge.gfx.drawrectoutline(124, 194, 336, 48, white)
		end			
		if sst==1 then
			pge.gfx.drawrect(125,51,5,47,red)
		end
		if sst==2 then
			pge.gfx.drawrect(125,99,5,47,red)
		end
		if sst==3 then
			pge.gfx.drawrect(125,147,5,47,red)
		end
		if sst==4 then
			pge.gfx.drawrect(125,195,5,47,red)
		end
		cur_sea_sel = ssl + sst - 1
		if pge.controls.pressed(PGE_CTRL_DOWN) or pge.controls.held(PGE_CTRL_RIGHT) then
			srm = #sr
			if cur_sea_sel == srm then
			else
				if sst > 3 then
					ssl = ssl + 1
					sst = 4
					if ssl == srm+1 then
						ssl = srm
					end
				elseif sst < 4 then
					sst = sst + 1
				end
			end
		end
		if pge.controls.pressed(PGE_CTRL_UP) or pge.controls.held(PGE_CTRL_LEFT) then 
			if sst < 2 then
				ssl = ssl - 1
				sst = 1
				if ssl == 0 then
					ssl = 1
				end	
			elseif sst > 1 then
				sst = sst - 1
			end
		end
		if pge.controls.pressed(PGE_CTRL_CROSS) then
			pge.gfx.startdrawing()
			pge.gfx.clearscreen()
			img_down:activate()
			img_down:draweasy(0,0)
			arial:activate()
			arial:print(10, 40,white, txt_baaa.." "..sr[cur_sea_sel].f.."...")
			arial:print(10, 55,white, txt_baab)
			pge.gfx.enddrawing()
			pge.gfx.swapbuffers()
			if pge.net.isconnected() then	
				pge.net.getfile(sr[cur_sea_sel].l..sr[cur_sea_sel].i,"ms0:/"..sr[cur_sea_sel].f)
				pge.gfx.startdrawing()
				pge.gfx.clearscreen()
				img_down:activate()
				img_down:draweasy(0,0)
				arial:activate()
				arial:print(10, 40,white, sr[cur_sea_sel].n.." "..txt_baac)
				arial:print(10, 55,white, txt_star)
				currentwd = pge.dir.getcwd()
				myzipfile = pge.zip.open("ms0:/"..sr[cur_sea_sel].f)
				pge.dir.chdir("ms0:/")
				if myzipfile then

					myzipfile:extract()

					pge.zip.close(myzipfile)
					myzipfile = nil
					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
				
else

					myzipfile = nil

					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
					pge.gfx.startdrawing()
					pge.gfx.clearscreen()
					img_down:activate()
					img_down:draweasy(0,0)
					arial:activate()
					arial:print(10, 40,white, sr[cur_sea_sel].n.." "..txt_baad)

					arial:print(10, 60,white, txt_baae.." "..sr[cur_sea_sel].h)
					arial:print(10, 75,white, txt_star)
					pge.gfx.enddrawing()
					pge.gfx.swapbuffers()
				end
				pge.dir.chdir(currentwd)
				collectgarbage("collect")
				pge.file.remove("ms0:/"..sr[cur_sea_sel].f)
			end
			while not pge.controls.pressed(PGE_CTRL_START) do
				pge.controls.update()
			end
		end
		if pge.controls.pressed(PGE_CTRL_LTRIGGER) then
			state=2
		end
		if pge.controls.pressed(PGE_CTRL_RTRIGGER) then
			state=4
		end
		if pge.controls.pressed(PGE_CTRL_CIRCLE) then
			state=3
		end
	end

	if state == 33 then
		img_sear:activate()
		img_sear:draweasy(0,0)
		arial:activate()
		arial:print(120,55,white,txt_aabb.." \""..searchstr.."\".")
		arial:print(120,70,white,txt_aabc)
		if pge.controls.pressed(PGE_CTRL_LTRIGGER) then
			state=2
		end
		if pge.controls.pressed(PGE_CTRL_RTRIGGER) then
			state=4
		end
		if pge.controls.pressed(PGE_CTRL_CIRCLE) then
			state=3
		end
	end
	
	if state == 4 then
		img_exit:activate()
		img_exit:draweasy(0,0)
		arial:activate()
		arial:print(120,55,white,txt_aabd)
		arial:print(120,70,white,txt_aabe)
		if pge.controls.pressed(PGE_CTRL_CROSS) then
			pge.exit()
		end
		if pge.controls.pressed(PGE_CTRL_LTRIGGER) then
			state=3
		end
	end
	if pge.net.isconnected() then
	else
		oldstate=state
		state=404
	end
	if state == 404 then
		pge.net.shutdown()
		pge.net.init()
		pge.utils.netinit()
		while pge.running() do
			pge.gfx.startdrawing()
			pge.gfx.clearscreen()
			img_down:activate()
			img_down:draweasy(0,0)
			arial:activate()
			arial:print(10, 40,white,txt_aabf)
			state2 = pge.utils.netupdate()
			if state2 == 0 then
				pge.gfx.enddrawing()
				state=oldstate
				break
			elseif state2 == 1 then
				pge.net.shutdown()
				break
			elseif state2 ~= PGE_UTILS_DIALOG_RUNNING then
				pge.net.shutdown()
				break
			else
				arial:print(10, 70,white, txt_aabg)
				arial:print(10, 85,white, txt_aabe)
				pge.gfx.enddrawing()
				pge.gfx.swapbuffers()
				while not pge.controls.pressed(PGE_CTRL_CROSS) do
					pge.controls.update()
				end
				pge.exit()
			end	
			pge.gfx.enddrawing()
			pge.gfx.swapbuffers()
		end
	end	
	pge.gfx.enddrawing()
	pge.gfx.swapbuffers()	
end