#!/usr/bin/env python3
from compose_format import ComposeFormat


if __name__ == '__main__':

    import argparse
    import sys

    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--replace', action='store_const', const=True,
        help='should the original file be replaced?', default=False)
    parser.add_argument(
        '--ignore_changes', action='store_const', const=False,
        help='ignore changes for return code', default=True)
    parser.add_argument(
        '--non_strict', action='store_const', const=True,
        help='if this is provided, unknown keys errors are ignored', default=False)
    parser.add_argument('files', nargs=argparse.REMAINDER)
    args = parser.parse_args()
    formatter = ComposeFormat()

    if len(args.files) == 0:
        assert args.replace is False, 'replace makes no sense when reading from stdin'

        data = sys.stdin.read()
        formatted = formatter.format_string(data, strict=not args.non_strict)
        print(formatted)
        if not args.ignore_changes:
            if data != formatted:
                sys.exit(1)

    for path in args.files:
        if not formatter.format(path, replace=args.replace, strict=not args.non_strict):
            if args.replace:
                print('path {} has been changed'.format(path))
            if not args.ignore_changes:
                sys.exit(1)
