---- LUA XMB Theme Cycler v0.3
---- Author: LuizLizard
-----------------------------------------------
---- Some code grabbed from SG57 flash example.

black = Color.new(0,0,0)
green = Color.new(0,255,0)
magenta = Color.new(255,0,255)
white = Color.new(255,255,255)
red = Color.new(255,0,0)
blue = Color.new(0,0,255)
yellow = Color.new(255,255,0)
msgcolor = white
control = 0

-- Unassign flash0
System.Unassign("flash0:")
-- Reassign flash0 with writing rights
System.Assign("flash0:", "lflash0:0,0", "flashfat0:")

-- Draw text function
function drawText(text)
screen:clear()
screen:print(10,10,text,msgcolor)
screen.flip()
end

-- Backup current theme
file = io.open("backup.txt", "r")
if file then file:close() else
-- Create backup
drawText("Creating backup...")
currdir = System.currentDirectory()
System.createDirectory(currdir.."/backup")
System.writeFile("flash0:/vsh/resource/topmenu_plugin.rco",currdir.."/backup/topmenu_plugin.rco","no")
System.writeFile("flash0:/vsh/resource/opening_plugin.rco",currdir.."/backup/opening_plugin.rco","no")
System.writeFile("flash0:/vsh/resource/savedata_plugin.rco",currdir.."/backup/savedata_plugin.rco","no")
System.writeFile("flash0:/vsh/resource/gameboot.pmf",currdir.."/backup/gameboot.pmf","no")
System.writeFile("flash0:/vsh/resource/system_plugin.rco",currdir.."/backup/system_plugin.rco","no")
System.writeFile("flash0:/vsh/resource/01-12.bmp",currdir.."/backup/01-12.bmp","no")
System.writeFile("flash0:/vsh/resource/impose_plugin.rco",currdir.."/backup/impose_plugin.rco","no")
System.writeFile("flash0:/vsh/resource/premo_plugin.rco",currdir.."/backup/premo_plugin.rco","no")
System.writeFile("flash0:/vsh/resource/system_plugin_fg.rco",currdir.."/backup/system_plugin_fg.rco","no")
System.writeFile("flash0:/vsh/resource/system_plugin_bg.rco",currdir.."/backup/system_plugin_bg.rco","no")
System.writeFile("flash0:/vsh/resource/photo_plugin.rco",currdir.."/backup/photo_plugin.rco","no")
System.writeFile("flash0:/vsh/resource/game_plugin.rco",currdir.."/backup/game_plugin.rco","no")
System.writeFile("flash0:/vsh/resource/sysconf_plugin.rco",currdir.."/backup/sysconf_plugin.rco","no")
System.writeFile("flash0:/font/ltn0.pgf",currdir.."/backup/ltn0.pgf","no")
file = io.open("backup.txt", "w")
file:write("1")
file:close()
screen:clear()
end

-- Skip routine
pad = Controls.read()
if pad:circle() then
drawText("Entering configuration menu...")
screen.waitVblankStart(120) 
dofile("config.lua")
screen:clear()
screen.flip()
end
if pad:l() then
drawText("Theme cycling cancelled, going to XMB...")
screen.waitVblankStart(120) 
System.Quit()
end

-- Load show progress setting
file = io.open("showprogress.txt", "r")
if file then
showprogress = file:read("*n")
else
showprogress = 1
file = io.open("showprogress.txt", "w")
file:write(showprogress)
end
file:close()
if showprogress == 1 then msgcolor = white else msgcolor = black end

-- Replace unused font to free space in flash0
dirlist = System.listDirectory("flash0:/font")
currdir = System.currentDirectory()
dummyfont = currdir.."/dummyfont"
			
for index,file in ipairs(dirlist) do

if dirlist[index].name == "gb3s1518.bwfon" then
if dirlist[index].size ~= 69108 then
drawText("Flashing 'gb3s1518.bwfon' dummy font...")
System.writeFile("flash0:/font/gb3s1518.bwfon","ms0:/themecycler/gb3s1518.bwfon.backup","no")
System.writeFile(dummyfont,"flash0:/font/gb3s1518.bwfon","no")
screen.waitVblankStart(60) 
end
end

if dirlist[index].name == "jpn0.pgf" then
if dirlist[index].size ~= 69108 then
drawText("Flashing 'jpn0.pgf' dummy font...")
System.writeFile("flash0:/font/jpn0.pgf","ms0:/themecycler/jpn0.pgf.backup","no")
System.writeFile(dummyfont,"flash0:/font/jpn0.pgf","no")
screen.waitVblankStart(60) 
end
end

end

-------- Cycling scheme
-- Load cycle mode configuration
file = io.open("ms0:/themecycler/CYCLEMODE.cfg", "r")
if file then
cyclemode = file:read(7)
else
cyclemode = "normal"
file = io.open("ms0:/themecycler/CYCLEMODE.cfg", "w")
file:write(cyclemode)
end
file:close()

-- Disabled (Disable cycling)
if cyclemode == "disable" then
drawText("Cycling disabled")
screen.waitVblankStart(60) 
System.Quit()
end

-- Normal mode (Change theme every startup)
if cyclemode == "normal" then
-- This mode just lets the program continue
end

-- Monthly cycle (Change theme each month)
if cyclemode == "monthly" then
date = os.date("*t")

file = io.open("MONTHCOUNT.txt", "r")
if file then
lastcount = file:read("*n")
file:close()
else
lastcount = 0
end

