-- (c) 2007 Nymbia.  see LGPLv2.1.txt for full details.
local key = {"1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}
key[0] = "0"
local function to36(data)
	local pre = ""
	if data:sub(1,1) == "-" then
		pre = "-"
		data = data:sub(2)
	end
	if not tonumber(data) then
		return data
	end
	local working = tonumber(data)
	local newstr = ""
	if working == 0 then
		newstr = "0"
	end
	while working > 0 do
		newstr = key[working%36]..newstr
		working = math.floor(working / 36)
	end
	return pre..newstr
end
local function squish(data)
	if data:sub(1,2) == "m," or data:sub(1,2) == "u," then
		return data
	elseif data:sub(1,2) == "o," then
		local newdata = "o,"
		for itemstring in data:sub(3):gmatch("([^,]+)") do
			local id, value = itemstring:match("^([^:]+):(.+)$")
			if id then
				newdata = newdata..to36(id)..":"..value..","
			else
				newdata = newdata..to36(itemstring)..","
			end
		end
		return newdata:sub(1,-2)
	elseif data:sub(1,2) == "c[" then
		local newdata = "c"
		for setstring in data:gmatch("(%b[])") do
			local childstring,rest = setstring:sub(2,-2):match("([^,]+,)(.+)$")
			newdata = newdata.."["..childstring..squish(rest).."]"
		end
		return newdata
	else
		local newdata = ""
		for itemstring in data:gmatch("([^,]+)") do
			local id, value = itemstring:match("^([^:]+):(.+)$")
			if id then
				newdata = newdata..to36(id)..":"..to36(value)..","
			else
				newdata = newdata..to36(itemstring)..","
			end
		end
		return newdata:sub(1,-2)
	end
end
local sets = {}
local function update(filename)
	print("updating file:", filename)
	local f = assert(io.open(filename, "r"))
	local file = f:read("*all")
	f:close()
	local f = assert(io.open(filename, "w"))
	for line in string.gmatch(file, '([^\n]-\n)') do
		local setname, data = string.match(line, '\t%[%"([^\n^"]+)%"%][^\n]-=[^\n]-"([^\n]-)",')
		if setname and sets[setname] then
			f:write('\t["'..setname..'"]="'..sets[setname]..'",\n')
		else
			f:write(line)
		end
	end
	f:close()
end
local f = assert(io.open("data.lua", "r"))
local file = f:read("*all")
f:close()
local f = assert(io.open("datacompressed.lua", "w"))
f:write("--THIS FILE SHOULD -NEVER- BE EDITED DIRECTLY.  SEE THE WIKI PAGE ON MAKING CHANGES TO THESE FILES.\n")
for line in string.gmatch(file, '([^\n]-\n)') do
	local setname, data = string.match(line, '\t%[%"([^\n^"]+)%"%][^\n]-=[^\n]-"([^\n]-)",')
	if data then
		data = squish(data)
		f:write('\t["'..setname..'"]="'..data..'",\n')
		sets[setname] = data
	else
		f:write(line)
	end
end
f:close()

update("./PeriodicTable-3.0-Consumable/PeriodicTable-3.0-Consumable.lua")
update("./PeriodicTable-3.0-InstanceLoot/PeriodicTable-3.0-InstanceLoot.lua")
update("./PeriodicTable-3.0-InstanceLootHeroic/PeriodicTable-3.0-InstanceLootHeroic.lua")
update("./PeriodicTable-3.0-Gear/PeriodicTable-3.0-Gear.lua")
update("./PeriodicTable-3.0-GearSet/PeriodicTable-3.0-GearSet.lua")
update("./PeriodicTable-3.0-Misc/PeriodicTable-3.0-Misc.lua")
update("./PeriodicTable-3.0-Reputation/PeriodicTable-3.0-Reputation.lua")
update("./PeriodicTable-3.0-Tradeskill/PeriodicTable-3.0-Tradeskill.lua")
update("./PeriodicTable-3.0-TradeskillResultMats/PeriodicTable-3.0-TradeskillResultMats.lua")
