#!/usr/bin/env python3

"""
OpenAccess_EPUB

Usage:
  oaepub [--version | --help] [options] COMMAND [ARGS ...]

Options:
  -h --help        show this help message and exit
  -v --version     show program version and exit
  -V --verbose     print additional information about command execution

The available commands are:
  batch       Convert all the contents of a directory to individual EPUB
  clearcache  Delete some, or all, of the contents of OpenAccess_EPUB's cache
  collection  Convert multiple articles into a single omnibus EPUB
  configure   Configure some settings for your OpenAccess_EPUB install
  convert     Convert explicit input(s) individually to EPUB
  publishers  Show which publishers are currently supported by OpenAccess_EPUB
  validate    Validate article XML files according to their specification

See 'oaepub COMMAND --help' for more information on a specific command.

For more information, please visit the project's GitHub page at:
https://github.com/SavinaRoja/OpenAccess_EPUB

Feel free to bother the developer for issues/suggestions/help.
"""

#Standard Library modules
import sys
import logging

#Non-Standard Library modules
from docopt import docopt

#OpenAccess_EPUB modules
from openaccess_epub._version import __version__


if __name__ == '__main__':
    args = docopt(__doc__,
                  version='OpenAccess_EPUB v.' + __version__,
                  options_first=True)

    log = logging.getLogger()

    def printverbose(info):
        if args['--verbose']:
            print(info)

    #Let's shape what gets passed along to the subcommands
    argv = []

    #If we want to pass an ubercommand option to a subcommand, do it now because
    #options are first, and like so:
    #if args['--verbose']:
    #    argv.append('--verbose')  # this would pass --verbose onward
    #argv += ['--verbose'] if args['--verbose'] else argv  # Cute, one-liner

    #Add on all of the ARGS ...  (mixture of options and args for sub-parsing)
    argv += args['ARGS']

    #Decide which subcommand to run based on args['COMMAND']
    if args['COMMAND'] == 'batch':
        import openaccess_epub.commands.batch as batch
        main = batch.main
        printverbose(
'''Running Batch command with the following options/arguments:
{0}

Batch command script located at:
{1}'''.format(str(argv), str(batch.__file__)))

    elif args['COMMAND'] == 'clearcache':
        import openaccess_epub.commands.clearcache as clearcache
        main = clearcache.main
        printverbose(
'''Running Clearcache command with the following options/arguments:
{0}

Clearcache command script located at:
{1}'''.format(str(argv), str(clearcache.__file__)))

    elif args['COMMAND'] == 'collection':
        import openaccess_epub.commands.collection as collection
        main = collection.main
        printverbose(
'''Running Collection command with the following options/arguments:
{0}

Collection command script located at:
{1}'''.format(str(argv), str(collection.__file__)))

    elif args['COMMAND'] == 'configure':
        import openaccess_epub.commands.configure as configure
        main = configure.main
        printverbose(
'''Running Configure command with the following options/arguments:
{0}

Configure command script located at:
{1}'''.format(str(argv), str(configure.__file__)))

    elif args['COMMAND'] == 'convert':
        import openaccess_epub.commands.convert as convert
        main = convert.main
        printverbose(
'''Running Convert command with the following options/arguments:
{0}

Convert command script located at:
{1}'''.format(str(argv), str(convert.__file__)))

    elif args['COMMAND'] == 'validate':
        import openaccess_epub.commands.validate as validate
        main = validate.main
        printverbose(
'''Running Validate command with the following options/arguments:
{0}

Validate command script located at:
{1}'''.format(str(argv), str(validate.__file__)))

    elif args['COMMAND'] == 'publishers':
        sys.exit('''\
PLoS      - http://www.plos.org/
Frontiers - http://www.frontiersin.org/''')

    elif args['COMMAND'] == 'haiku':
        sys.exit('''
A field of cotton--
as if the moon
had flowered.

- Matsuo Bashō''')
    else:
        print('Your command \'{0}\' was not recognized'.format(args['COMMAND']))
        sys.exit()

    main(argv=argv)

