#! /usr/bin/env python

# Copyright (c) 2011 SEOmoz
# 
# 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 THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import argparse
import pkg_resources
# First off, read the arguments
parser = argparse.ArgumentParser(description='Rake, for Python')

parser.add_argument('method', help='The task to run')
parser.add_argument('--verbose', dest='verbose', action='store_true',
    help='Be extra talkative')
parser.add_argument('--dry-run', dest='dryRun', action='store_true',
    help='Don\'t actually run the command, but show the args that would be used')

ver = pkg_resources.require('shovel')[0].version
parser.add_argument('--version', action='version', version='Shovel v %s' %(ver), help='print the version of Shovel.')

# Parse our arguments
clargs, remaining = parser.parse_known_args()

import shovel
import logging
if clargs.verbose:
    shovel.logger.setLevel(logging.DEBUG)

def parse(remaining):
    # Now build up our arguments to the function
    args   = []
    kwargs = {}
    kw     = None
    for item in remaining:
        # If it's been given an argument name, save it and wait for the
        # next argument to be parsed
        if item.startswith('--'):
            # If this item begins with a '--', but there already exists
            # a keyword argument name, then we should treat that last 
            # name as a flag for 'True'
            if kw:
                kwargs[kw] = True
            kw, sep, value = item.strip('-').partition('=')
            if value:
                kwargs[kw] = value
                kw = None
        elif kw != None:
            kwargs[kw] = item
            kw = None
        else:
            args.append(item)
    
    # If there's still a keyword argument name, we'll treat is as a
    # flag to set that option to True.
    if kw:
        kwargs[kw] = True
    
    return args, kwargs

args, kwargs = parse(remaining)
shovel.load()
if clargs.method == 'help':
    shovel.help(*args, **kwargs)
elif clargs.method:
    # Try to get the first command provided
    tasks = shovel.Task.find(clargs.method)
    if not tasks:
        print 'Could not find task "%s"' % clargs.method
        exit(1)
    
    if len(tasks) > 1:
        print 'Specifier "%s" matches multiple tasks:' % clargs.method
        for task in tasks:
            print '\t%s' % task.fullname
        exit(2)
    
    task = tasks[0]
    if clargs.dryRun:
        task.dry(*args, **kwargs)
    else:
        task(*args, **kwargs)
else:
    print 'Help'
