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

from __future__ import print_function

import argparse
import os
import sys

import productmd.compose

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

from compose_utils.changelog import ComposeChangelog


def run(opts):
    old_compose = productmd.compose.Compose(opts.compose1)
    new_compose = productmd.compose.Compose(opts.compose2)

    changelog = ComposeChangelog()
    try:
        data = changelog.get_changelog(old_compose, new_compose)
    except RuntimeError as exc:
        print('Error: {0}'.format(exc), file=sys.stderr)
        sys.exit(1)

    name = opts.name or new_compose.info.compose.id

    changelog.write(data, path=opts.path, name=name, verbose=opts.full)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('compose1', metavar='OLD_COMPOSE')
    parser.add_argument('compose2', metavar='NEW_COMPOSE')
    parser.add_argument('-p', '--path',
                        help='Target directory where to write the changelogs. '
                             'Defaults to current directory.')
    parser.add_argument('-n', '--name',
                        help='Identifier to be included in generated filenames. '
                             'Defaults to compose id of new compose.')
    parser.add_argument('--full',
                        action='store_true',
                        default=False,
                        help='Print all information about about packages.')
    opts = parser.parse_args()
    run(opts)
