#!/usr/bin/env python

# $Id: styles.py.in,v 1.5 2003/10/02 02:44:55 mackstann Exp $

import os

style_suffixes = ( ".kastyle", ".style" )

kahakai_bin = "/usr/X11R6/bin/kahakai"

style_names = []

def search_dir(dirname):
    """Recursively searches directories for style files."""
    menutext = ""

    if not os.path.isdir(dirname): return ""

    for filename in os.listdir(dirname):
        full_path = dirname + '/' + filename
        if os.path.isdir(full_path):
            menutext += search_dir(full_path)
        else:
            for suffix in style_suffixes:
                if filename.endswith(suffix):
                    nice_name = filename[:-len(suffix)]

                    # filter out duplicates
                    # (thanks to jabba2k for this and the sorting)
                    if nice_name not in style_names:
                      style_names.append(nice_name)
                      menutext += \
                        r'[item] (%s) "restart(%s --stylefile \"%s\")"' % \
                        (nice_name, kahakai_bin, full_path.replace("'", r"\'"))
                      menutext += "\n"
                    
    return menutext


if __name__ == "__main__":
    print "[title] (styles)"

    menutext = ""

    menutext += search_dir(os.path.expanduser("~/.kahakai/styles"))
    menutext += search_dir("/usr/X11R6/share/kahakai/styles")

    # sort alphabetically, ignoring case
    sorted = menutext.split("\n")
    sorted.sort(lambda a,b: cmp(a.lower(), b.lower()))
    menutext = '\n'.join(sorted)

    print menutext,


