#!/usr/bin/python
# -*- coding: UTF-8 -*-
import argparse
import niprov

parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="command",
    title='subcommands')

discover = subparsers.add_parser('discover', 
    help='Find image files.')
discover.add_argument('root', default='.', 
    help='Directory below which to search for files.')

log = subparsers.add_parser('log', 
    help='Enter provenance of new files created as the result of a '+
    'transformation of existing files. Multiple new and parent files allowed.')
log.add_argument('transformation', 
    help='Name of the transformation applied.')
log.add_argument('--new','-n', nargs='+',
    help='Path to a resulting image.')
log.add_argument('--parent','-p', nargs='+',
    help='Path to an image that was the basis of the transformation.')
log.add_argument('--code', default=None,
    help='Code used to generate the new image.')

record = subparsers.add_parser('record',
    help='Run shell commands and log provenance for files created by it.')
record.add_argument('code', nargs=argparse.REMAINDER,
    help='Shell commands that result in a new image.')

add = subparsers.add_parser('add', 
    help='Simply register the file for future reference.')
add.add_argument('new', 
    help='Where the file resides.')

report = subparsers.add_parser('report',
    help='Provide provenance for known files.')
report.add_argument('--file', default=None,
    help='Report on the file at this location.')
report.add_argument('--subject', default=None,
    help='Report on files for this subject.')
report.add_argument('--html', dest='format', action='store_const',
    const='html', default=None,
    help='Export provenance as html file(s).')

report = subparsers.add_parser('version',
    help='Show niprov software version.')

args = parser.parse_args()
if args.command == 'discover':
    niprov.discover(args.root)
elif args.command == 'log':
    niprov.log(args.new, args.transformation, args.parent, code=args.code)
elif args.command == 'record':
    niprov.record(args.code)
elif args.command == 'add':
    niprov.add(args.new)
elif args.command == 'report':
    niprov.report(format=args.format, forFile=args.file, forSubject=args.subject)
elif args.command == 'version':
    import pkg_resources
    print(pkg_resources.get_distribution("niprov").version)
