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)
black = Color.new(0,0,0)
bg = Image.load("bg.jpg")
msgcolor = white
current = 1
usb = 0
anim = 0
animctrl = 1

function printCentered(y,text,color)
     length = string.len(text)
     x = 240 - ((length*8)/2)
     screen:print(x,y,text,color)
end

function printRight(y,text,color)
     length = string.len(text)
	 x = 480 - (length*8)
	 screen:print(x,y,text,color)
end

function printLeft(y,text,color)
	 screen:print(0,y,text,color)
end

function drawSelector(y,fill)
	 if fill == 1 then
	 x = 40 + anim
     w = 400 - (anim*2)
	 y = y - 1
	 animctrl = 1
	 g = 255
	 if x > 0 then anim = anim - 2 end
	 else
	 if animctrl == 1 and anim == 20 then animctrl = 2 end
	 if animctrl == 2 and anim == 0 then animctrl = 1 end
	 if animctrl == 1 then anim = anim + 2 end
	 if animctrl == 2 then anim = anim - 2 end
	 x = 40 + anim
     w = 400 - (anim*2)
     y = y - 1
	 g = 255 - (anim*8)
	 end
	 color = Color.new(0,g,0)
     selector = Image.createEmpty(w,10)
     selector:clear(color)
	 screen:blit(x,y,selector)
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)

-- Load theme count
file = io.open("themecount.txt", "r")
if file then
theme = file:read("*n")
else
theme = 1
file = io.open("themecount.txt", "w")
file:write("1")
end
file:close()

-- Load cycle mode
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()

-- 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()

while true do
date = os.date()

bat = System.powerGetBatteryLifePercent()
if bat < 15 then batcolor,batmsg = red,"-Charge before flashing-" else batcolor,batmsg = green,"" end
if (System.powerIsBatteryCharging()) then batcolor,batmsg = green,"-Charging-" end

screen:clear()
screen:blit(0,0,bg)
printLeft(0,"XMB Theme Cycler V0.3",red)
printLeft(10,"Battery : "..bat.."%".." "..batmsg,batcolor)
printRight(0,date,red)
printLeft(262,"Next theme to flash : "..theme,red)
if usb == 1 then printLeft(252,"USB Enabled",green) else printLeft(252,"USB Disabled",red) end

if current == 1 then drawSelector(40) end
if cyclemode == "normal" then cyclemodetxt = "Normal" end
if cyclemode == "monthly" then cyclemodetxt = "Monthly" end
if cyclemode == "disable" then cyclemodetxt = "Disabled" end
printCentered(40,"Cycle mode : "..cyclemodetxt,blue)

if current == 2 then drawSelector(50) end 
if (theme+1) == max then nextthemetxt = 1 else nextthemetxt = theme + 1 end
printCentered(50,"Skip to theme "..nextthemetxt,blue)

if current == 3 then drawSelector(60) end 
if showprogress == 1 then showprogresstxt = "Yes" else showprogresstxt = "No" end
printCentered(60,"Show flashing progress : "..showprogresstxt,blue)

if current == 4 then drawSelector(70) end
printCentered(70,"Flash saved theme",blue)

if current == 5 then drawSelector(80) end 
if usb == 1 then usbtxt = "Disable USB" else usbtxt = "Enable USB" end
printCentered(80,usbtxt,blue)

if current == 6 then drawSelector(90,1) end 
printCentered(90,"Exit",blue)
printLeft(90,"Press O to exit to XMB --",black)
printRight(90,"-- Press X to cycle theme",black)

pad = Controls.read()

-- Actions
if current == 6 and pad:cross() then -- Exit and cycle theme
System.usbDiskModeDeactivate()
break
end

if current == 6 and pad:circle() then -- Exit and return to xmb
System.usbDiskModeDeactivate()
System.Quit()
end

