# signature { license=0, seed=277031123, key="9daf94484c95a36e82d969ff7d7725c4" }





local lfs = require( "lfs" )
local set ={}

local function scan( dir, pattern)
	for filename in lfs.dir( dir..pattern ) do

		local file = io.open( dir..filename )
		if file then
			local text = file:read("*a")
			file:close()

			print("processing "..filename)
			for _, key in text:gmatch( "i18n%s*[(]%s*([\"\'])(.-)%1" ) do
				set[key] = key
			end
		end
	end
end

scan( '',       '*.lua' )
scan( 'html/', '*.cgi' )
scan( 'html/', '*.js' )
scan( '',       'default.cfg' )

cgi.header( "Content-Disposition", ("attachment; filename=i18n-%s-%s.txt"):format( i18n("iso639_language","en"), i18n("iso639_country","UK") ) );
cgi.header( "Content-Type",'plain/text; charset="UTF-8"');


cgi.print()

for row in io.lines( "i18n.txt" ) do
	local line = row:gsub('\r','')
	local key = line:match('%["([^"]*)"%]')
	if key then
		if set[key] then
			set[key] = nil
		else
			line = line .. "-\45 unused !"
		end
	else
		if line:find("^}") then

			local sorted = {}
			for k in pairs( set ) do table.insert( sorted, k ) end

			cgi.printf("-\45\r\n-\45 new texts\r\n-\45\r\n")
			table.sort( sorted )
			for _, k in ipairs( sorted ) do
				cgi.printf( '["%s"]=\r\n[[%s]];\r\n\r\n' , k, set[ k ]:gsub('\\n','\r\n') )
			end
		end
	end
	cgi.printf("%s\r\n", line)
end
