#!/usr/local/bin/python

# remake-menus v1.6
# (c)1997-98 Dan Potter

# This code is freely usable for whatever purposes you want. However, no
# warranties are implied, so if it messes something up, don't sue me =).
# However, comments/questions/bug reports are welcome. (I forgot that
# one in KanjiHack, didn't I? oops...!)

# This script rebuilds the menus file for WindowMaker from the directory
# tree located in ~/GNUstep/Library/WindowMaker/menus, and stores the
# result on stdout. If some comment in this doesn't make sense, it's 
# because it was applicable in AfterStep and now it's not. I haven't done
# much cleaning on it yet.. sorry =)

# This works by recursively examaning directories until there are no more
# .MENU directories, then it backs out, building the text file output.

# For path stuff.
import os

# String processing functions
import string

# For replacing tokens in extdefs definitions.
import regsub

# Read in the extension definitions file.
from extdefs import *

# These are the valid non-extension WM commands (to avoid menu parse
# errors and dumps to shell).
validcmds = ['OPEN_MENU', 'WORKSPACE_MENU', 'EXEC', 'EXIT', 
	'RESTART', 'REFRESH', 'ARRANGE_ICONS', 'SHUTDOWN', 'SHOW_ALL',
	'HIDE_OTHERS']

# Seperates the extension and the filename
def sepExtension(name):
	idx = string.rfind(name, ".")
	realname = name[0:idx]
	extension = name[idx+1:]
	return (realname, extension)


# Takes a filename and reads the contents of the file, returning a string.
def readFile(name):
	f = open(name, 'rb')
	if not f: return ""
	rv = f.read(1024)
	f.close()
	return rv	


# Processes one menu directory
def processTree(name, path):
  # Expand the pathname fully
	realpath = os.path.expanduser(path)

  # Get a list of files in the directory
	files = os.listdir(realpath)

  # Sort them into submenus and other stuff
	submenus = []
	others = []
	for i in files:
		if (not string.find(i, ".MENU") == -1):
			submenus.append(i)
		else:
			others.append(i)

  # Sort each list
	submenus.sort()
	others.sort()

  # Get the actual title of the menu
	(menuname, menuext) = sepExtension(name)

  # Print out the new menu
	if (not name==''): print "\"" + menuname + "\" MENU"

  # For each submenu, go process it.
	for i in submenus:  processTree(i, path + '/' + i)

  # Now for the remaining menu choices..
	for i in others:
           # Seperate the name(choice title) and extension(command)
		(n, e) = sepExtension(i)

           # Is that command in the extsdefs.py file as an association?
		if (exts.has_key(e)):
                   # Yes, so process the association and use it as an EXEC
                   # command (could do other things later also, like SUB_MENU)
			target = os.readlink(realpath + '/' + n + '.' + e)
			execcmd = exts[e]
			execcmd = regsub.gsub("\{choice\}", n, execcmd)
			execcmd = regsub.gsub("\{file\}", target, execcmd)
			print "\"" + n + "\" EXEC " + execcmd + " &"
		else:
                   # Nope, so check it against our 'valid' list. If it's not
                   # in there, then print the line out commented out for
                   # debugging purposes. Otherwise, dump the contents of the
                   # file onto the line with the appropriate command.
			if (e not in validcmds):
				print "// [error] " + e + " \"" + n + "\""
			else:
				print "\"" + n + "\" " + e + " " + readFile(realpath + '/' + i)

   # End the menu definition.
	print "END"
	print ""
	print ""



# Print out a little warning header
print "// WARNING!!!! This file is generated by remake-menus! DO NOT EDIT!!!"
print ""
print "#include \"wmmacros\""
print ""


# Start at the root of the tree!
processTree('', '~/GNUstep/Library/WindowMaker/menu-tree')



