-- DO NOT REPRODUCE THIS CODE WITHOUT PERMISSION
-- IT DAMAGES THE HOMEBREW COMMUNITY
-- PSPInstaller by Richard Sparrow (spike_132000/TheMeq)

dofile("INC/SYSTEM.LUA")
dofile("INC/FUNCTION.LUA")
fl={}
if pge.file.exists("INC/FEA.LUA") then
	dofile("INC/FEA.LUA")
end
if pge.file.exists("INC/OPTION.LUA") then
	dofile("INC/OPTION.LUA")
end
if settin_media==1 then
	pi_medi = "ms0:/"
elseif settin_media==2 then
	pi_medi = "ef0:/"
end
init_stars()

while pge.running() do
	pge.controls.update()
	if cur_state==0 then
		for k=0,255,2 do
			pge.gfx.startdrawing()
			pge.gfx.clearscreen()
			IMG_GENE:activate()
			IMG_GENE:draweasy(0,0,0,k)
			finis_drawing()
		end
		pge.delay(4000000)
		for k=0,255,2 do
			pge.gfx.startdrawing()
			pge.gfx.clearscreen()
			IMG_GENE:activate()
			IMG_GENE:draweasy(0,0)
			IMG_BACK:activate()
			IMG_BACK:draweasy(0,0,0,k)
			finis_drawing()
		end
		for k=0,255,2 do
			start_drawing()
			update_stars()
			IMG_LOGO:activate()
			IMG_LOGO:draweasy(174,64,0,k)
			finis_drawing()
		end
		if settin_delfi==1 then
			if pge.file.exists("INC/FEA.LUA") then
				dofile("INC/FEA.LUA")
				if #fl then
					for k=1,#fl do
						if pge.file.exists(pi_medi..fl[k].f) then
							pge.file.remove(pi_medi..fl[k].f)
						end
					end
				end
			end
			clsold()
			if pge.file.exists("INC/SIG.LUA") then
				dofile("INC/SIG.LUA")
				if #gl then
					for k=1,#gl do
						if pge.file.exists(pi_medi..gl[k].f) then
							pge.file.remove(pi_medi..gl[k].f)
						end
					end
				end
				if #al then
					for k=1,#al do
						if pge.file.exists(pi_medi..al[k].f) then
							pge.file.remove(pi_medi..al[k].f)
						end
					end
				end
				if #ol then
					for k=1,#ol do
						if pge.file.exists(pi_medi..ol[k].f) then
							pge.file.remove(pi_medi..ol[k].f)
						end
					end
				end
				if #tl then
					for k=1,#tl do
						if pge.file.exists(pi_medi..tl[k].f) then
							pge.file.remove(pi_medi..tl[k].f)
						end
					end
				end
			end
			clsold()
			if pge.file.exists("INC/UNSIG.LUA") then
				dofile("INC/UNSIG.LUA")
				if #gl then
					for k=1,#gl do
						if pge.file.exists(pi_medi..gl[k].f) then
							pge.file.remove(pi_medi..gl[k].f)
						end
					end
				end
				if #al then
					for k=1,#al do
						if pge.file.exists(pi_medi..al[k].f) then
							pge.file.remove(pi_medi..al[k].f)
						end
					end
				end
				if #tl then
					for k=1,#tl do
						if pge.file.exists(pi_medi..tl[k].f) then
							pge.file.remove(pi_medi..tl[k].f)
						end
					end
				end
				if #ol then
					for k=1,#ol do
						if pge.file.exists(pi_medi..ol[k].f) then
							pge.file.remove(pi_medi..ol[k].f)
						end
					end
				end
			end
		end
		for k=255,0,-2 do
			start_drawing()
			update_stars()
			IMG_LOGO:activate()
			IMG_LOGO:draweasy(174,64,0,k)
			finis_drawing()
		end
		for k=0,255,2 do
			start_drawing()
			update_stars()
			IMG_DEAT:activate()
			IMG_DEAT:draweasy(140,111,0,k)
			finis_drawing()
		end
		for k=255,0,-2 do
			start_drawing()
			update_stars()
			IMG_DEAT:activate()
			IMG_DEAT:draweasy(140,111,0,k)
			finis_drawing()
		end
		cur_state=1
	end

	if cur_state==1 then
		pge.net.init()
		pge.utils.netinit()
		cur_state=2
	end

	if cur_state==2 then
		start_drawing()
		update_stars()
		pge.gfx.enddrawing()
		pge.net.init()
		nstate = pge.utils.netupdate()
		if nstate == 0 then
			online=1
			cur_state=3
		elseif nstate == 1 then
			pge.net.shutdown()
			online=0
			cur_state=4
			SND_COMP:play()
		elseif nstate ~= PGE_UTILS_DIALOG_RUNNING then
			pge.net.shutdown()
			online=0
			cur_state=4
			SND_COMP:play()
		end
		pge.gfx.swapbuffers()
	end

	if cur_state==3 then
		start_drawing()
		pge.gfx.drawrect(5,5,470,262,COL_ALBL)
		FON_ARIA:activate()
		FON_ARIA:print(10,10,COL_WHIT,"PSPInstaller is downloading important files! Please wait!")
		FON_ARIA:print(10,25,COL_WHIT,"Should only take a few seconds...")
		finis_drawing()
		if pge.file.exists("INC/SIG.LUA") then
			pge.file.remove("INC/SIG.LUA")
		end
		pge.net.getfile("http://sig.pspinstaller.co.uk/repo.php","INC/SIG.LUA")
		if not pge.file.exists("INC/SIG.LUA") then
			stop_update=1
		end
		if pge.file.exists("INC/UNSIG.LUA") then
			pge.file.remove("INC/UNSIG.LUA")
		end
		pge.net.getfile("http://unsig.pspinstaller.co.uk/repo.php","INC/UNSIG.LUA")
		if not pge.file.exists("INC/UNSIG.LUA") then
			stop_update=1
		end
		if pge.file.exists("INC/FEA.LUA") then
			pge.file.remove("INC/FEA.LUA")
		end
		pge.net.getfile("http://pspinstaller.co.uk/fea/repo.txt","INC/FEA.LUA")
		if not pge.file.exists("INC/FEA.LUA") then
			stop_update=1
		end
		if pge.file.exists("INC/IMG_ADV1.PNG") then
			pge.file.remove("INC/IMG_ADV1.PNG")
		end
		if pge.file.exists("INC/IMG_ADV2.PNG") then
			pge.file.remove("INC/IMG_ADV2.PNG")
		end
		if pge.file.exists("INC/IMG_ADV3.PNG") then
			pge.file.remove("INC/IMG_ADV3.PNG")
		end
		pge.net.getfile("http://pspinstaller.co.uk/fea/IMG_ADV1.PNG","INC/IMG_ADV1.PNG")
		if not pge.file.exists("INC/IMG_ADV1.PNG") then
			stop_update=1
		end
		pge.net.getfile("http://pspinstaller.co.uk/fea/IMG_ADV2.PNG","INC/IMG_ADV2.PNG")
		if not pge.file.exists("INC/IMG_ADV2.PNG") then
			stop_update=1
		end
		pge.net.getfile("http://pspinstaller.co.uk/fea/IMG_ADV3.PNG","INC/IMG_ADV3.PNG")
		if not pge.file.exists("INC/IMG_ADV3.PNG") then
			stop_update=1
		end
		if stop_update==1 then
			finis_drawing()
			start_drawing()
			pge.gfx.drawrect(5,5,470,262,COL_ALBL)
			FON_ARIA:activate()
			FON_ARIA:print(5,10,COL_WHIT,wordwrap("PSPInstaller failed to download important files.",FON_ARIA,460))
			FON_ARIA:print(10,30,COL_WHIT,"This may be because -") 
			FON_ARIA:print(30,45,COL_WHIT,"The server is down")
			FON_ARIA:print(30,60,COL_WHIT,"You lost internet connection")
			FON_ARIA:print(10,80,COL_WHIT,"Please restart PSPInstaller by Pressing (X) to exit.")
			FON_ARIA:print(10,100,COL_WHIT,"Sorry for the inconvenience")
			finis_drawing()
			while true do
				pge.controls.update()
				if pge.controls.pressed(PGE_CTRL_CROSS) then
					for k=0,255,5 do
						start_drawing()
						pge.gfx.drawrect(5,5,470,262,COL_ALBL)
						FON_ARIA:activate()
						FON_ARIA:print(5,10,COL_WHIT,wordwrap("PSPInstaller failed to download important files.",FON_ARIA,460))
						FON_ARIA:print(10,30,COL_WHIT,"This may be because -") 
						FON_ARIA:print(30,45,COL_WHIT,"The server is down")
						FON_ARIA:print(30,60,COL_WHIT,"You lost internet connection")
						FON_ARIA:print(10,80,COL_WHIT,"Please restart PSPInstaller by Pressing (X) to exit.")
						FON_ARIA:print(10,100,COL_WHIT,"Sorry for the inconvenience")
						pge.gfx.drawrect(0,0,480,272,pge.gfx.createcolor(0,0,0,k))
						finis_drawing()
					end
				pge.exit()
				end
			end
			
		end
		IMG_ADV1 = pge.texture.load("INC/IMG_ADV1.PNG")
		IMG_ADV2 = pge.texture.load("INC/IMG_ADV2.PNG")
		IMG_ADV3 = pge.texture.load("INC/IMG_ADV3.PNG")
		if pge.file.exists("INC/FEA.LUA") then
			dofile("INC/FEA.LUA")
		end
		cur_state=4
		SND_COMP:play()
	end

	if cur_state==4 then
		start_drawing()
		update_stars()
		IMG_PSP6:activate()
		IMG_PSP6:draweasy(0,5)
		pge.gfx.drawrect(5,97,170,30,COL_ALBL)
		pge.gfx.drawrect(5,132,170,30,COL_ALBL)
		pge.gfx.drawrect(5,167,170,30,COL_ALBL)
		pge.gfx.drawrect(5,202,170,30,COL_ALBL)
		pge.gfx.drawrect(5,237,170,30,COL_ALBL)
		IMG_ADV1:activate()
		IMG_ADV1:draweasy(180,5,0,100)
		IMG_ADV2:activate()
		IMG_ADV2:draweasy(345,5,0,100)
		IMG_ADV3:activate()
		IMG_ADV3:draweasy(345,139,0,100)
		if option_mainx==1 then
			if option_mainy==1 then
				drawcursor(5,97,170,30,1)
				if pge.controls.pressed(PGE_CTRL_CROSS) then
					SND_ADVA:play()
					repo_type=1
					repo_file="INC/SIG.LUA"
					repo_desc="Signed is mainly for PSP's with Official Firmware on them but everything will still work for Custom Firmware PSP's also. Please choose a catagory below."
					if pge.file.exists(repo_file) then
						repo_check=1
					else
						repo_check=0
					end
					cur_state=10
				end
			elseif option_mainy==2 then
				drawcursor(5,132,170,30,1)
				if pge.controls.pressed(PGE_CTRL_CROSS) then
					SND_ADVA:play()
					repo_type=2
					repo_file="INC/UNSIG.LUA"
					repo_desc="Unsigned is mainly for PSP's with Custom Firmware on them. Some things in here may work with PSP's on Official Firmare. Please choose a catagory below." 
					if pge.file.exists(repo_file) then
						repo_check=1
					else
						repo_check=0
					end
					cur_state=10
				end
			elseif option_mainy==3 then
				drawcursor(5,167,170,30,1)
				if pge.controls.pressed(PGE_CTRL_CROSS) then
					SND_ADVA:play()
					old_medi = settin_media
					old_delf = settin_delfi
					old_dlty = settin_dltyp
					cur_state=6
				end
			elseif option_mainy==4 then
				drawcursor(5,202,170,30,1)
				if pge.controls.pressed(PGE_CTRL_CROSS) then
					SND_ADVA:play()
					cur_state=5
				end
			elseif option_mainy==5 then
				drawcursor(5,237,170,30,1)
				if pge.controls.pressed(PGE_CTRL_CROSS) then
					SND_ADVA:play()
					cur_state=7
				end
			end
			if pge.controls.pressed(PGE_CTRL_DOWN) then
				option_mainy=option_mainy+1
				SND_SCRO:play()
				if option_mainy>5 then
					option_mainy=1
				end
			elseif pge.controls.pressed(PGE_CTRL_UP) then
				option_mainy=option_mainy-1
				SND_SCRO:play()
				if option_mainy<1 then
					option_mainy=5
				end
			elseif pge.controls.pressed(PGE_CTRL_LEFT) then
				SND_SCRO:play()
				option_mainy=1
				option_mainx=3
			elseif pge.controls.pressed(PGE_CTRL_RIGHT) then
				SND_SCRO:play()
				option_mainy=1
				option_mainx=2
			end
		elseif option_mainx==2 then
			if option_mainy==1 then
				IMG_ADV1:activate()
				IMG_ADV1:draweasy(180,5)
				drawcursor(180,5,160,262,2)
				if pge.controls.pressed(PGE_CTRL_CROSS) then
					SND_ADVA:play()
					clsold()
					dofile("INC/FEA.LUA")
					show_group=fl
					cur_selected=1
					cur_state=14
					from_menu=1
					directory_to_repo="http://pspinstaller.co.uk/fea/"
				end
			end
			if pge.controls.pressed(PGE_CTRL_LEFT) then
				SND_SCRO:play()
				option_mainy=1
				option_mainx=1
			elseif pge.controls.pressed(PGE_CTRL_RIGHT) then
				SND_SCRO:play()
				option_mainy=1
				option_mainx=3
			end
		elseif option_mainx==3 then
			if option_mainy==1 then
				IMG_ADV2:activate()
				IMG_ADV2:draweasy(345,5)
				drawcursor(345,5,128,128,2)
				if pge.controls.pressed(PGE_CTRL_CROSS) then
					SND_ADVA:play()
					clsold()
					dofile("INC/FEA.LUA")
					show_group=fl
					cur_selected=2
					cur_state=14
					from_menu=1
					directory_to_repo="http://pspinstaller.co.uk/fea/"
				end
			elseif option_mainy==2 then
				IMG_ADV3:activate()
				IMG_ADV3:draweasy(345,139)
				drawcursor(345,139,128,128,2)
				if pge.controls.pressed(PGE_CTRL_CROSS) then
					SND_ADVA:play()
					clsold()
					dofile("INC/FEA.LUA")
					show_group=fl
					cur_selected=3
					cur_state=14
					from_menu=1
					directory_to_repo="http://pspinstaller.co.uk/fea/"
				end
			end
			if pge.controls.pressed(PGE_CTRL_DOWN) then
				SND_SCRO:play()
				option_mainy=option_mainy+1
				if option_mainy>2 then
					option_mainy=1
				end
			elseif pge.controls.pressed(PGE_CTRL_UP) then
				SND_SCRO:play()
				option_mainy=option_mainy-1
				if option_mainy<1 then
					option_mainy=2
				end
			elseif pge.controls.pressed(PGE_CTRL_LEFT) then
				SND_SCRO:play()
				option_mainy=1
				option_mainx=2
			elseif pge.controls.pressed(PGE_CTRL_RIGHT) then
				SND_SCRO:play()
				option_mainy=1
				option_mainx=1
			end
		end
		FON_ARIA:activate()
		FON_ARIA:print(68,107,COL_WHIT,"Signed")
		FON_ARIA:print(63,142,COL_WHIT,"Unsigned")
		FON_ARIA:print(65,177,COL_WHIT,"Options")
		FON_ARIA:print(71,212,COL_WHIT,"About")
		FON_ARIA:print(78,247,COL_WHIT,"Exit")
		finis_drawing()
	end

	if cur_state==5 then
		start_drawing()
		update_stars()
		IMG_PSP6:activate()
		IMG_PSP6:draweasy(0,5)
		pge.gfx.drawrect(180,5,295,262,COL_ALBL)
		pge.gfx.drawrect(5,237,170,30,COL_ALBL)
		if option_abouy==1 then
			drawcursor(5,237,170,30,1)
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				SND_ADVA:play()
				if pge.file.exists("INC/FEA.LUA") then
					fea_check=1
				else
					fea_check=0
				end
				cur_state=60
			end
		end
		FON_ARIA:activate()
		FON_ARIA:print(185,10,COL_WHIT,"PSPInstaller by Richard Sparrow (spike_132000/TheMeq)")
		FON_ARIA:print(182,35,COL_WHIT,wordwrap("PSPInstaller is a helpful homebrew application which allows you to download games, applications, themes and utilities to your PSP without the need for a computer. You can help PSPInstaller grow by helping us upload homebrew and themes to our repos!", FON_ARIA, 285))
		FON_ARIA:print(185,135,COL_WHIT,"PSPInstaller Version: "..thisversion.."\n\n")
		FON_ARIA:print(182,205,COL_WHIT,wordwrap("Press (X) to check for updates or (O) to return to main menu!",FON_ARIA,285))
		FON_ARIA:print(40,247,COL_WHIT,"Check for Updates")
		if pge.controls.pressed(PGE_CTRL_CIRCLE) then
			SND_BACK:play()
			cur_state=4
		end
		finis_drawing()
	end

	if cur_state==6 then
		start_drawing()
		update_stars()
		IMG_PSP6:activate()
		IMG_PSP6:draweasy(0,5)
		pge.gfx.drawrect(5,97,170,170,COL_ALBL)
		pge.gfx.drawrect(180,5,295,48,COL_ALBL)
		pge.gfx.drawrect(180,58,295,48,COL_ALBL)
		pge.gfx.drawrect(180,111,295,48,COL_ALBL)
		pge.gfx.drawrect(180,164,295,49,COL_ALBL)
		pge.gfx.drawrect(180,218,295,49,COL_ALBL)
		if option_optiy==1 then
			drawcursor(180,5,295,48,1)
			if pge.controls.pressed(PGE_CTRL_LEFT) then
				SND_SCRO:play()
				settin_media=1
			elseif pge.controls.pressed(PGE_CTRL_RIGHT) then
				SND_SCRO:play()
				settin_media=2
			end
		elseif option_optiy==2 then
			drawcursor(180,58,295,48,1)
			if pge.controls.pressed(PGE_CTRL_LEFT) then
				SND_SCRO:play()
				settin_dltyp=1 
			elseif pge.controls.pressed(PGE_CTRL_RIGHT) then
				SND_SCRO:play()
				settin_dltyp=2
			end
		elseif option_optiy==3 then
			drawcursor(180,111,295,48,1)
			if pge.controls.pressed(PGE_CTRL_LEFT) then
				SND_SCRO:play()
				settin_delfi=1
			elseif pge.controls.pressed(PGE_CTRL_RIGHT) then
				SND_SCRO:play()
				settin_delfi=2 
			end
		elseif option_optiy==4 then
			drawcursor(180,164,295,49,1)
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				SND_COMP:play()
				if pge.file.exists("INC/OPTION.LUA") then
					pge.file.remove("INC/OPTION.LUA")
					OPF = pge.file.open("INC/OPTION.LUA" ,PGE_FILE_CREATE + PGE_FILE_WRONLY)
					OPF:write("settin_media="..settin_media.."\r\nsettin_delfi="..settin_delfi.."\r\nsettin_dltyp="..settin_dltyp)
					OPF:close()
					if settin_media==1 then
						pi_medi = "ms0:/"
					elseif settin_media==2 then
						pi_medi = "ef0:/"
					end
					cur_state=4
				end
			end
		elseif option_optiy==5 then
			drawcursor(180,218,295,49,1)
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				SND_BACK:play()
				settin_media=old_medi
				settin_delfi=old_delf
				settin_dltyp=old_dlty
				cur_state=4
			end
		end
		if pge.controls.pressed(PGE_CTRL_UP) then
			SND_SCRO:play()
			option_optiy=option_optiy-1
			if option_optiy==0 then
				option_optiy=5
			end
		elseif pge.controls.pressed(PGE_CTRL_DOWN) then
			SND_SCRO:play()
			option_optiy=option_optiy+1
			if option_optiy==6 then
				option_optiy=1
			end
		end
		FON_ARIA:activate()
		if settin_media==1 then
			FON_ARIA:print(190,30,COL_WHIT,"Memory Stick")
		elseif settin_media==2 then
			FON_ARIA:print(190,30,COL_WHIT,"PSPGo Internal Flash (N1000 Model Only)")
		end
		if settin_dltyp==1 then
			FON_ARIA:print(190,83,COL_WHIT,"Use Progress Bar (Slightly Slower)")
		elseif settin_dltyp==2 then
			FON_ARIA:print(190,83,COL_WHIT,"Straight Download")
		end
		if settin_delfi==1 then
			FON_ARIA:print(190,136,COL_WHIT,"Delete on Startup")
		elseif settin_delfi==2 then
			FON_ARIA:print(190,136,COL_WHIT,"Don't Delete")
		end
		FON_ARIA:print(185,10,COL_WHIT,"Use media type: ")
		FON_ARIA:print(185,116,COL_WHIT,"Delete old files on startup:")
		FON_ARIA:print(185,63,COL_WHIT,"Download Type:")
		FON_ARIA:print(185,169,COL_WHIT,"Save and Return")
		FON_ARIA:print(190,189,COL_WHIT,"Save changes and go back to main menu")
		FON_ARIA:print(185,223,COL_WHIT,"Return and Don't Save")
		FON_ARIA:print(190,243,COL_WHIT,"Don't save changes and go back to main menu")
		FON_ARIA:print(5,102,COL_WHIT,wordwrap("Use these options to configure PSPInstaller to work to your liking.",FON_ARIA,160))
		if pge.controls.pressed(PGE_CTRL_CIRCLE) then
			SND_BACK:play()
			settin_media=old_medi
			settin_delfi=old_delf
			settin_dltyp=old_dlty
			cur_state=4
		end
		finis_drawing()
	end

	if cur_state==10 then
		from_menu=0
		start_drawing()
		update_stars()
		IMG_PSP6:activate()
		IMG_PSP6:draweasy(0,5)
		if repo_check==0 then
			pge.gfx.drawrect(180,5,290,262,COL_ALBL)
			pge.gfx.drawrect(5,237,170,30,COL_ALBL)
			drawcursor(5,237,170,30,1)
			FON_ARIA:activate()
			FON_ARIA:print(182,10,COL_WHIT,wordwrap("A required file ("..repo_file..") appears to be missing! Please restart PSPInstaller and ensure that all required files are downloaded! Press (X) or (O) to return to main menu.", FON_ARIA, 285))
			FON_ARIA:print(78,247,COL_WHIT,"Back")
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				SND_BACK:play()
				cur_state=4
			end
		else
			pge.gfx.drawrect(180,5,295,262,COL_ALBL)
			pge.gfx.drawrect(5,97,170,30,COL_ALBL)
			pge.gfx.drawrect(5,132,170,30,COL_ALBL)
			pge.gfx.drawrect(5,167,170,30,COL_ALBL)
			pge.gfx.drawrect(5,202,170,30,COL_ALBL)
			pge.gfx.drawrect(5,237,170,30,COL_ALBL)
			clsold()
			dofile(repo_file)
			if repo_file == "INC/SIG.LUA" then
				directory_to_repo="http://pspinstaller.co.uk/sig/"
			elseif repo_file == "INC/UNSIG.LUA" then
				directory_to_repo="http://pspinstaller.co.uk/unsig/"
			end
			cur_state=11
		end
		if pge.controls.pressed(PGE_CTRL_CIRCLE) then
			SND_BACK:play()
			cur_state=4
		end
		finis_drawing()

	end

	if cur_state==11 then
		start_drawing()
		update_stars()
		IMG_PSP6:activate()
		IMG_PSP6:draweasy(0,5)
		pge.gfx.drawrect(5,97,170,30,COL_ALBL)
		pge.gfx.drawrect(180,5,295,262,COL_ALBL)
		pge.gfx.drawrect(5,132,170,30,COL_ALBL)
		pge.gfx.drawrect(5,167,170,30,COL_ALBL)
		pge.gfx.drawrect(5,202,170,30,COL_ALBL)
		pge.gfx.drawrect(5,237,170,30,COL_ALBL)
		if option_cat == 1 then
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				SND_ADVA:play()
				option_catlin=1
				option_catopt=1
				show_group=gl
				cur_state=12
			end
			drawcursor(5,97,170,30,1)
			IMG_CAT1:activate()
			IMG_CAT1:draweasy(281,80)
			FON_ARIA:activate()
			FON_ARIA:print(185,187,COL_WHIT,"Our Repo has "..#gl.. " games!")
			FON_ARIA:print(185,217,COL_WHIT,"Last Database Update: "..last_update)
		elseif option_cat == 2 then
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				SND_ADVA:play()
				option_catlin=1
				option_catopt=1
				show_group=al
				cur_state=12
			end
			drawcursor(5,132,170,30,1)
			IMG_CAT2:activate()
			IMG_CAT2:draweasy(281,80)
			FON_ARIA:activate()
			FON_ARIA:print(185,187,COL_WHIT,"Our Repo has "..#al.. " applications!")
			FON_ARIA:print(185,217,COL_WHIT,"Last Database Update: "..last_update)
		elseif option_cat == 3 then
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				SND_ADVA:play()
				option_catlin=1
				option_catopt=1
				show_group=tl
				cur_state=12
			end
			drawcursor(5,167,170,30,1)
			IMG_CAT3:activate()
			IMG_CAT3:draweasy(281,80)
			FON_ARIA:activate()
			FON_ARIA:print(185,187,COL_WHIT,"Our Repo has "..#tl.. " themes!")
			FON_ARIA:print(185,217,COL_WHIT,"Last Database Update: "..last_update)
		elseif option_cat == 4 then
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				SND_ADVA:play()
				option_catlin=1
				option_catopt=1
				show_group=ol
				cur_state=12
			end
			drawcursor(5,202,170,30,1)
			IMG_CAT4:activate()
			IMG_CAT4:draweasy(281,80)
			FON_ARIA:activate()
			FON_ARIA:print(185,187,COL_WHIT,"Our Repo has "..#ol.. " other items!")
			FON_ARIA:print(185,217,COL_WHIT,"Last Database Update: "..last_update)
		elseif option_cat == 5 then
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				SND_ADVA:play()
				cur_state=13
			end
			drawcursor(5,237,170,30,1)
			IMG_CAT5:activate()
			IMG_CAT5:draweasy(281,80)
			FON_ARIA:activate()
			FON_ARIA:print(185,187,COL_WHIT,"Can't find what your after? Search for it!")
		end
		if pge.controls.pressed(PGE_CTRL_DOWN) then
			SND_SCRO:play()
			option_cat = option_cat + 1
			if option_cat==6 then
				option_cat=1
			end
		elseif pge.controls.pressed(PGE_CTRL_UP) then
			SND_SCRO:play()
			option_cat = option_cat - 1
			if option_cat==0 then
				option_cat=5
			end
		end
		FON_ARIA:activate()
		FON_ARIA:print(182,10,COL_WHIT,wordwrap(repo_desc,FON_ARIA,285))
		FON_ARIA:print(70,107,COL_WHIT,"Games")
		FON_ARIA:print(60,142,COL_WHIT,"Applications")
		FON_ARIA:print(69,177,COL_WHIT,"Themes")
		FON_ARIA:print(72,212,COL_WHIT,"Other")
		FON_ARIA:print(70,247,COL_WHIT,"Search")
		finis_drawing()
		if pge.controls.pressed(PGE_CTRL_CIRCLE) then
			SND_BACK:play()
			cur_state=4
		end
	end

	if cur_state==12 then
		start_drawing()
		update_stars()
		IMG_PSP6:activate()
		IMG_PSP6:draweasy(0,5)
		
		if not show_group[1] then
			pge.gfx.drawrect(5,97,170,170,COL_ALBL)
			pge.gfx.drawrect(180,5,295,262,COL_ALBL)
			FON_ARIA:activate()
			FON_ARIA:print(182,10,COL_WHIT,wordwrap("This section has no items available for download at the moment. Why not help PSPInstaller grow and contribute at one of our repo's. Press (O) to return",FON_ARIA,285))
			if pge.controls.pressed(PGE_CTRL_CIRCLE) then
				SND_BACK:play()
				cur_state=11
			end
		else
			pge.gfx.drawrect(5,97,170,170,COL_ALBL)
			pge.gfx.drawrect(180,5,295,48,COL_ALBL)
			pge.gfx.drawrect(180,58,295,48,COL_ALBL)
			pge.gfx.drawrect(180,111,295,48,COL_ALBL)
			pge.gfx.drawrect(180,164,295,49,COL_ALBL)
			pge.gfx.drawrect(180,218,295,49,COL_ALBL)
			if option_catopt==1 then
				drawcursor(180,5,295,48,1)
			end
			if option_catopt==2 then
				drawcursor(180,58,295,48,1)
			end
			if option_catopt==3 then
				drawcursor(180,111,295,48,1)
			end
			if option_catopt==4 then
				drawcursor(180,164,295,48,1)
			end
			if option_catopt==5 then
				drawcursor(180,218,295,48,1)
			end
			
			FON_ARIA:activate()
			if show_group[option_catlin] then
				FON_ARIA:print(185,10,COL_WHIT,option_catlin .. ". ".. show_group[option_catlin].n)
				FON_ARIA:print(185,30,COL_WHIT,string.sub(string.gsub(show_group[option_catlin].d, "\r\n", " "), 1, 40) .. "...")
			end
			if show_group[option_catlin+1] then
				FON_ARIA:print(185,63,COL_WHIT,option_catlin+1 .. ". ".. show_group[option_catlin+1].n)
				FON_ARIA:print(185,83,COL_WHIT,string.sub(string.gsub(show_group[option_catlin+1].d, "\r\n", " "), 1, 40) .. "...")
			end
			if show_group[option_catlin+2] then
				FON_ARIA:print(185,116,COL_WHIT,option_catlin+2 .. ". ".. show_group[option_catlin+2].n)
				FON_ARIA:print(185,136,COL_WHIT,string.sub(string.gsub(show_group[option_catlin+2].d, "\r\n", " "), 1, 40) .. "...")
			end
			if show_group[option_catlin+3] then
				FON_ARIA:print(185,169,COL_WHIT,option_catlin+3 .. ". ".. show_group[option_catlin+3].n)
				FON_ARIA:print(185,189,COL_WHIT,string.sub(string.gsub(show_group[option_catlin+3].d, "\r\n", " "), 1, 40) .. "...")
			end
			if show_group[option_catlin+4] then
				FON_ARIA:print(185,223,COL_WHIT,option_catlin+4 .. ". ".. show_group[option_catlin+4].n)
				FON_ARIA:print(185,243,COL_WHIT,string.sub(string.gsub(show_group[option_catlin+4].d, "\r\n", " "), 1, 40) .. "...")
			end
			FON_ARIA:print(5,102,COL_WHIT,wordwrap("MeqSoft will not take responsibility for files downloaded but will try to prevent issues where possible",FON_ARIA,160))
			cur_selected = option_catopt + option_catlin - 1
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				SND_ADVA:play()
				cur_state=14					
			end
			if pge.controls.pressed(PGE_CTRL_DOWN) or pge.controls.held(PGE_CTRL_RIGHT) then
				if cur_selected ~= #show_group then
					SND_SCRO:play()
				end
				if cur_selected == #show_group then
				else
					if option_catopt ==5 then
						option_catlin=option_catlin+1
						option_catopt=5
						if option_catlin == #show_group+1 then
							option_catlin = #show_group
						end
					elseif option_catopt<5 then
						option_catopt = option_catopt + 1
					end
				end
			end
			if pge.controls.pressed(PGE_CTRL_UP) or pge.controls.held(PGE_CTRL_LEFT) then
				if cur_selected ~= 1 then
					SND_SCRO:play()
				end
				if option_catopt == 1 then
					option_catlin = option_catlin - 1
					option_catopt = 1
					if option_catlin <= 1 then
						option_catlin = 1
					end
				elseif option_catopt > 1 then
					option_catopt = option_catopt - 1
				end
			end

		end
		finis_drawing()
		if pge.controls.pressed(PGE_CTRL_CIRCLE) then
			SND_BACK:play()
			cur_state=11
		end
	end

	if cur_state==13 then
		start_drawing()
		update_stars()
		q=1
		searchstr=""
		clsold()
		if not pge.utils.oskinit("Enter Search Query", searchstr) then
			error("Error on osk init.")
		end
		while true do
			pge.gfx.enddrawing()
			pge.gfx.swapbuffers()
			pge.controls.update()
			start_drawing()
			update_stars()
			pge.gfx.enddrawing()
			result, searchstr = pge.utils.oskupdate()
			if result then
				cur_state=131
				break
			else
				cur_state=4
				
			end
		end
		pge.gfx.swapbuffers()
		pge.gfx.startdrawing()
	end

	if cur_state==131 then
		searchstr = string.lower(searchstr)
		sr = {}
		q=1
		dofile(repo_file)
		
		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, s=gl[sg].s, e=gl[sg].e,z=gl[sg].z }
					q = q + 1 
				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 , s=al[sa].s, e=al[sa].e,z=gl[sa].z}
					q = q + 1
				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 ,s=tl[st].s, e=tl[st].e,z=tl[st].z}
					q = q + 1
				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 , s=ol[so].s, e=ol[so].e,z=ol[so].z }
					q = q + 1
				end
			end
		end
		show_group=sr
		from_menu=2
		option_catlin=1
		option_catopt=1
		option_srch=1
		cur_state=12
	end
			
	if cur_state==14 then
		start_drawing()
		update_stars()
		IMG_PSP6:activate()
		IMG_PSP6:draweasy(0,5)
		pge.gfx.drawrect(180,5,295,262,COL_ALBL)
		pge.gfx.drawrect(5,237,170,30,COL_ALBL)
		pge.gfx.drawrect(5,202,170,30,COL_ALBL)
		drawcursor(5,237,170,30,1)
		FON_ARIA:activate()
		FON_ARIA:print(185,10,COL_WHIT,show_group[cur_selected].n)
		FON_ARIA:print(182,30,COL_WHIT,wordwrap(show_group[cur_selected].d,FON_ARIA,285))
		sizeoffile=pge.math.floor(show_group[cur_selected].z / 1024)
		FON_ARIA:print(185,160,COL_WHIT,"This download is "..sizeoffile.." KB ("..show_group[cur_selected].z.." bytes)")
		if from_menu==1 then
			FON_ARIA:print(182,182,COL_WHIT,wordwrap("No progress bar will be used for featured downloads.",FON_ARIA,285))
		elseif from_menu==2 then
			FON_ARIA:print(182,182,COL_WHIT,wordwrap("No progress bar will be used for searched item downloads.",FON_ARIA,285))	
		end
		FON_ARIA:print(64,247,COL_WHIT,"Download")
		FON_ARIA:print(56,212,COL_WHIT,"Screenshots")
		FON_ARIA:print(182,222,COL_WHIT,wordwrap("MeqSoft will not take responsibility for files downloaded but will try to prevent issues where possible.",FON_ARIA,285))
		if pge.controls.pressed(PGE_CTRL_CROSS) then
			SND_ADVA:play()
			if online==1 then
				if from_menu==1 or from_menu==2 then
					cur_state=17
				else
					if settin_dltyp==1 then
						cur_state=15
					elseif settin_dltyp==2 then
						cur_state=17
					end
				end
			end
		end
		if pge.controls.pressed(PGE_CTRL_CIRCLE) then
			SND_BACK:play()
			if from_menu==1 or from_menu==2 then
				cur_state=4
			else
				cur_state=12
			end
		end
		finis_drawing()
	end

	if cur_state==15 then
		start_drawing()
		IMG_PSP6:activate()
		IMG_PSP6:draweasy(0,5)
		pge.gfx.drawrect(180,5,295,262,COL_ALBL)
		pge.gfx.drawrect(5,97,170,170,COL_ALBL)
		pge.gfx.drawrectoutline(183,250,290,10,COL_WHIT)
		FON_ARIA:activate()
		FON_ARIA:print(185,10,COL_WHIT,"Starting download...")
		FON_ARIA:print(5,102,COL_WHIT,wordwrap("MeqSoft will not take responsibility for files downloaded but will try to prevent issues where possible.",FON_ARIA,160))
		finis_drawing()
		if repo_type==1 then 
			if show_group == gl then
				file_dir = "/~redston2/pspinstaller.co.uk/sig/files/approved/g/f"..show_group[cur_selected].i.."/package.zip"
			elseif show_group == al then
				file_dir = "/~redston2/pspinstaller.co.uk/sig/files/approved/a/f"..show_group[cur_selected].i.."/package.zip"
			elseif show_group == tl then
				file_dir = "/~redston2/pspinstaller.co.uk/sig/files/approved/t/f"..show_group[cur_selected].i.."/package.zip"
			elseif show_group == ol then
				file_dir = "/~redston2/pspinstaller.co.uk/sig/files/approved/o/f"..show_group[cur_selected].i.."/package.zip"
			end
		elseif repo_type==2 then
			if show_group == gl then
				file_dir = "/~redston2/pspinstaller.co.uk/unsig/files/approved/g/f"..show_group[cur_selected].i.."/package.zip"
			elseif show_group == al then
				file_dir = "/~redston2/pspinstaller.co.uk/unsig/files/approved/a/f"..show_group[cur_selected].i.."/package.zip"
			elseif show_group == tl then
				file_dir = "/~redston2/pspinstaller.co.uk/unsig/files/approved/t/f"..show_group[cur_selected].i.."/package.zip"
			elseif show_group == ol then
				file_dir = "/~redston2/pspinstaller.co.uk/unsig/files/approved/o/f"..show_group[cur_selected].i.."/package.zip"
			end
		end
		down_stat = "Starting"
		down_serv = pge.net.resolve("pspinstaller.co.uk")
		down_port = 80
		down_file = file_dir
		down_save = show_group[cur_selected].f
		down_loca = pi_medi
		down_spee = 25000
		down_spec = 0
		down_timo = 10
		down_rect = 0
		down_recc = 0
		down_recp = 0
		cur_state=16	
		tpekb=0
	end	

	if cur_state==16 then
		start_drawing()
		if down_stat == "Starting" then
			if pge.file.exists(down_loca .. down_save) then
				pge.file.remove(down_loca .. down_save)
			end
	 
			socket = pge.net.socket.create()
			socketConnection = socket:connect(down_serv, down_port)
	 
			if not socketConnection then
				error("Failed to connect to the server.")
			end
	 
			request = "GET /" .. down_file .. " HTTP/1.1\r\nHost: " .. down_serv .. "\r\n\r\n"
			down_reqs = pge.net.socket.send(socket, request)
	 
			down_head = ""
			while not string.find(down_head, "\r\n\r\n") do
				down_head = down_head .. socket:receive(1)
			end
	 
			down_a, down_b, down_leng = string.find(down_head, "Content%-Length: (%d+)\r\n")
			down_a = nil
			down_b = nil
			down_rect = tonumber(down_leng)
	 		
			down_stat = "Downloading"
		elseif down_stat == "Downloading" then
			if down_safi == "" then
				down_safi = pge.file.open(down_loca .. down_save, PGE_FILE_CREATE + PGE_FILE_RDWR)
			end
	 
			down_recp = down_safi:size()
			down_safi:write(socket:receive(down_spee))
			down_recc = down_safi:size()
			down_spec = down_recc - down_recp
			pge.delay(1000)
 
			if down_recc == down_rect then
				down_safi:close()
				pge.delay(1000000)
				down_stat = "Finishing"
			end
		elseif down_stat == "Finishing" then
			down_safi = ""
			socket:close()
			down_stat = "Complete"
		elseif down_stat=="Complete" then
			cur_state=18
		end
		IMG_PSP6:activate()
		IMG_PSP6:draweasy(0,5)
		pge.gfx.drawrect(180,5,295,262,COL_ALBL)
		pge.gfx.drawrect(5,97,170,170,COL_ALBL)
		pge.gfx.drawrectoutline(183,250,288,13,COL_WHIT)
		if down_stat=="Downloading" and down_rect and down_recc then
			curpercent=284 / down_rect * down_recc
			actpercent=100 / down_rect * down_recc
			curkb = pge.math.ceil(down_recc/1024)
			totkb = pge.math.ceil(down_rect/1024)
			spekb = pge.math.ceil(down_spec/1024)
			tpekb = tpekb + spekb
			curdate = os.date()
			if oldtime ~= curdate then
				cursp = tpekb
				tpekb = 0
			end
			oldtime = curdate
			pge.gfx.drawrect(185,252,curpercent,10,COL_WHIT)
			FON_ARIA:activate()
			FON_ARIA:print(185,10,COL_WHIT,"Downloading "..show_group[cur_selected].n.."...")
			FON_ARIA:print(185,25,COL_WHIT,"This may take a few minutes.")
			FON_ARIA:print(5,102,COL_WHIT,wordwrap("MeqSoft will not take responsibility for files downloaded but will try to prevent issues where possible.",FON_ARIA,160))
			FON_ARIA:print(185,235,COL_WHIT,pge.math.ceil(actpercent).." Percent Complete")
			
			FON_ARIA:print(185,205,COL_WHIT,"Download Speed : "..cursp.." Kbp/s")
			FON_ARIA:print(185,220,COL_WHIT,"Downloaded "..curkb.."Kb of "..totkb.."Kb")
		end
		finis_drawing()
	end

	if cur_state==17 then
		start_drawing()
		IMG_PSP6:activate()
		IMG_PSP6:draweasy(0,5)
		pge.gfx.drawrect(180,5,295,262,COL_ALBL)
		pge.gfx.drawrect(5,97,170,170,COL_ALBL)
		FON_ARIA:activate()
		FON_ARIA:print(185,10,COL_WHIT,"Downloading "..show_group[cur_selected].n.."...")
		FON_ARIA:print(5,102,COL_WHIT,wordwrap("MeqSoft will not take responsibility for files downloaded but will try to prevent issues where possible.",FON_ARIA,160))
		FON_ARIA:print(185,25,COL_WHIT,"This may take a few minutes.")
		finis_drawing()
		if from_menu==1 then
			pge.net.getfile(directory_to_repo..show_group[cur_selected].f,pi_medi..show_group[cur_selected].f)
		elseif from_menu==2 then
			pge.net.getfile(directory_to_repo.."download.php?id="..show_group[cur_selected].i,pi_medi..show_group[cur_selected].f)
		else
			pge.net.getfile(directory_to_repo.."download.php?id="..show_group[cur_selected].i,pi_medi..show_group[cur_selected].f)
		end
		cur_state=18
	end


	if cur_state==18 then
		start_drawing()
		IMG_PSP6:activate()
		IMG_PSP6:draweasy(0,5)
		pge.gfx.drawrect(180,5,295,262,COL_ALBL)
		pge.gfx.drawrect(5,97,170,170,COL_ALBL)
		FON_ARIA:activate()
		FON_ARIA:print(185,10,COL_WHIT,"Installing "..show_group[cur_selected].n.."...")
		FON_ARIA:print(185,25,COL_WHIT,"This may take a few minutes.")
		FON_ARIA:print(5,102,COL_WHIT,wordwrap("MeqSoft will not take responsibility for files downloaded but will try to prevent issues where possible.",FON_ARIA,160))
		finis_drawing()
		currentwd = pge.dir.getcwd()
		myzipfile = pge.zip.open(pi_medi..show_group[cur_selected].f)
		pge.dir.chdir(pi_medi)
		if myzipfile then
			myzipfile:extract()
			pge.zip.close(myzipfile)
			myzipfile = nil
			error_msg="Installed "..show_group[cur_selected].n.."!"
			cur_state=20
			SND_COMP:play()
		else
			error_msg="There was an error processing the file."
			cur_state=20
		end
		pge.dir.chdir(currentwd)
	end

	if cur_state==20 then
		start_drawing()
		update_stars()
		IMG_PSP6:activate()
		IMG_PSP6:draweasy(0,5)
		pge.gfx.drawrect(180,5,295,262,COL_ALBL)
		pge.gfx.drawrect(5,97,170,170,COL_ALBL)
		FON_ARIA:activate()
		FON_ARIA:print(5,102,COL_WHIT,wordwrap("Thank you for using PSPInstaller to download your homebrew, themes and utilities!",FON_ARIA,160))
		FON_ARIA:print(185,10,COL_WHIT,error_msg)
		FON_ARIA:print(185,25,COL_WHIT,"Press (X) to return or (O) to exit.")
		finis_drawing()
		if pge.controls.pressed(PGE_CTRL_CROSS) then
			SND_BACK:play()
			cur_state=14
		end
		if pge.controls.pressed(PGE_CTRL_CIRCLE) then
			SND_BACK:play()
			for k=0,255,5 do
				start_drawing()
				update_stars()
				IMG_PSP6:activate()
				IMG_PSP6:draweasy(0,5)
				pge.gfx.drawrect(180,5,295,262,COL_ALBL)
				pge.gfx.drawrect(5,97,170,170,COL_ALBL)
				FON_ARIA:activate()
				FON_ARIA:print(5,102,COL_WHIT,wordwrap("Thank you for using PSPInstaller to download your homebrew, themes and utilities!",FON_ARIA,160))
				FON_ARIA:print(185,10,COL_WHIT,error_msg)
				FON_ARIA:print(185,25,COL_WHIT,"Press (X) to return or (O) to exit.")
				pge.gfx.drawrect(0,0,480,272,pge.gfx.createcolor(0,0,0,k))
				finis_drawing()
			end
			pge.exit()
		end
	end

	if cur_state==60 then
		start_drawing()
