#!/usr/bin/env python
#
# Copyright 2016 Universidad de Cantabria
#
# Licensed under the EUPL, Version 1.1 only (the
# "Licence");
# You may not use this work except in compliance with the
# Licence.
# You may obtain a copy of the Licence at:
#
# http://ec.europa.eu/idabc/eupl
#
# Unless required by applicable law or agreed to in
# writing, software distributed under the Licence is
# distributed on an "AS IS" basis,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied.
# See the Licence for the specific language governing
# permissions and limitations under the Licence.
#

"""
WRF4G is a framework for the execution and monitoring of the WRF Modelling System 
on DCIs. For additional information, see http://meteo.unican.es/trac/wiki/WRF4G.

Usage: wrf4g [ --version ] [ -h | --help ] [ --dbg ]
             <command> [ <args>... ]

Options:
    -h --help  Show help.
    --version  Show version.
    --dbg      Debug mode.
    
wrf4g commands are:
   start       Start DRM4G daemon and ssh-agent.
   stop        Stop  DRM4G daemon and ssh-agent.
   status      Check DRM4G daemon and ssh-agent.
   syncdb      Create WRF4G database tables on the configured database.  
   conf        Configure DRM4G daemon and scheduler, database and logger parameters.
   exp         Manage WRF4G experiments.
   rea         Manage WRF4G realizations.
   resource    Manage computing resources.
   id          Manage identities for resources.
   host        Print information about the DCI hosts.
   job         Get status and history and cancel jobs.
 
See 'wrf4g <command> --help' for more information on a specific command.
"""
__version__  = '2.3.0'
__author__   = 'Carlos Blanco'
__revision__ = "$Id$"

from docopt import docopt

if __name__ == "__main__":
    args = docopt( __doc__, version = __version__ , options_first = True )
    argv = [ args[ '<command>' ] ] + args[ '<args>' ]
    sub_commands = [ 'start', 'stop', 'status', 'syncdb', 'conf', 'resource', 'host', 'job', 'id', 'rea', 'exp', 'vcp' ]
    if args['<command>'] in sub_commands :
        command = getattr( __import__( "wrf4g.commands.%s" %  args['<command>'] ).commands, args['<command>'] )
        arg = docopt( command.__doc__ , argv = argv )
        try :
            command.run( arg )
        except Exception as err :
            exit( err )
    else:
        from wrf4g.commands import get_similar_commands
        guess = get_similar_commands( args[ '<command>' ], sub_commands )
        msg = 'Unknown command "%s"' % args[ '<command>' ]
        if guess :
            msg = msg + ' maybe you meant "%s"' % guess
        exit( "%s.\nSee wrf4g --help" % msg )
