#!/usr/bin/env python

import argparse
from gerrit_python_tools import sync


def get_args():
    """
    Set up and use the argument parser.

    @return argparse.Namespace

    """
    # Description
    description = ("Setup/sync gerrit groups, gerrit users, and initial"
                   " projects as specified by a configuration file.")
    parser = argparse.ArgumentParser(description=description)

    # Configuration file location - Optional
    default_config = '/etc/gerrit-python-tools/projects.yaml'
    parser.add_argument('--config', type=str, default=default_config,
                        help="Path to yaml file(default: %s" % default_config)

    # Specify single project - Optional
    project_help = ("Specify a single project to setup/sync. Project must"
                    " be in the config file. This option will skip gerrit"
                    "  groups and gerrit users.")
    parser.add_argument('--project', type=str, default=None,
                        help=project_help)

    # Doesn't acutally start a daemon. Merely indicates gerrit-sync
    # should be a long running process. Should be managed by upstart
    parser.add_argument('--daemon', '-d', action="store_true",
                        help="Run as daemon.")

    # Parse and return args
    args = parser.parse_args()
    return args

if __name__ == '__main__':
    args = get_args()

    kwargs = {'yaml_file': args.config}

    if not args.daemon:
        # If a specific project is indicated, only sync that project.
        if args.project:
            kwargs['groups'] = False
            kwargs['users'] = False
            kwargs['project'] = args.project
        sync.sync(**kwargs)
        exit()

    else:
        sync.sync_daemon(yaml_file=args.config)
