#!/usr/bin/python
#
# This file is part of the Coral project.
# Please read the copyright notice in the LICENSE file.
#
# coral-start.py - Start a Coral tool
#
# SYNOPSIS
#
# coral-start [ --list | --help | tool-name [--help | tool-args] ]
#
# DESCRIPTION
#
# Start a tool such as modeler, the model editor, or diff, the model difference utility
# --list shows the list of supported tools
# 
# AUTHORS
#
# Coral Team - 2003-2005


tools={
    'arbitrary_model_generator' : 'coral.scripts.arbitrary_model_generator',
    'countelements' : 'coral.scripts.countelements',
    'diff' : "coral.scripts.diff",
    'load' : "coral.scripts.load",
    'merge' : "coral.scripts.merge",
    'modeler' : "coral.modeler.main",
    'natural2smd' : 'coral.lgen.natural2smd',
    'pegen' : 'coral.lgen.pegen.pegen',
    'savediagram' : 'coral.scripts.savediagram',
    'script' : 'coral.core.transform.spyrun',
    'evaluateconstraints' : 'coral.addins.constraint.script.evaluateconstraints',
    }

import sys

usage="Usage: coral-start [ --list | --help | tool-name [--help | tool-args] ]"

if len(sys.argv)<2:
    name="modeler"
else:
    if sys.argv[1]=="--list" or sys.argv[1]=="--help":
        print usage
        print "Available tools:"
        print "%-20s %s" % ("Name","Module")
        print "-"*70
        for t in tools.keys():
            print "%-20s %s" % (t, tools[t])
        sys.exit(0)
    
    if sys.argv[1]=="--dump":
        for t in tools.keys():
            print "coral-start",t
        sys.exit(0)
        
    name=sys.argv[1]
    
if not tools.has_key(name):
    print "Unknown tool ",name
    sys.exit(2)
else:
    try:
        name=tools[name]
        module=__import__(name,globals(),locals(),name)
        if hasattr(module,"main"):
            module.main(sys.argv[2:])
    except SystemExit,e:
        raise e
    except Exception,e:
        import traceback
        traceback.print_exc()
        sys.exit(2)
