#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Merges given JSON Schemas, inferring the required properties
"""

from __future__ import absolute_import, division, print_function

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))

from skinfer.json_schema_merger import merge_schema
import json


def get_schemas(schema_filelist):
    for schema_file in args.schemas:
        with open(schema_file) as f:
            yield json.load(f)


def run(args):
    schemas = get_schemas(args.schemas)

    merged = next(schemas)
    for schema in schemas:
        merged = merge_schema(merged, schema)

    if args.output:
        with open(args.output, 'w') as f:
            json.dump(merged, f, indent=4)
    else:
        print(json.dumps(merged, indent=4))


if '__main__' == __name__:
    import argparse
    parser = argparse.ArgumentParser(description=__doc__)

    parser.add_argument('-o', dest='output',
                        help='Write JSON schema to this file')
    parser.add_argument('schemas', nargs='+',
                        help='List of JSON schema files to merge')

    args = parser.parse_args()
    run(args)
