#!/usr/bin/env python

from __future__ import print_function

import argparse
import logging
import os
import sys
import yaml

from rosinstall_generator.generator import ARG_ALL_PACKAGES, ARG_CURRENT_ENVIRONMENT, generate_rosinstall, sort_rosinstall


def main(argv=sys.argv[1:]):
    distro_name = os.environ['ROS_DISTRO'] if 'ROS_DISTRO' in os.environ else None
    parser = argparse.ArgumentParser(
        description='Generate a .rosinstall file for a set of packages.')
    parser.add_argument('--debug', action='store_true', default=False,
        help='Print debug information to stderr')
    parser.add_argument('--rosdistro', required=distro_name is None, default=distro_name,
        help='The ROS distro (default: environment variable ROS_DISTRO if defined)')
    parser.add_argument('package_names', nargs='+', metavar='pkgname',
        help="Wet package names, dry stack names or variant names. Use '%s' to specify all packages available in the current environment. Use '%s' to specify all release packages." % (ARG_CURRENT_ENVIRONMENT, ARG_ALL_PACKAGES))

    group = parser.add_mutually_exclusive_group()
    group.add_argument('--deps', action='store_true', default=False,
        help='Include recursive dependencies')
    group.add_argument('--deps-up-to', nargs='*',
        help="A set of packages which will limit the recursive dependencies to packages which (in-)directly depend on a package in this set. Use '%s' to specify all packages available in the current environment." % ARG_CURRENT_ENVIRONMENT)

    # implies either --deps or --deps-up-to
    parser.add_argument('--deps-only', action='store_true', default=False,
        help='Include only the recursive dependencies but not the specified packages')

    group = parser.add_mutually_exclusive_group()
    group.add_argument('--wet-only', action='store_true', default=False,
        help='Only include wet packages')
    group.add_argument('--dry-only', action='store_true', default=False,
        help='Only include dry stacks')

    parser.add_argument('--exclude', nargs='*',
        help="Exclude a set of packages (also skips further recursive dependencies). Use '%s' to specify all packages available in the current environment." % ARG_CURRENT_ENVIRONMENT)

    parser.add_argument('--tar', action='store_true', default=False,
        help='Use tarballs instead of repositories for wet packages (dry packages are always tarballs)')

    args = parser.parse_args(argv)

    # check for invalid combinations
    if args.deps_only and not args.deps and not args.deps_up_to:
        parser.error("Option '--deps-only' can only be used together with either '--deps' or '--deps-up-to'")

    if args.debug:
        logging.basicConfig(format='%(message)s', level=logging.DEBUG)

    try:
        rosinstall_data = generate_rosinstall(args.rosdistro, args.package_names,
            deps=args.deps, deps_up_to=args.deps_up_to, deps_only=args.deps_only,
            wet_only=args.wet_only, dry_only=args.dry_only,
            excludes=args.exclude,
            tar=args.tar)
    except RuntimeError as e:
        if args.debug:
            raise
        print(str(e), file=sys.stderr)
        return 1
    rosinstall_data = sort_rosinstall(rosinstall_data)
    print(yaml.safe_dump(rosinstall_data, default_flow_style=False))
    return 0


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