#!/usr/bin/env python
# -*- 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.insert(0, os.path.dirname(here))

from compose_utils.legacy import write_legacy_composeinfo


def run(opts):
    compose = productmd.compose.Compose(opts.compose)
    target = opts.path or compose.compose_path
    with open(os.path.join(target, '.composeinfo'), 'w') as f:
        write_legacy_composeinfo(compose.info, f)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('compose', metavar='COMPOSE')
    parser.add_argument('-p', '--path',
                        help='Directory in which to write the .composeinfo file. '
                             'Defaults to the compose subdirectory.')

    opts = parser.parse_args()
    run(opts)
