#!/usr/bin/python
#
# $Id: edelib-global.h 2785 2009-09-01 13:26:08Z karijes $
#
# Part of edelib tools.
# Copyright (c) 2011 Sanel Zukan
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library.  If not, see <http://www.gnu.org/licenses/>.

import sys
import os
import stat

def visit(arg, dirname, names):
	for name in names:
		if name[-4:] == ".xml" and name[:-4] == arg:
			print "%s/%s" % (dirname, name)

def mime_search(mime):
	dirs = []
	data_dirs = os.environ.get("XDG_DATA_DIRS", "/usr/local/share:/usr/share")
	data_home = os.environ.get("XDG_DATA_HOME")

	for i in data_dirs.split(":"):
		dirs.append("%s/mime" % i)

	if data_home:
		for i in data_home.split(":"):
			dirs.append("%s/mime" % i)

	for d in dirs:
		os.path.walk(d, visit, mime)

def main():
	if len(sys.argv) != 2:
		print("Usage: edelib-mime-find [mime-part]")
		print("Find the given mime part type and return full path")
		print("e.g. 'edelib-mime-find plain'")
	else:
		mime_search(sys.argv[1])


if __name__ == "__main__":
	main()
