import re
from pprint import pprint


IGNORE = "./diffios_ignore"


def context_list(config_file):
    ig, normalize, prev, contextual = [], [], [], []
    config_file = open(config_file).readlines()
    for line in config_file:
        if not line.startswith("!") and len(line) > 1:
            normalize.append(line.rstrip())
    for line in normalize:
        if line.startswith(" "):
            prev.append(line)
        else:
            contextual.append(prev)
            prev = [line]
    ignored = open(IGNORE).readlines()
    ignored = [elem.strip() for elem in ignored]
    contextual = [elem for elem in contextual if len(elem) > 0]
    for m, elem in enumerate(contextual):
        for n, e in enumerate(elem):
            for i in ignored:
                if re.findall(i, e.lower()):
                    if n == 0:
                        ig.append(contextual[m])
                        contextual[m] = []
                    else:
                        ig.append(elem[n])
                        elem[n] = ""
    contextual = [x for x in contextual if len(x) > 0]
    return sorted(contextual)


def compare(candidate, case):
    diff = {"plus": [], "minus": []}
    case_grp_head = [line[0] for line in case if len(line) > 1]
    cand_grp_head = [line[0] for line in candidate if len(line) > 1]
    for case_line in case:
        if len(case_line) == 1 and case_line not in candidate:
            diff["plus"].append(case_line)
        if len(case_line) > 1:
            first_line = case_line[0]
            if first_line in cand_grp_head:
                for cand_line in candidate:
                    if len(cand_line) > 1:
                        if case_line[0] == cand_line[0]:
                            plus = [case_line[0]]
                            minus = [cand_line[0]]
                            for case_grp_line in case_line:
                                if case_grp_line not in cand_line:
                                    plus.append(case_grp_line)
                            for cand_grp_line in cand_line:
                                if cand_grp_line not in case_line:
                                    minus.append(cand_grp_line)
                            if len(plus) > 1:
                                diff["plus"].append(plus)
                            if len(minus) > 1:
                                diff["minus"].append(minus)
            else:
                diff["plus"].append(case_line)
    for cand_line in candidate:
        if len(cand_line) == 1 and cand_line not in case:
            diff["minus"].append(cand_line)
        if len(cand_line) > 1:
            first_line = cand_line[0]
            if first_line in case_grp_head:
                for case_line in case:
                    if len(case_line) > 1:
                        if cand_line[0] == case_line[0]:
                            plus = [cand_line[0]]
                            minus = [case_line[0]]
                            for cand_grp_line in cand_line:
                                if cand_grp_line not in case_line:
                                    plus.append(cand_grp_line)
                            for case_grp_line in case_line:
                                if case_grp_line not in cand_line:
                                    minus.append(case_grp_line)
                            if len(plus) > 1:
                                diff["plus"].append(plus)
                            if len(minus) > 1:
                                diff["minus"].append(minus)
            else:
                diff["minus"].append(cand_line)
    return diff


candidate = context_list("./jon_candidate.conf")
case = context_list("./jon_cases/10.1.240.19.conf")

diff = compare(candidate, case)
pprint(diff)

# def main():

#     parser = argparse.ArgumentParser(
#     prog="cscodiff",
#     description="""
#         list differences between multiple Cisco configuration files
#         and a base Cisco configuration file.
#         """,
#     formatter_class=argparse.RawDescriptionHelpFormatter,
#     epilog="""
#         TODO: add epic description
#   	""")
#     parser.add_argument("base_file",
#                         help="Specify the base configuration file you are comparing against")
#     parser.add_argument("comparison_files",
#                         help="Specify the directory containing the Cisco configuration files you want to compare against the base file")
#     #  group = parser.add_mutually_exclusive_group()
#     #  group.add_argument("--csv", help="Output results as CSV file", action="store_true")
#     args = parser.parse_args()


#     base = filelines(args.base_file)
#     for f in os.listdir(args.comparison_files):
#         case = filelines(os.path.join(args.comparison_files, f))
#         diff = compare(base, case)
#         append_to_file('[{0}] {1}\n\n{2}\n'.format(hostname(case), '*' * 50, diff))


# if __name__ == '__main__':
#     main()

@@ -3771,1187 +3771,4 @@
 ff)%0A
-%0A# def main():%0A%0A#     parser = argparse.ArgumentParser(%0A#     prog=%22cscodiff%22,%0A#     description=%22%22%22%0A#         list differences between multiple Cisco configuration files%0A#         and a base Cisco configuration file.%0A#         %22%22%22,%0A#     formatter_class=argparse.RawDescriptionHelpFormatter,%0A#     epilog=%22%22%22%0A#         TODO: add epic description%0A#   %09%22%22%22)%0A#     parser.add_argument(%22base_file%22,%0A#                         help=%22Specify the base configuration file you are comparing against%22)%0A#     parser.add_argument(%22comparison_files%22,%0A#                         help=%22Specify the directory containing the Cisco configuration files you want to compare against the base file%22)%0A#     #  group = parser.add_mutually_exclusive_group()%0A#     #  group.add_argument(%22--csv%22, help=%22Output results as CSV file%22, action=%22store_true%22)%0A#     args = parser.parse_args()%0A%0A%0A#     base = filelines(args.base_file)%0A#     for f in os.listdir(args.comparison_files):%0A#         case = filelines(os.path.join(args.comparison_files, f))%0A#         diff = compare(base, case)%0A#         append_to_file('%5B%7B0%7D%5D %7B1%7D%5Cn%5Cn%7B2%7D%5Cn'.format(hostname(case), '*' * 50, diff))%0A%0A%0A# if __name__ == '__main__':%0A#     main()%0A

remove main()