#!/usr/local/bin/python2.5
#
#  Copyright (C) 2007 Mark Lee <avant-wn@lazymalevolence.com>
#
#  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.
#
#  This program 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 General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
#
#  Author : Mark Lee <avant-wn@lazymalevolence.com>

import sys
from ConfigParser import SafeConfigParser as ConfigParser
try:
    # >=dev-lang/python-2.5 cElementTree
    from xml.etree import cElementTree as ET
except ImportError:
    try:
        # >=dev-lang/python-2.5 ElementTree
        from xml.etree import ElementTree as ET
    except ImportError:
        try:
            # dev-python/lxml
            import lxml.etree as ET
        except ImportError:
            try:
                # dev-python/celementtree
                import cElementTree as ET
            except:
                # dev-python/elementtree
                from elementtree import ElementTree as ET

def main(argv):
    if len(argv) < 2:
        sys.stderr.write('Usage: %s [input file] [output file] ([key prefix])\n')
        sys.exit(1)
    else:
        in_schema = ConfigParser()
        in_fp = file(argv[0], 'r')
        in_schema.readfp(in_fp, argv[0])
        in_fp.close()
        out_schema = ET.ElementTree(ET.Element('gconfschemafile'))
        schemalist = ET.SubElement(out_schema.getroot(), 'schemalist')
        for key in in_schema.sections():
            if key.startswith('DEFAULT/'):
                real_key = key[8:]
            else:
                real_key = key
            if len(argv) == 3:
                full_key = '/apps/avant-window-navigator/applets/%s/%s' % (argv[2], real_key)
            else:
                full_key = '/apps/avant-window-navigator/%s' % real_key
            schema_key = '/schemas' + full_key
            schema_element = ET.SubElement(schemalist, 'schema')
            ET.SubElement(schema_element, 'key').text = schema_key
            ET.SubElement(schema_element, 'applyto').text = full_key
            ET.SubElement(schema_element, 'owner').text = 'avant-window-navigator'
            key_type = in_schema.get(key, 'type')
            element_type = ET.SubElement(schema_element, 'type')
            value = in_schema.get(key, 'default')
            if key_type.startswith('list-'):
                element_type.text = 'list'
                ET.SubElement(schema_element, 'list_type').text = key_type[5:]
                value = '[%s]' % value.replace(';', ',')
            else:
                element_type.text = key_type
            ET.SubElement(schema_element, 'default').text = value
            locale = ET.SubElement(schema_element, 'locale')
            locale.set('name', 'C')
            ET.SubElement(locale, 'short')
            ET.SubElement(locale, 'long').text = in_schema.get(key, 'description')
            for lang in [x[12:-1] for x in in_schema.options(key) if x.startswith('description[')]:
                locale = ET.SubElement(schema_element, 'locale')
                locale.set('name', lang)
                ET.SubElement(locale, 'short')
                ET.SubElement(locale, 'long').text = in_schema.get(key, 'description[%s]' % lang)

        out_schema.write(argv[1], encoding='utf-8')

if __name__ == '__main__':
    main(sys.argv[1:])

# vim: set ft=python et sw=4 ts=4 sts=4
