#!/usr/bin/env python
###############################################################################
#                                                                             #
#    ScreamingBackpack                                                         #
#                                                                             #
#    Description!!                                                            #
#                                                                             #
#    Copyright (C) Michael Imelfort                                           #
#                                                                             #
###############################################################################
#                                                                             #
#    This program is free software: you can redistribute it and/or modify     #
#    it under the terms of the GNU General Public License as published by     #
#    the Free Software Foundation, either version 3 of the License, or        #
#    (at your option) any later version.                                      #
#                                                                             #
#    This program is distributed in the hope that it will be useful,          #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
#    GNU General Public License for more details.                             #
#                                                                             #
#    You should have received a copy of the GNU General Public License        #
#    along with this program. If not, see <http://www.gnu.org/licenses/>.     #
#                                                                             #
###############################################################################

__author__ = "Michael Imelfort"
__copyright__ = "Copyright 2013"
__credits__ = ["Michael Imelfort"]
__license__ = "GPLv3"
__version__ = "0.2.0"
__maintainer__ = "Michael Imelfort"
__email__ = "mike@mikeimelfort.com"
__status__ = "Beta"

###############################################################################
###############################################################################
###############################################################################
###############################################################################

# system imports

import argparse
import sys

# local imports

from screamingbackpack.manifestManager import ManifestManager

###############################################################################
###############################################################################
###############################################################################
###############################################################################

def doWork(args):
    """Wrapper function to allow easy profiling"""
    if (args.subparser_name == 'create'):
        # create a new manifest
        MM = ManifestManager(manType=args.mantype)
        MM.createManifest(args.path, manifestName=args.name)

    elif (args.subparser_name == 'diff'):
        # work out the difference between two manifests
        MM = ManifestManager()
        MM.diffManifests(args.localpath, args.sourcepath, localManifestName=args.localname, sourceManifestName=args.sourcename, printDiffs=True)

    elif (args.subparser_name == 'update'):
        # update a local manifest
        MM = ManifestManager()
        MM.updateManifest(args.localpath, args.sourcepath, localManifestName=args.localname, sourceManifestName=args.sourcename)

    else:
        print "ERROR: Unknown mode '%s'" % args.subparser_name


    URL = "https://data.ace.uq.edu.au/public/CheckM_databases/.dmanifest"

###############################################################################
###############################################################################
###############################################################################
###############################################################################

if __name__ == '__main__':
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    subparsers = parser.add_subparsers(help="--", dest='subparser_name')

    create_parser = subparsers.add_parser('create',
                                          formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                                          help='Create a manifest',
                                          description='Create a manifest')
    create_parser.add_argument('path', help="path to files to be added to manifest")
    create_parser.add_argument('-t', '--mantype', default=None, help="type of the manifest")
    create_parser.add_argument('-n', '--name', default=None, help="name for the manifest file")


    diff_parser = subparsers.add_parser('diff',
                                         formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                                         help='Work out the difference between two manifests',
                                         description='Work out the difference between two manifests')
    diff_parser.add_argument('localpath', help="path to the local collection of files")
    diff_parser.add_argument('-l', '--localname', default=None, help="name of the local manifest file")
    diff_parser.add_argument('sourcepath', help="path to the collection of source files (URL or file path)")
    diff_parser.add_argument('-s', '--sourcename', default=None, help="name of the source manifest file")

    update_parser = subparsers.add_parser('update',
                                          formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                                          help='Update a local manifest',
                                          description='Update a local manifest')
    update_parser.add_argument('localpath', help="path to the local collection of files")
    update_parser.add_argument('-l', '--localname', default=None, help="name of the local manifest file")
    update_parser.add_argument('sourcepath', help="path to the collection of source files (URL or file path)")
    update_parser.add_argument('-s', '--sourcename', default=None, help="name of the source manifest file")

    # parse the arguments
    args = parser.parse_args()

    # profiling happens here. If you'd like to track the speed your code runs at
    # then set the following to True and voila!
    if(False):
        import cProfile
        cProfile.run('doWork(args)', 'profile')
        ##########################################
        ##########################################
        # Use this in python console!
        #import pstats
        #p = pstats.Stats('prof')
        #p.sort_stats('cumulative').print_stats(10)
        #p.sort_stats('time').print_stats(10)
        ##########################################
        ##########################################
    else:
        doWork(args)

###############################################################################
###############################################################################
###############################################################################
###############################################################################

