#!/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='+', required=True,
    help='Path to a resulting image.')
log.add_argument('--parent','-p', nargs='+', required=True,
    help='Path to an image that was the basis of the transformation.')
log.add_argument('--transient', action='store_const', const=True, default=False, 
    help='Indicate that the file may not exist now or may be temporary.')
log.add_argument('--code', default=None,
    help='Code used to generate the new image.')
log.add_argument('--logtext', default=None,
    help='Feedback printed during transformation.')
log.add_argument('--script', default=None,
    help='Path to the code file used for the transformation.')

record = subparsers.add_parser('record',
    help='Run shell commands and log provenance for files created by it.')
record.add_argument('--transient', action='store_const', const=True, default=False, 
    help='Indicate that the file may not exist now or may be temporary.')
record.add_argument('--new','-n', nargs='*',
    help='Path to a resulting image.')
record.add_argument('--parent','-p', nargs='*',
    help='Path to an image that was the basis of the transformation.')
record.add_argument('code',
    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.')
add.add_argument('--transient', action='store_const', const=True, default=False, 
    help='Indicate that the file may not exist now or may be temporary.')

report = subparsers.add_parser('report',
    help='Provide provenance for known files. By default shows most recent 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('--statistics', action='store_const', const=True, default=False, 
    help='Summarize provenance collected.')
report.add_argument('--stdout', dest='medium', action='store_const',
    const='stdout', default=None,
    help='Export provenance on the terminal. (default medium)')
report.add_argument('--narrative', dest='form', action='store_const',
    const='narrative', default=None,
    help='Provenance in story form instead of key-value pairs.')
report.add_argument('--pipeline', dest='form', action='store_const',
    const='pipeline', default=None,
    help='Pretty-print pipeline for a file specified with --file.')

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

rename = subparsers.add_parser('rename',
    help='Rename dicom files without extension.')
rename.add_argument('directory', 
    help='Directory in which to look for files to rename.')

todo = subparsers.add_parser('todo',
    help='Show files marked for approval.')

approve = subparsers.add_parser('approve',
    help='Mark one file as having passed QC.')
approve.add_argument('filepath', 
    help='Where the file resides.')

serve = subparsers.add_parser('serve',
    help='Start a webserver that allows browsing through provenance.')

export = subparsers.add_parser('export',
    help='Backup provenance metadata to a file.')

importp = subparsers.add_parser('import',
    help='Load backup data from a file.')
importp.add_argument('file',
    help='Path to json file with provenance backup.')

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, 
        transient=args.transient, logtext=args.logtext, script=args.script)
elif args.command == 'record':
    niprov.record(args.code, new=args.new, parents=args.parent, 
        transient=args.transient)
elif args.command == 'add':
    niprov.add(args.new, transient=args.transient)
elif args.command == 'report':
    if args.medium is None:
        args.medium = 'stdout'
    niprov.report(medium=args.medium, form=args.form, forFile=args.file, 
        forSubject=args.subject, statistics=args.statistics)
elif args.command == 'version':
    import pkg_resources
    print(pkg_resources.get_distribution("niprov").version)
elif args.command == 'rename':
    niprov.renameDicoms(args.directory)
elif args.command == 'todo':
    niprov.markedForApproval()
elif args.command == 'approve':
    niprov.approve(args.filepath)
elif args.command == 'serve':
    niprov.serve()
elif args.command == 'export':
    niprov.export()
elif args.command == 'import':
    niprov.importp(args.file)
