#!/usr/bin/env python

# Add vprof/share to sys.path
import os
import sys

cwd = os.path.dirname(os.path.realpath(__file__))
installPrefix = cwd[:cwd.find('bin')]

sys.path.append(installPrefix + 'share/vprofiler/Main')

from AnnotatorDispatcher import Annotator
from BreakdownDispatcher import Breakdown
from RestoreDispatcher import Restore
from FullDispatcher import Full

import argparse

##################################
# Set command line options begin #
##################################

parser = argparse.ArgumentParser(prog="vprof",
                         description="VProfiler - a system for identifying sources of variance within a codebase",
                         epilog="For more help, visit https://github.com/mozafari/vprofiler")

parser.add_argument('-b', '--build_script',
                    help='Path to the script which will build your source tree')

parser.add_argument('-r', '--run_script',
                    help='Path to the script which will automatically run your system')

parser.add_argument('--annotate',
                    action='store_true',
                    help='Setting this flag tells vprofiler to annotate your code')

parser.add_argument('--breakdown',
                    action='store_true',
                    help='Setting this flag tells vprofiler to determine which parts of your code most attribute to variance')

parser.add_argument('--restore', action='store_true',
                    help='Setting this flag tells vprofiler to restore your source tree to its un-annotated state. ' \
                          'That is, the state of the source tree before vprofiler performed annotations')

parser.add_argument('-o', '--originals_dir',
                    help='This directory contains all the un-annotated version of each file that vprofiler annotated')

parser.add_argument('-s', '-source_dir',
                    help='This directory is the root of your source tree.  This must be set when using the option -a')

parser.add_argument('-p', '-parallelism_functions',
                    help='A file listing the parallelism functions which should be profiled.  For specifics ' \
                            'on the format of this file see https://github.com/mozafari/vprofiler')

parser.add_argument('-c', '--compilation_db',
                    help='The compilation database vprofiler will use when instrumenting source code.')

parser.add_argument('-m', '--main_file',
                    help='The file with the semantic interval to be profiled')

parser.add_argument('-t', '--target_fxn',
                    help='The name of the top level function in which the semantic interval begins')

# For finding path to trace_tool.cc, maybe make some dir like /lib/vprof/ and put trace_tool
# there instead of having a separate command line option?

parser.add_argument('-d', '--data_dir',
                    help='Directory containing the data files created by vprofiler')

parser.add_argument('--heights_file',
                    help='File containing each function in the call graph and its ' \
                         'corresponding height in the graph')

parser.add_argument('-k', help='The number of factors to be selected')

parser.add_argument('--root_fxn', help='Take the subtree of the call graph where ' \
                                       'each node of the subtree has part of its ' \
                                       'execution contained in a target semantic ' \
                                       'interval.  root_fxn is the root of this '  \
                                       'subtree')

parser.add_argument('--selected_fxns', help='The functions you\'d like to be ' \
                                            'instrumented')

##################################
# Set command line options end   #
##################################

dispatchers = { 'Annotator': Annotator(),
                'Breakdown': Breakdown(),
                'Restore':   Restore(),
                'Full':      Full() }

args = parser.parse_args()

mode = ''
if args.annotate:
    mode = 'Annotator'
elif args.breakdown:
    mode = 'Breakdown'
elif args.restore:
    mode = 'Restore'
else:
    mode = 'Full'


if not dispatchers[mode].ParseOptions(args):
    print('Error parsing command line options')
    exit(1)

dispatchers[mode].Dispatch()
