#!/usr/bin/env python2
# -*- encoding: utf-8 -*-

import argparse
import os
import sys

import productmd.compose

here = sys.path[0]
if here != '/usr/bin':
    sys.path[0] = os.path.dirname(here)

import compose_utils


def run(opts, rsync_opts=[]):
    compose = productmd.compose.Compose(opts.compose)

    compose_utils.copy_compose(compose, opts.dest,
                               variants=opts.variant, arches=opts.arch,
                               dry_run=opts.dry_run,
                               rsync_opts=rsync_opts)


DESCRIPTION = ('Copy parts of compose to another location via rsync. '
               'You can specify which variants and/or architectures you '
               'want to copy. Multiple invocations of rsync may be used '
               'to achieve the desired result. '
               'Unrecognized options will be passed directly to rsync.')


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=DESCRIPTION)
    parser.add_argument('compose', metavar='COMPOSE',
                        help='path to compose that should be copied')
    parser.add_argument('dest', metavar='DEST',
                        help='where to copy the data (variants will form subdirectories here)')
    parser.add_argument('--arch', action='append', default=[],
                        help='copy only this architecture; can be used multiple times')
    parser.add_argument('--variant', action='append', default=[],
                        help='copy only this variant; can be used multiple times')
    parser.add_argument('-n', '--dry-run', default=False, action='store_true',
                        help='only print rsync commands, do not copy anything')

    opts, remaining = parser.parse_known_args()
    run(opts, remaining)
