// File: /workspace/github_c_makefile_star2_O2/System-Glitch[P]ESGILinter-C/src/main.c
// Function: main
int main(int argc, char **argv) {
	char *path = NULL;
	char *file = NULL;
	arraylist_t *conf = arraylist_init(ARRAYLIST_DEFAULT_CAPACITY);
	arraylist_t *files = arraylist_init(ARRAYLIST_DEFAULT_CAPACITY);
	arraylist_t *buffer = NULL;
	arraylist_t *real_file = NULL;
	arraylist_t *lines = NULL;
	scope_t *scope = NULL;
	rule_t *rule = NULL;

	if(conf == NULL || files == NULL) {
		printf("%s[ERROR]%s %s%s\n", COLOR_RED, COLOR_YELLOW, strerror(errno), FORMAT_RESET);
		return EXIT_FAILURE;
	}

	path = malloc(sizeof(char) * 255);
	if(argc >= 2) {
		strcpy(path, argv[1]);
	} else {
		strcpy(path, ".");
	}

	if(load_configuration("linter.conf", conf)) { //replace with argument
		printf("%s[ERROR]%s Failed to load config: %s%s\n", COLOR_RED, COLOR_YELLOW, strerror(errno), FORMAT_RESET);
		arraylist_free(conf, 0);
		return EXIT_FAILURE;
	}

	search_files(conf, files, path);

	for(int i = 0; i < files->size; i++){
		buffer = arraylist_init(ARRAYLIST_DEFAULT_CAPACITY);
		real_file = arraylist_init(ARRAYLIST_DEFAULT_CAPACITY);

		if(buffer == NULL || real_file == NULL) {
			printf("%s[ERROR]%s %s%s\n", COLOR_RED, COLOR_YELLOW, strerror(errno), FORMAT_RESET);
			return EXIT_FAILURE;
		}

		file = arraylist_get(files, i);
		printf("Checking file: %s\n", file);
		file_loader(buffer/*, files*/, real_file, file);

		lines = get_lines_list(buffer);
		scope = parse_root_scope(lines);
		arraylist_free(lines, 1);

		if(scope != NULL) {
			if(check_rule(conf, "max-file-line-numbers")) {
				rule = get_rule(conf, "max-file-line-numbers");
				max_file_line_numbers(real_file, rule->value, file);
			}
			if(check_rule(conf, "max-line-numbers")) {
				rule = get_rule(conf, "max-line-numbers");
				check_max_line_length(real_file, rule->value, file);
			}
			if(check_rule(conf, "comments-header")) {
				check_comments_header(real_file, file);
			}
			if(check_rule(conf, "indent")) {
				rule = get_rule(conf, "indent");
				check_indent(real_file, rule->value, file);
			}
			if(check_rule(conf, "no-trailing-space")) {
				no_trailing_spaces(real_file, file);
			}
			if(check_rule(conf, "array-bracket-eol")) {
				array_bracket_eol(buffer, real_file);
			}
			if(check_rule(conf, "comma-spacing")) {
				comma_spacing(buffer, real_file);
			}
			if(check_rule(conf, "no-prototype")) {
				check_no_prototype(scope, buffer, real_file);
			}
			if(check_rule(conf, "no-multi-declaration")) {
				check_no_multi_declaration(scope, buffer, real_file);
			}
			parse_and_check(scope, buffer, NULL, NULL, conf, real_file);
		} else {
			printf("%s[WARNING]%s Scope parsing failed for %s%s\n", COLOR_RED, COLOR_YELLOW, FORMAT_RESET, file);
		}

		/*
		 *
		 * TREAT DATA FROM HERE
		 *
		 * APPLY RULES
		 *
		 */
		arraylist_free(real_file, 1);
		free_buffer(buffer);
	}
	arraylist_free(files, 1);

	free_conf(conf);
    free(path);
	return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movl %edi, %r15d
pushq $0xa
popq %rbx
movl %ebx, %edi
callq 0x67e4
movq %rax, %r12
movl %ebx, %edi
callq 0x67e4
testq %r12, %r12
sete %dl
movq %rax, -0x30(%rbp)
testq %rax, %rax
sete %cl
orb %dl, %cl
cmpb $0x1, %cl
jne 0xc34e
callq 0x2050
movl (%rax), %edi
callq 0x21a0
leaq 0xcda(%rip), %rdi       # 0xd007
leaq 0xce5(%rip), %rsi       # 0xd019
leaq 0xce4(%rip), %rdx       # 0xd01f
leaq 0xce3(%rip), %r8        # 0xd025
movq %rax, %rcx
xorl %eax, %eax
callq 0x20d0
jmp 0xc3c4
movl $0xff, %edi
callq 0x2130
cmpl $0x2, %r15d
movq %rax, -0x38(%rbp)
jl 0xc370
movq 0x8(%r14), %rsi
movq %rax, %rdi
callq 0x2080
jmp 0xc375
movw $0x2e, (%rax)
leaq 0x2937(%rip), %rdi      # 0xecb3
movq %r12, %rsi
callq 0x56a0
testb %al, %al
je 0xc3da
callq 0x2050
movl (%rax), %edi
callq 0x21a0
leaq 0x2924(%rip), %rdi      # 0xecbf
leaq 0xc77(%rip), %rsi       # 0xd019
leaq 0xc76(%rip), %rdx       # 0xd01f
leaq 0xc75(%rip), %r8        # 0xd025
movq %rax, %rcx
xorl %eax, %eax
callq 0x20d0
movq %r12, %rdi
xorl %esi, %esi
callq 0x69f4
pushq $0x1
popq %r15
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x283f
pushq $0x1
popq %r15
xorl %ebx, %ebx
movq %r12, -0x40(%rbp)
movq -0x30(%rbp), %rax
cmpq %rbx, 0x8(%rax)
jbe 0xc676
pushq $0xa
popq %r14
movl %r14d, %edi
callq 0x67e4
movq %rax, %r13
movl %r14d, %edi
callq 0x67e4
testq %r13, %r13
je 0xc69b
movq %rax, %r14
testq %rax, %rax
je 0xc69b
movq -0x30(%rbp), %rdi
movq %rbx, -0x50(%rbp)
movq %rbx, %rsi
callq 0x6978
movq %rax, %r15
leaq 0x28a0(%rip), %rdi      # 0xece8
movq %rax, %rsi
xorl %eax, %eax
callq 0x20d0
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x2312
movl 0x8(%r13), %edi
callq 0x67e4
movq %rax, %r12
xorl %ebx, %ebx
cmpq 0x8(%r13), %rbx
jae 0xc497
movq %r13, %rdi
movq %rbx, %rsi
callq 0x6978
movq (%rax), %rdi
callq 0x794d
movq %r12, %rdi
movq %rax, %rsi
callq 0x68b5
incq %rbx
jmp 0xc46e
movq %r12, %rdi
callq 0x5f9a
movq %rax, %rbx
movq %r12, %rdi
pushq $0x1
popq %rsi
callq 0x69f4
testq %rbx, %rbx
je 0xc629
movq %rbx, -0x48(%rbp)
movq -0x40(%rbp), %r12
movq %r12, %rdi
leaq 0x2833(%rip), %rbx      # 0xecfb
movq %rbx, %rsi
callq 0x5eca
testb %al, %al
je 0xc4ed
movq %r12, %rdi
movq %rbx, %rsi
callq 0x5d9f
movl 0xc(%rax), %esi
movq %r14, %rdi
movq %r15, %rdx
callq 0x8d68
movq %r12, %rdi
leaq 0x281a(%rip), %rbx      # 0xed11
movq %rbx, %rsi
callq 0x5eca
testb %al, %al
je 0xc51c
movq %r12, %rdi
movq %rbx, %rsi
callq 0x5d9f
movl 0xc(%rax), %esi
movq %r14, %rdi
movq %r15, %rdx
callq 0x96b0
movq %r12, %rdi
leaq 0x27fc(%rip), %rsi      # 0xed22
callq 0x5eca
testb %al, %al
je 0xc53a
movq %r14, %rdi
movq %r15, %rsi
callq 0xa3c0
movq %r12, %rdi
leaq 0x27ee(%rip), %rbx      # 0xed32
movq %rbx, %rsi
callq 0x5eca
testb %al, %al
je 0xc569
movq %r12, %rdi
movq %rbx, %rsi
callq 0x5d9f
movl 0xc(%rax), %esi
movq %r14, %rdi
movq %r15, %rdx
callq 0x8ffc
movq %r12, %rdi
leaq 0x27c6(%rip), %rsi      # 0xed39
callq 0x5eca
testb %al, %al
je 0xc587
movq %r14, %rdi
movq %r15, %rsi
callq 0x8a14
movq %r12, %rdi
leaq 0x27ba(%rip), %rsi      # 0xed4b
callq 0x5eca
testb %al, %al
movq -0x50(%rbp), %rbx
je 0xc5a9
movq %r13, %rdi
movq %r14, %rsi
callq 0x8ad4
movq %r12, %rdi
leaq 0x27aa(%rip), %rsi      # 0xed5d
callq 0x5eca
testb %al, %al
je 0xc5c7
movq %r13, %rdi
movq %r14, %rsi
callq 0x93d8
movq %r12, %rdi
leaq 0x279a(%rip), %rsi      # 0xed6b
callq 0x5eca
testb %al, %al
je 0xc5e9
movq -0x48(%rbp), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x94e8
movq %r12, %rdi
leaq 0x2785(%rip), %rsi      # 0xed78
callq 0x5eca
testb %al, %al
movq -0x48(%rbp), %r15
je 0xc60e
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x8e24
movq %r15, %rdi
movq %r13, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r12, %r8
movq %r14, %r9
callq 0x97e4
pushq $0x1
popq %r15
jmp 0xc65b
leaq 0x275d(%rip), %rdi      # 0xed8d
leaq 0x9e2(%rip), %rsi       # 0xd019
leaq 0x9e1(%rip), %rdx       # 0xd01f
leaq 0x9e0(%rip), %rcx       # 0xd025
movq %r15, %r8
xorl %eax, %eax
callq 0x20d0
movq -0x40(%rbp), %r12
pushq $0x1
popq %r15
movq -0x50(%rbp), %rbx
movq %r14, %rdi
movl %r15d, %esi
callq 0x69f4
movq %r13, %rdi
callq 0x2a37
incq %rbx
jmp 0xc3f4
pushq $0x1
popq %rsi
movq -0x30(%rbp), %rdi
callq 0x69f4
movq %r12, %rdi
callq 0x5e7f
movq -0x38(%rbp), %rdi
callq 0x2030
xorl %r15d, %r15d
jmp 0xc3c8
callq 0x2050
movl (%rax), %edi
callq 0x21a0
leaq 0x959(%rip), %rdi       # 0xd007
leaq 0x964(%rip), %rsi       # 0xd019
leaq 0x963(%rip), %rdx       # 0xd01f
leaq 0x962(%rip), %r8        # 0xd025
movq %rax, %rcx
xorl %eax, %eax
callq 0x20d0
jmp 0xc3c8