if current == 1 and pad:cross() then -- Set cycling mode
if cyclemode == "normal" then cyclemode = "monthly" else
if cyclemode == "monthly" then cyclemode = "disable" else
if cyclemode == "disable" then cyclemode = "normal" 
end
end
end
printRight(262,"Saving Configuration...",red)
screen.flip()
file = io.open("ms0:/themecycler/CYCLEMODE.cfg", "w")
file:write(cyclemode)
file:close()
screen.waitVblankStart(60)
end

if current == 2 and pad:cross() then -- Skip theme
theme = theme + 1
if theme == 4 then theme = 1 end
printRight(262,"Saving Configuration...",red)
screen.flip()
file = io.open("themecount.txt", "w")
file:write(theme)
file:close()
screen.waitVblankStart(60)
end

if current == 3 and pad:cross() then -- Set show progress
showprogress = 1 - showprogress
printRight(262,"Saving Configuration...",red)
screen.flip()
file = io.open("showprogress.txt", "w")
file:write(showprogress)
file:close()
screen.waitVblankStart(60)
end

if current == 4 and pad:cross() then -- Flash saved theme
currdir = System.currentDirectory()
System.writeFile(currdir.."/backup/topmenu_plugin.rco","flash0:/vsh/resource/topmenu_plugin.rco","no")
System.writeFile(currdir.."/backup/opening_plugin.rco","flash0:/vsh/resource/opening_plugin.rco","no")
System.writeFile(currdir.."/backup/savedata_plugin.rco","flash0:/vsh/resource/savedata_plugin.rco","no")
System.writeFile(currdir.."/backup/gameboot.pmf","flash0:/vsh/resource/gameboot.pmf","no")
System.writeFile(currdir.."/backup/system_plugin.rco","flash0:/vsh/resource/system_plugin.rco","no")
System.writeFile(currdir.."/backup/01-12.bmp","flash0:/vsh/resource/01-12.bmp","no")
System.writeFile(currdir.."/backup/impose_plugin.rco","flash0:/vsh/resource/impose_plugin.rco","no")
System.writeFile(currdir.."/backup/premo_plugin.rco","flash0:/vsh/resource/premo_plugin.rco","no")
System.writeFile(currdir.."/backup/system_plugin_fg.rco","flash0:/vsh/resource/system_plugin_fg.rco","no")
System.writeFile(currdir.."/backup/system_plugin_bg.rco","flash0:/vsh/resource/system_plugin_bg.rco","no")
System.writeFile(currdir.."/backup/photo_plugin.rco","flash0:/vsh/resource/photo_plugin.rco","no")
System.writeFile(currdir.."/backup/game_plugin.rco","flash0:/vsh/resource/game_plugin.rco","no")
System.writeFile(currdir.."/backup/sysconf_plugin.rco","flash0:/vsh/resource/sysconf_plugin.rco","no")
System.writeFile(currdir.."/backup/ltn0.pgf","flash0:/font/ltn0.pgf","no")
file = io.open("ms0:/themecycler/CYCLEMODE.cfg", "w")
file:write("disable")
file:close()
screen:clear()
printCentered(130,"Save Theme Flashed.",red)
printCentered(140,"Theme Cycling Disabled.",red)
printCentered(150,"Returning to XMB...",red)
screen.flip()
screen.waitVblankStart(180)
System.Quit() 
end

if current == 5 and pad:cross() then --  Enable/Disable USB
usb = 1 - usb
if usb == 1 then System.usbDiskModeActivate() else System.usbDiskModeDeactivate() end
screen.waitVblankStart(60)
end

-- Control

	if pad:up() and oldpad:up() ~= pad:up() then
         current = current - 1
	end
	if pad:down() and oldpad:down() ~= pad:down() then
		 current = current + 1
	end

	if current >= 7 then
	     current = 1
	end
	if current <= 0 then
	     current = 6
	end
	
screen.waitVblankStart()
System.powerTick()
screen.flip()
oldpad = pad
end