#!/usr/bin/env python
##############################################################################
# Part of the Agnos RPC Framework
#    http://agnos.sourceforge.net
#
# Copyright 2011, International Business Machines Corp.
#                 Author: Tomer Filiba (tomerf@il.ibm.com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################

"""
agnos command-line utility
usage: 
    agnosc -t <LANG> myidl.xml
where LANG is one of
    "python", "java", "csharp", "cpp", "doc"
example:
    agnosc -t java myidl.xml
"""
import os
import sys

from optparse import OptionParser
from agnos_compiler import compile, IDLError
from agnos_compiler.targets import JavaTarget, PythonTarget, CPPTarget, CSharpTarget, DocTarget

TARGET_ALIASES = {
    "doc" : DocTarget,
    "html" : DocTarget,
    "py" : PythonTarget, 
    "python" : PythonTarget, 
    "java" : JavaTarget, 
    "csharp" : CSharpTarget,
    "c#" : CSharpTarget,
    "cs" : CSharpTarget,
    "cpp" : CPPTarget,
    "c++" : CPPTarget,
}

parser = OptionParser()
parser.add_option("-o", "--outdir", dest="outdir", default=None,
                  help="generate output into OUTDIR; the default directory used is that of the input file",  
                  metavar="OUTDIR")
parser.add_option("-t", "--target", dest="target",
                  help="specify the target output language ('python', 'java', 'csharp', 'cpp', or 'doc')",
                  metavar="TARGET")
parser.add_option("--doc",
                  action="store_true", dest="emitdoc", default=False,
                  help="emit documentation into generated code")
parser.add_option("--debug",
                  action="store_true", dest="debug", default=False,
                  help="set debug flag")
parser.add_option("-O", "--opt",
                  action="append", dest="options", default=[],
                  help="pass target-specific options, e.g., '-O foo' or '-O foo=bar'")


if __name__ == "__main__":
    options, args = parser.parse_args()
    if not args:
        parser.error("must specify IDL input file(s)")
    if not options.target:
        parser.error("must specify a target")
    if options.emitdoc:
        options.options.append("doc")
    try:
        target_factory = TARGET_ALIASES[options.target.lower()]
    except KeyError:
        parser.error("invalid target: %r" % (target,))
    target_options = dict((opt.split("=", 1) if "=" in opt else (opt, True)) 
        for opt in options.options)
    try:
        for filename in args:
            if options.outdir:
                outdir = options.outdir
            else:
                outdir = os.path.dirname(filename)
                if not outdir:
                    outdir = "."
            compile(filename, target_factory(outdir, target_options))
    except IDLError:
        raise
    except Exception as ex:
        if not options.debug:
            raise
        import pdb, sys
        print ("-" * 50)
        print (repr(ex))
        print()
        pdb.post_mortem(sys.exc_info()[2])