update_stars()
IMG_PSP6:activate()
IMG_PSP6:draweasy(0,5)
pge.gfx.drawrect(180,5,295,262,COL_ALBL)
FON_ARIA:activate()
if online==0 then
	cur_state=5	
else
	if fea_check==0 then
		drawcursor(5,237,170,30,1)
		FON_ARIA:activate()
		FON_ARIA:print(182,15,COL_WHIT,wordwrap("A required file (FEA.LUA) appears to be missing! Please restart PSPInstaller and ensure that all required files are downloaded! Press (X) or (O) to return to about.", FON_ARIA, 285))
		FON_ARIA:print(78,247,COL_WHIT,"Back")
		if pge.controls.pressed(PGE_CTRL_CROSS) then
			SND_BACK:play()
			cur_state=5
		end
	else
		if thisversion==currversion then
			FON_ARIA:activate()
			FON_ARIA:print(182,10,COL_WHIT,wordwrap("There is no new release of PSPInstaller available at the moment.",FON_ARIA,285))
			FON_ARIA:print(185,50,COL_WHIT,"You have version: "..thisversion)
			FON_ARIA:print(185,75,COL_WHIT,"Release Notes:")
			FON_ARIA:print(182,95,COL_WHIT,wordwrap(thisversionde,FON_ARIA,285))
		else
			drawcursor(5,237,170,30,1)
			FON_ARIA:activate()
			FON_ARIA:print(182,10,COL_WHIT,wordwrap("There is a new release of PSPInstaller available!",FON_ARIA,285))
			FON_ARIA:print(185,40,COL_WHIT,"You have version: "..thisversion)
			FON_ARIA:print(185,55,COL_WHIT,"This version is available: "..currversion)
			FON_ARIA:print(185,80,COL_WHIT,"Release Notes:")
			FON_ARIA:print(182,95,COL_WHIT,wordwrap(currversionde,FON_ARIA,285))
			FON_ARIA:print(182,205,COL_WHIT,wordwrap("Press (X) to update or (O) to return to go back.",FON_ARIA,285))
			FON_ARIA:print(70,247,COL_WHIT,"Update")
			if pge.controls.pressed(PGE_CTRL_CROSS) then
				SND_ADVA:play()
				cur_state=61
			end
		end
	end
