#!/usr/local/bin/python2.7
# -*- python-mode -*-
"""git-dag: An advanced git DAG visualizer
"""

__copyright__ = """
Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013
David Aguilar <davvid@gmail.com> and contributors

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

"""

import os
import sys
from argparse import ArgumentParser
from os.path import abspath
from os.path import dirname
from os.path import realpath

def setup_environment():
    """Provides access to the cola modules"""
    # Try to detect where it is run from and set prefix and the search path.
    # It is assumed that the user installed Cola using the --prefix= option
    prefix = dirname(dirname(realpath(abspath(__file__))))

    # Look for modules in the source or install trees
    cola_mod = os.path.join(prefix, 'cola', '__init__.py')
    if os.path.exists(cola_mod):
        # Source tree
        sys.path.insert(1, prefix)
    else:
        # Install tree
        install_lib = os.path.join(prefix, 'share', 'git-cola', 'lib')
        sys.path.insert(1, install_lib)
setup_environment()


from cola.app import add_common_arguments
from cola.app import application_init
from cola.app import application_start


def main():
    args = parse_args()
    return args.func(args)


def parse_args():
    parser = ArgumentParser()
    parser.set_defaults(func=dag)

    add_common_arguments(parser)
    parser.add_argument('-c', '--count', metavar='<count>',
                        type=int, default=1000,
                        help='number of commits to display')
    parser.add_argument('args', nargs='*', metavar='<args>',
                        help='git log arguments')
    return parser.parse_args()


def dag(args):
    context = application_init(args)
    from cola.widgets.dag import git_dag
    view = git_dag(context.model, args=args)
    return application_start(context, view)


if __name__ == '__main__':
    sys.exit(main())
