#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
import os
import logging
from optparse import OptionParser

sys.path.append('{0}/..'.format(os.path.join(os.path.dirname(os.path.realpath(__file__)))))

from burpui.agent import BUIAgent as Agent

if __name__ == '__main__':
    """
    Main function
    """
    parser = OptionParser()
    parser.add_option('-v', '--verbose', dest='log', help='verbose output', action='store_true')
    parser.add_option('-c', '--config', dest='config', help='configuration file', metavar='CONFIG')

    (options, args) = parser.parse_args()

    if options.config:
        if os.path.isfile(options.config):
            conf = options.config
        else:
            raise IOError('File not found: \'{0}\''.format(options.config))
    else:
        root = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            '..',
            'share',
            'burpui',
            'etc'
        )
        conf_files = [
            '/etc/burp/buiagent.cfg',
            os.path.join(root, 'buiagent.cfg'),
            os.path.join(root, 'buiagent.sample.cfg')
        ]
        for p in conf_files:
            if os.path.isfile(p):
                conf = p
                break

    agent = Agent(conf, options.log)
    agent.run()
