local temp = io.open(os.getenv("HOME").."/.config/awesome/file.lua","r")
local filehelper = loadstring(temp:read("*a"))()
temp:close()
--get a giant list of xdg data
log = function(...)
	local params = {}
	for k,v in pairs({...}) do
		params[#params+1] = tostring(v)
	end
	local str = table.concat(params,"   ")
	filehelper.write("./awesome_log",filehelper.read("./awesome_log").."\n"..str)
end
local function parse_xdg()
	local output = {}
	local temp = io.popen("find /usr/share/applications","r")
	local file_table = temp:read("*a")
	temp:close()
	local temp = io.popen("find "..os.getenv("HOME").."/.local/share/applications","r")
	file_table = file_table.."\n"..temp:read("*a")
	temp:close()
	local lines = {}
	file_table:gsub("[^\n]+",function(capt) lines[#lines+1] = capt end)
	for k,v in pairs(lines) do
		local data = filehelper.read(v,"*a")
		--check if its an app, if it has a name, and if it's even readable.
		if data and data:match("Type=([^\n]+)") and data:match("Type=([^\n]+)") == "Application" and data:match("Name=([^\n]+)") then
			--get the tags
			local tags = data:match("Categories=([^\n]+)")
			local categories = {}
			if tags then
				tags:gsub("[^;]+",function(capt) categories[#categories+1] = capt:match("%w+") end)
			end
			--remove a bunch of useless category extensions
			while true do
				local occurences = 0
				local whitelist = {
					Network = true,
					Game = true,
					Education = true,
					Development = true,
					Graphics = true,
					Utility = true,
					System = true,
					AudioVideo = true,
					Office = true,
					Settings = true,
				}
				for k,v in pairs(categories) do
					if not whitelist[v] then
						table.remove(categories,k)
						occurences = occurences + 1
					end
				end
				if occurences == 0 then
					break
				end
			end
			--add this to the end so it'll be detected like a path.
			categories[#categories+1]=data:match("Name=([^\n]+)")
			output[#output+1] = {data:match("Exec=([^\n%%]+)"),categories = categories}
		end
	end
	return output
end

--fuck
local function sort_by_categories(input)
	local output = {}
	assert(type(input) == "table")
	for k,v in pairs(input) do
		local categories = v.categories
		local function sort(tab,list,value)
			local found = nil
			for k,v in pairs(tab) do
				if v[1] == list[1] then
					found = v
				end
			end
			if not found then
				if list[2] then
					tab[#tab+1] = {list[1],{}}
					found = tab[#tab]
				elseif list[1] then
					tab[#tab+1] = {list[1],value}
					found = tab[#tab]
				end
			end
			if list[2] then
				table.remove(list,1)
				found[2] = sort(found[2],list,value)
			end
			return tab
		end
		output = sort(output,categories,v[1])
	end
	return output
end

local function sort_untagged(input)
	local output = input
	local other = {}
	for k,v in pairs(input) do
		log(k,v[1],v[2])
		if type(v[2]) == "string" then
			other[#other+1] = {v[1],v[2]}
			output[k] = nil
		end
	end
	local new_output = {}
	for k,v in pairs(output) do
		new_output[#new_output+1] = v
	end
	new_output[#new_output+1] = {"Other",other}
	return new_output
end

return sort_untagged(sort_by_categories(parse_xdg()))
