#!/bin/python3

from os import sys #To read stdin
from matrix import matrix
import copy

def print_usage(): # -h
    print("USAGE\n\t./303make makefile [file]")
    print("DESCRIPTION\n\tmakefile\tname of the makefile")
    print("\tfile\tname of a recently modified file")

def parser():
    is_print_matrix = 0
    if (len(sys.argv) == 2 and sys.argv[1] == "-h"):
        print_usage()
        quit (0)
    elif (len(sys.argv) == 2):
        is_print_matrix = 0
    elif (len(sys.argv) == 3):
        is_print_matrix = 1
    else :
        print_usage()
        quit (84) #exit
    try:
        with (open(sys.argv[1])) as file:
            data = file.read().rstrip().split('\n')
    except:
        quit (84)
    return data, is_print_matrix

data, is_print_matrix = parser()
save = copy.deepcopy(data)

#Reparse lines to make tuples between elems and compilines.

for element in data:
    if 'cc ' in element:
        data.remove(element)

data = [i for i in data if i]
data = [w.replace(':', ' ') for w in data]
data = [element.split() for element in data]
#print(data)

list_file = []

for element in data:
    for file in element:
        if not(file in list_file):
            list_file.append(file)
list_file.sort()


tab = matrix(len(list_file), len(list_file), None, list_file)

for element in data:
    for file in element[1:]:
        tab.matrix[tab.names.index(file)][tab.names.index(element[0])] = 1
if not is_print_matrix:
    tab.print_matrix()
    print()
    tab.print_dep(tab.read_matrix())
    quit(0)

#faire des tuples de materiaux x complilines
tab.create_compile_lines(save) #put the compile lines in self.compile_line

mat_x_compilines = list(zip(data, tab.compile_line))
compilines_used = []
tab.read_compiled_lines(sys.argv[2], mat_x_compilines, compilines_used)
tab.print_compilines_used(compilines_used)
quit(0)