#!/usr/bin/env python
 ###########################################################################
 #                        makelegacyconfig.py script
 #                   part of the legacyconfig package
 #                       -------------------
 # Creates desktop & config files describing config programs available on
 # this machine, so that kcontrol will list them, and legacyconfig can
 # embed them
 #
 #   begin                : Tue May 21 2002
 #   copyright            : (C) 2002 by Jonathon Sim
 #   email                : jsim@free.net.nz
 #   url                  : http://jonsim.kdedevelopers.net
 ###########################################################################

 ###########################################################################
 #                                                                         #
 #   This program is free software; you can redistribute it and/or modify  #
 #   it under the terms of the GNU General Public License as published by  #
 #   the Free Software Foundation; either version 2 of the License, or     #
 #   (at your option) any later version.                                   #
 #                                                                         #
 ###########################################################################

import os, os.path, string, sys, commands
class KDEResourceLocator:
	"A class that wraps the KDE kde-config command, and performs a few sanity checks on its output"
	def __init__(self, appname):
		self.mAppName = appname

	def findFile(self, file, resourceType):
		"""Returns the full path to the specified file, following the KDE convention of prefering
		files in the users home tree"""
		(status,output) = commands.getstatusoutput("kde-config  --path %s"%resourceType)
		if status:
			raise OSError, "kde-config returned error"
		if not len(output) > 0:
			raise OSError, "kde-config returned nonsense output"
		resourcedirs = string.split(output, ":")

		for dir in resourcedirs:
			if resourceType =="data":
				#Look in the apps data dir
				dir = os.path.join(dir, self.mAppName)
			print "\tlooking in ", dir
			if os.path.exists(os.path.join(dir,file) ):
				return os.path.join(dir,file)
		raise OSError, "File not found in KDE dirs"

	def userPath(self, resourceType):
		"Returns the appropriate kde dir for the current user"
		(status,output) = commands.getstatusoutput("kde-config  --path %s"%resourceType)
		if status:
			raise OSError, "kde-config returned error"
		if not len(output) > 0:
			raise OSError, "kde-config returned nonsense output"
		if resourceType == "data":
			return os.path.join( string.split(output, ":")[0], self.mAppName)
		else:
			return string.split(output, ":")[0]

	def systemPath(self, resourceType):
		"Returns the appropriate system-wide kde dir"
		(status,output) = commands.getstatusoutput("kde-config  --expandvars --install %s"%resourceType)
		if status:
			raise OSError, "kde-config returned error"
		if not len(output) > 0:
			raise OSError, "kde-config returned nonsense output"
		if resourceType == "data":
			return os.path.join( string.split(output, ":")[0], self.mAppName)
		else:
			return string.split(output, ":")[0]




def main() :
	"The main function. Does everything."
	locator = KDEResourceLocator("klegacyconfig")
	if not os.path.exists("osbackend.py"):
		modpath = locator.findFile("osbackend.py", "data")
		sys.path.insert(0,os.path.dirname(modpath))
	import osbackend

	if not os.path.exists("apps.py"):
		modpath = locator.findFile("apps.py", "data")
		sys.path.insert(0,os.path.dirname(modpath))
	import apps
	osHandler  = osbackend.createOSHandler("user")
	osHandler.createSections(apps.tree_parent)
	for app in apps.Apps:
		filename = app[1]
		arg = app[4]
		command = filename + " " + arg
		dir = string.join(string.split(app[0],"/")[:-1], "/" )
		kcmname = "kcm" + os.path.splitext(os.path.basename(app[1]) )[0] + arg
		desktopname = dir + "/"+ kcmname
		if osHandler.isAppInstalled(filename):
			print "Found app %s, adding"%app[0]
			icon = app[2]
			windowtitle = app[3]
			osHandler.makeMenu(app[0], kcmname, command, icon)
			osHandler.makeConfig(kcmname, windowtitle, command)
		else:
			print "App not present%s, at %s => removing"%(app[0], filename)
			osHandler.removeMenu(app[0], kcmname)
	osHandler.close()
if __name__ == "__main__":
	main()