end
if pge.controls.pressed(PGE_CTRL_CIRCLE) then
	SND_BACK:play()
	cur_state=5
end
finis_drawing()
	end

	if cur_state==61 then
		start_drawing()
		IMG_PSP6:activate()
		IMG_PSP6:draweasy(0,5)
		pge.gfx.drawrect(180,5,295,262,COL_ALBL)
		FON_ARIA:activate()
		FON_ARIA:print(185,10,COL_WHIT,"PSPInstaller is updating...")
		FON_ARIA:print(185,30,COL_WHIT,"Your PSP will return to XMB when finished!")
		finis_drawing()
		pge.delay(1000000)
		for k=1,#upd do
			if pge.file.exists(upd[k].f) then
				pge.file.remove(upd[k].f)
			end
			pge.net.getfile(updateloc..upd[k].s,upd[k].f)
		end
		SND_COMP:play()
		for k=0,255,5 do
			start_drawing()
			IMG_PSP6:activate()
			IMG_PSP6:draweasy(0,5)
			pge.gfx.drawrect(180,5,295,262,COL_ALBL)
			FON_ARIA:activate()
			FON_ARIA:print(185,10,COL_WHIT,"PSPInstaller is updating...")
			FON_ARIA:print(185,30,COL_WHIT,"Your PSP will return to XMB when finished!")
			pge.gfx.drawrect(0,0,480,272,pge.gfx.createcolor(0,0,0,k))
			finis_drawing()
		end
		pge.exit()
	end

	if cur_state==7 then
		for k=0,255,5 do
			start_drawing()
			update_stars()
			IMG_PSP6:activate()
			IMG_PSP6:draweasy(0,5)
			pge.gfx.drawrect(5,97,170,30,COL_ALBL)
			pge.gfx.drawrect(5,132,170,30,COL_ALBL)
			pge.gfx.drawrect(5,167,170,30,COL_ALBL)
			pge.gfx.drawrect(5,202,170,30,COL_ALBL)
			pge.gfx.drawrect(5,237,170,30,COL_ALBL)
			IMG_ADV1:activate()
			IMG_ADV1:draweasy(180,5,0,100)
			IMG_ADV2:activate()
			IMG_ADV2:draweasy(345,5,0,100)
			IMG_ADV3:activate()
			IMG_ADV3:draweasy(345,139,0,100)
			drawcursor(5,237,170,30,1)
			FON_ARIA:activate()
			FON_ARIA:print(68,107,COL_WHIT,"Signed")
			FON_ARIA:print(63,142,COL_WHIT,"Unsigned")
			FON_ARIA:print(65,177,COL_WHIT,"Options")
			FON_ARIA:print(71,212,COL_WHIT,"About")
			FON_ARIA:print(78,247,COL_WHIT,"Exit")
			pge.gfx.drawrect(0,0,480,272,pge.gfx.createcolor(0,0,0,k))
			finis_drawing()
		end
		pge.exit()
	end
end