require "hook"

session={}

local path = getpref(TYPE_STRING, "kmeleon.general.settingsDir")
local fname=path.."session.1"

local function SaveSession()
	local i=1
	pluginmsg("utils","write",fname,"")
	while (i<=#session) do
		pluginmsg("utils","append",fname,session[i].."\n")
		i=i+1
	end
end

local function OnCreateTab()
	local i=getpref(TYPE_INT,"kmeleon.session.number")
	if i==0 then
		setpref(TYPE_INT,"kmeleon.session.number",1)
		return
	end
	local title, url = GetDocInfo()
	table.insert(session,1,url)
	SaveSession()
end
hook.add(CreateTabHook, OnCreateTab)

function OpenSession()
	local urls=pluginmsgex("utils","read",fname,TYPE_STRING)
	if urls then
		local len=string.len(urls)
		local i=1
		while (i<len) do
			local p=string.find(urls,"\n")
			local temp=string.sub(urls,1,p-1)
			NavigateTo(temp,OPEN_NEW)
			urls=string.sub(urls,p+1)
			i=i+p
		end
	end
end



