--[[
	UO_OGRANISER:
	'uo' (unOfficial) Version by Hoshizora [http://hoshizora.tripod.com/uo_ograniser]
	Original Concept and Program by modsyn [http://cs.ulm.edu/~jself]

	Uses LUA Player: 0.20 MOD 2 by Cools [http://www.theriddlingsphinx.com/]
	Last updated   : 2006.10.10
  ]]

----------------------
-- Import Libraries --
----------------------
require('lib/util')

-----------------------------
-- Global vars (Constants) --
-----------------------------
-- Hoshizora 2006.07.17: Program & debug info
progname='uo_Ograniser'
progver ='0.9.2se'
debug   =false

-- Activate USB mode (for development stage use only)
--System.usbDiskModeActivate()

-- Hoshizora 2006.07.17: Screen titles
list1_0title ='FW1.0 Homebrew'
list1_5title ='FW1.5 Homebrew'
organisetitle='   Organise   '
reporttitle  ='Summary Report'
errcodetitle =' Error Codes  '
quittitle    ='     Quit     '

-- Hoshizora 2006.07.08: File/folder locations. Note: All dir paths must include trailing forwardslash
device        ='ms0:/'
pspdir        ='PSP/'
pspgamedir    ='GAME/'
pspgame150dir ='GAME150/'
gamedir       =nil
imagesdir     ='Images/'
progextdir    ='uo_Ogr/'
scrsavedir    ='ScrnCaps/'
logfile       ='uo_Ogr.log'
customsavefile='Custom.dat'

-- Hoshizora 2006.07.17: Screen info used mainly by print command
fontdim     =8   -- Font dimension (8x8). Usable space is only (7x7)
screenwidth =480 -- Can accomodate 59 characters per line (screenwidth/fontdim)-1 based on fontdim (8x8)
screenheight=272 -- Can accomodate 33 lines (screenheight/fontdim)-1 based on fontdim (8x8)
maxtitlelen =39  -- Used by pad_right and pad_left for report screen
red   =Color.new(255,0,0)
ltred =Color.new(255,210,210)
green =Color.new(54,255,87)
blue  =Color.new(30,30,230)
ltblue=Color.new(210,255,255)
yellow=Color.new(255,255,0)
grey  =Color.new(154,154,154)
black =Color.new(0,0,0)
white =Color.new(255,255,255)

-- Hoshizora 2006.07.08: Messages for the status bar
msgSplashProgInfo1    ='\'uo\' (unOfficial) Version by Hoshizora'
msgSplashProgInfo2    ='Original Concept and Program by modsyn'
msg1_0HomebrewsInTotal=' title(s) for firmware 1.0 in total'
msg1_5HomebrewsInTotal=' KXploit title(s) for firmware 1.5 in total'
msgManualSort         ='Move the selection using D-pad or analog stick'
msgAutoSort           ='The FW1.5 Homebrew list sorted in '
msgRestoreCustomList  ='The custom FW1.5 list order has been restored'
msgEditDisabled       ='The FW1.0 Homebrew list cannot be edited'
msgIsLowBattery       ='Battery power is low. Please recharge and try again'
msgScreenCaptured     ='Screen capture saved to '
msgScrSaveError       ='An error has occurred when saving the captured file'
msgDispHideOption     ='Hide KXploit shortcut folders in list'
msgDispShowOption     ='Display KXploit shortcut folders in list'
msgIconRepairSCEOption='Hide corrupted icons via __SCE & %__SCE name scheme'
msgIconRepairIRSOption='Hide corrupted icons via iR Shell compatible scheme'
msgIconRestoreOption  ='Show corrupted icons via FOLDER & FOLDER% name scheme'
msgIconNoChangeOption ='Folder names will not be modified during organising'
msgItemRepairToggle   ='Repair this title during organising'
msgItemIgnoreToggle   ='Do not repair this title during organising'
msgAllRepairToggle    ='Allow changes to all folder names during organising'
msgAllIgnoreToggle    ='Deny changes to all folder names during organising'
msgItemRepairOption   ='Allow change to this folder name during organising'
msgItemIgnoreOption   ='Deny change to this folder name during organising'
msgReportScreen       ='Press     to return to Splash Screen or    to Quit'
msgErrorCodeScreen    ='Press   to return to Summary Report or    to Quit'
msgViewErrorCode      ='For help in Error Codes, press'
msgYNCommitChanges    ='Commit changes made to the list order?'
msgYNQuitOgraniser    ='Do you wish to quit uo_Ograniser?'
msgYNCQuitOgraniser   ='Do you wish to save the list order?'

-- Pad controls
analogDeadZone=32 -- Sensitivity of the analog D-pad

--[[	
  Supported folder naming scheme/convention:
	1. foldername
	   foldername%
	2. __sce__foldername
	   %__sce__foldername
	3. folde                          1
	   folde1~1%
	4. folde                          _
	   folde_~1%
	5. foldername_____________________1
	   folder~1%
	6. foldername                     1
	   folder~1%
	7. foldername                     _
	   folder~1%
	8. foldername______________________
	   folder~1%
	NB: Folder name schemes 2-8 can hide corrupted data icons in XMB menu
	]]
minProgramDirSchemeLen=32
minShortcutDirSchemeLen=9
maxSCEFolderNameLen=30
maxFPFFolderNameLen=30

-- Hoshizora 2006.07.17: Program images
os              =Image.createEmpty(screenwidth,screenheight)
splashbg        =Image.load(imagesdir..'bg-splash.png')
organisebg      =nil
contextbg       =nil
padup           =nil
paddown         =nil
padleft         =nil
padright        =nil
padtriangle     =nil
padsquare       =nil
padcircle       =nil
padcross        =nil
padl            =nil
padr            =nil
padselect       =nil
padstart        =nil
analogleft      =nil
iconbattery     =nil
iconlistlocked  =nil
iconlistunlocked=nil
iconitemnoedit  =nil
iconitemrepair  =nil
iconitemrepair0 =nil
iconitemignore  =nil
iconitemignore0 =nil
iconbarstatus   =nil
iconbarhelp     =nil
iconbarprogress =nil
textascending   =nil
textdescending  =nil
textformatted   =nil
textoriginal    =nil
text__sce       =nil
textirshell     =nil

-------------------
-- Main Function --
-------------------
-- Hoshizora 2006.07.15: Main program function
function main()
	local firstrun=true
	local prog1_5={}
	local list1_5={}
	local clist1_5={}
	local list1_0={}
	local sel,repair,max

	while true do
		if firstrun then
			osPreSplashScreen()
			if dirExists(device,progextdir)==0 then dirCreate(device,progextdir) end
			load_images()
			gamedir=getGameDir()
			firstrun=false
		end

		getGameList(prog1_5,list1_5,clist1_5,list1_0)
		osSplashScreen()

		sel,repair=choose(list1_5title,list1_5,clist1_5,list1_0)
		if sel then
			-- Hoshizora 2006.08.11: Remove log file from previous run (if exist)
			delLogFile()
			osClear()
			max=table.getn(sel)
			for i=1,max do
				-- Hoshizora 2006.07.07: Modified touch_icon function to include repair mode, current and max queue counter
				-- Hoshizora 2006.08.06: Modified touch_icon function to return the modified shortcutdir for the saveCustomList() function
				sel[max-i+1].name=touch_shortcut(prog1_5,sel[max-i+1],repair,i,max)
			end
			saveCustomList(sel)
			osClear()
			osReportScreen()
		end
	end
end

------------------
-- Main Program --
------------------
main()