if date.month ~= lastcount then -- If count is different then flash theme
-- Save this month's number
file = io.open("MONTHCOUNT.txt", "w")
file:write(date.month)
file:close()
else
System.Quit() -- Return to XMB
end

end

--------

-- Load number of themes
max = 0
dirlist = System.listDirectory("ms0:/themecycler")
for index,file in ipairs(dirlist) do -- Count folders
if dirlist[index].directory then
max = max + 1
end
end
max = max - 1 --Discount the . and .. folders (2) and add 1 extra

-- Open and check wich theme to flash and increase count by 1 and reset to 0 if count = maximum
file = io.open("themecount.txt", "r")
if file then
control = file:read("*n")
else
control = 1
file = io.open("themecount.txt", "w")
file:write("1")
end
file:close()

theme = control
control = control + 1
if control >= max then
control = 1
end
file = io.open("themecount.txt", "w")
file:write(control)
file:close()
--

-- Flash specified theme
-- Set Phats
topmenu_plugin = "ms0:/themecycler/"..theme.."/topmenu_plugin.rco"
opening_plugin = "ms0:/themecycler/"..theme.."/opening_plugin.rco"
savedata_plugin = "ms0:/themecycler/"..theme.."/savedata_plugin.rco"
gameboot = "ms0:/themecycler/"..theme.."/gameboot.pmf"
system_plugin = "ms0:/themecycler/"..theme.."/system_plugin.rco"
background = "ms0:/themecycler/"..theme.."/01-12.bmp"
impose_plugin = "ms0:/themecycler/"..theme.."/impose_plugin.rco"
premo_plugin = "ms0:/themecycler/"..theme.."/premo_plugin.rco"
system_plugin_fg = "ms0:/themecycler/"..theme.."/system_plugin_fg.rco"
system_plugin_bg = "ms0:/themecycler/"..theme.."/system_plugin_bg.rco"
photo_plugin = "ms0:/themecycler/"..theme.."/photo_plugin.rco"
game_plugin = "ms0:/themecycler/"..theme.."/game_plugin.rco"
sysconf_plugin = "ms0:/themecycler/"..theme.."/sysconf_plugin.rco"
ltn0 = "ms0:/themecycler/"..theme.."/ltn0.pgf"
-- Start flashing

drawText("Flashing Theme:"..theme.." - 1/14")
file = io.open(topmenu_plugin, "r")
if file then
		System.writeFile(topmenu_plugin,"flash0:/vsh/resource/topmenu_plugin.rco","no")
file:close()
end

drawText("Flashing Theme:"..theme.." - 2/14")
file = io.open(opening_plugin, "r")
if file then
		System.writeFile(opening_plugin,"flash0:/vsh/resource/opening_plugin.rco","no")
file:close()
end

drawText("Flashing Theme:"..theme.." - 3/14")
file = io.open(savedata_plugin, "r")
if file then
		System.writeFile(savedata_plugin,"flash0:/vsh/resource/savedata_plugin.rco","no")
		file:close()
end

drawText("Flashing Theme:"..theme.." - 4/14")
file = io.open(gameboot, "r")
if file then		
		System.writeFile(gameboot,"flash0:/vsh/resource/gameboot.pmf","no")
		file:close()
end

drawText("Flashing Theme:"..theme.." - 5/14")
file = io.open(system_plugin, "r")
if file then		
		System.writeFile(system_plugin,"flash0:/vsh/resource/system_plugin.rco","no")
		file:close()
end

drawText("Flashing Theme:"..theme.." - 6/14")
file = io.open(background, "r")
if file then
		System.writeFile(background,"flash0:/vsh/resource/01-12.bmp","no")
		file:close()
end

drawText("Flashing Theme:"..theme.." - 7/14")
file = io.open(impose_plugin, "r")
if file then
		System.writeFile(impose_plugin,"flash0:/vsh/resource/impose_plugin.rco","no")
		file:close()
end

drawText("Flashing Theme:"..theme.." - 8/14")
file = io.open(premo_plugin, "r")
if file then
        System.writeFile(premo_plugin,"flash0:/vsh/resource/premo_plugin.rco","no")
		file:close()
end

drawText("Flashing Theme:"..theme.." - 9/14")
file = io.open(system_plugin_fg, "r")
if file then
		System.writeFile(system_plugin_fg,"flash0:/vsh/resource/system_plugin_fg.rco","no")
		file:close()
end

drawText("Flashing Theme:"..theme.." - 10/14")
file = io.open(system_plugin_bg, "r")
if file then
		System.writeFile(system_plugin_bg,"flash0:/vsh/resource/system_plugin_bg.rco","no")
		file:close()
end

drawText("Flashing Theme:"..theme.." - 11/14")
file = io.open(photo_plugin, "r")
if file then	
		System.writeFile(photo_plugin,"flash0:/vsh/resource/photo_plugin.rco","no")
		file:close()
end

drawText("Flashing Theme:"..theme.." - 12/14")
file = io.open(game_plugin, "r")
if file then
		System.writeFile(game_plugin,"flash0:/vsh/resource/game_plugin.rco","no")
		file:close()
end

drawText("Flashing Theme:"..theme.." - 13/14")
file = io.open(sysconf_plugin, "r")
if file then
		System.writeFile(sysconf_plugin,"flash0:/vsh/resource/sysconf_plugin.rco","no")
		file:close()
end

drawText("Flashing Theme:"..theme.." - 14/14")
file = io.open(ltn0, "r")
if file then
		System.writeFile(ltn0,"flash0:/font/ltn0.pgf","no")
		file:close()
end

-- End Program (Return to XMB)
System.Quit()
















