#!/usr/bin/env python
# Copyright (c) 2006-2008 Andrew Walkingshaw <andrew@lexical.org.uk>
# except XSLT components: copyright (c) 2005-2008 Toby White <tow@uszla.me.uk> 
#                 and (c) 2007-2008 Andrew Walkingshaw <andrew@lexical.org.uk>
#
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR USE OF OTHER DEALINGS IN
# THE SOFTWARE.


import sys
#INSTALLDIR

import golem, optparse

def make_parser():
    parser = optparse.OptionParser()
    parser.add_option("-m", "--model-dictionary", metavar="FILE",
                      help="Model dictionary to incorporate",
                      default=None, dest="model")
    parser.add_option("-i", "--input-config", dest="inputfn", 
                      metavar="FILE", help="Input configuration file",
                      default=None)
    parser.add_option("-o", "--output", dest="output",
                      metavar="FILE", 
                      help="Output filename (defaults to stdout)",
                      default="__STDOUT")
    parser.add_option("-p", "--prefix", dest="prefix",
                      help="Dictionary prefix")
    parser.add_option("-n", "--namespace", dest="namespace",
                      help="Dictionary namespace")
    parser.add_option("-t", "--title", dest="title", default="",
                      help="Dictionary title")
    parser.add_option("-l", "--no-title", dest="use_title",
                      action="store_false", default=True)
    parser.add_option("-d", "--no-id", dest="use_id",
                      action="store_false", default=True)
    return parser

def main():
    groupings = {
        "/cml:cml/cml:parameterList[@dictRef='input']":"parameterInInput",
        "/cml:cml/cml:propertyList[@id='finalProperties']":"inFinalProperties"
        }
    parser = make_parser()
    options, files = parser.parse_args()
    
    if options.output == "__STDOUT":
        output = sys.stdout
    else:
        output = open(options.output, "w")

    print >> output, golem.helpers.dict.make(
        files, options.namespace, options.prefix,
        options.title, groupings, model=options.model, inputfn=options.inputfn,
        use_title=options.use_title, use_id=options.use_id)

if __name__ == "__main__":
    main()
