#!/usr/bin/python

import argparse
import sys
import os
import json

# Get the path to the executable
executablePath = os.path.dirname(os.path.realpath(__file__))

# Import program modules
sys.path.append(os.path.join(executablePath,'..'))
from vsg import rule_list
from vsg import vhdlFile

def parse_command_line_arguments():
    '''Parses the command line arguments and returns them.'''

    parser = argparse.ArgumentParser(prog='VHDL Style Guide (VSG)',
                                     description='Analyzes VHDL files\
                                                  for style guide violations.')

    parser.add_argument('-f', '--filename', required=True, help = 'File to analyze')
    parser.add_argument('--local_rules', help = 'Path to local rules')
    parser.add_argument('--configuration', help = 'JSON configuration file')
    parser.add_argument('--fix', default=False, action='store_true', help = 'Fix issues found')

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)
    else:
        return parser.parse_args()


def read_configuration_file(commandLineArguments):
    if commandLineArguments.configuration:
        with open(commandLineArguments.configuration) as json_file:
            return json.load(json_file)


def main():
    '''Main routine of the VHDL Style Guide (VSG) program.'''

    commandLineArguments = parse_command_line_arguments()

    configuration = read_configuration_file(commandLineArguments)

    ### Add local rule path to system path so the rules can be loaded
    if commandLineArguments.local_rules:
        sys.path.append(os.path.abspath(commandLineArguments.local_rules))
    oVhdlFile = vhdlFile.vhdlFile(commandLineArguments.filename)
    oRules = rule_list.list(oVhdlFile, commandLineArguments.local_rules)
    oRules.configure(configuration)

    if commandLineArguments.fix:
        oRules.fix()
        oVhdlFile.write()

    oRules.check_rules()
    oRules.report_violations()

if __name__ == '__main__':
    main()
