#!/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('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)
        print(formatted)
        if not args.ignore_changes:
            if data != formatted:
                sys.exit(1)

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