#!/usr/bin/env python

from __future__ import print_function
import argparse
import os
import sys

from euslint import lint


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('path', type=str, help='checking dir or file path')
    parser.add_argument(
        '-e', '--extension', type=str, help='file extension, defalut: l')
    parser.add_argument(
        '-l', '--max-line-length', type=int,
        help='maximum line length, defalut: 100')
    parser.add_argument(
        '-i', '--ignore', type=str, help='list of ignoring check ids.')
    args = parser.parse_args()

    path = args.path
    if args.extension is None:
        extension = 'l'
    else:
        extension = args.extension
    extension = '.' + extension
    max_line_length = args.max_line_length
    if max_line_length is None:
        max_line_length = 100
    if args.ignore is None:
        ignore_list = []
    else:
        ignore_list = args.ignore.split(',')
    retcode = 0
    if os.path.isfile(path):
        if lint(path, ignore_list, max_line_length) > 0:
            retcode = 1
    else:
        for root, dirs, files in os.walk(path):
            for file_ in files:
                filepath = os.path.join(root, file_)
                if filepath.endswith(extension):
                    if lint(filepath, ignore_list, max_line_length) > 0:
                        retcode = 1
    sys.exit(retcode)


if __name__ == '__main__':
    main()
