/**
 * A user-space application for generating pseudo-random specification compliant CPER records. 
 * 
 * Author: Lawrence.Tang@arm.com
 **/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../edk/Cper.h"
#include "cper-generate.h"
#include "sections/gen-section.h"

void print_help();

int main(int argc, char *argv[])
{
	//If help requested, print help.
	if (argc == 2 && strcmp(argv[1], "--help") == 0) {
		print_help();
		return 0;
	}

	//Parse the command line arguments.
	char *out_file = NULL;
	char *single_section = NULL;
	char **sections = NULL;
	UINT16 num_sections = 0;
	for (int i = 1; i < argc; i++) {
		if (strcmp(argv[i], "--out") == 0 && i < argc - 1) {
			out_file = argv[i + 1];
			i++;
		} else if (strcmp(argv[i], "--single-section") == 0 &&
			   i < argc - 1) {
			single_section = argv[i + 1];
			i++;
		} else if (strcmp(argv[i], "--sections") == 0 && i < argc - 1) {
			//All arguments after this must be section names.
			num_sections = argc - i - 1;
			sections = malloc(sizeof(char *) * num_sections);
			i++;

			for (int j = i; j < argc; j++) {
				sections[j - i] = argv[j];
			}
			break;
		} else {
			printf("Unrecognised argument '%s'. For command information, refer to 'cper-generate --help'.\n",
			       argv[i]);
			return -1;
		}
	}

	//If no output file passed as argument, exit.
	if (out_file == NULL) {
		printf("No output file provided. For command information, refer to 'cper-generate --help'.\n");
		if (sections) {
			free(sections);
		}
		return -1;
	}

	//Open a file handle to write output.
	FILE *cper_file = fopen(out_file, "w");
	if (cper_file == NULL) {
		printf("Could not get a handle for output file '%s', file handle returned null.\n",
		       out_file);
		if (sections) {
			free(sections);
		}
		return -1;
	}

	//Which type are we generating?
	if (single_section != NULL && sections == NULL) {
		generate_single_section_record(single_section, cper_file);
	} else if (sections != NULL && single_section == NULL) {
		generate_cper_record(sections, num_sections, cper_file);
	} else {
		//Invalid arguments.
		printf("Invalid argument. Either both '--sections' and '--single-section' were set, or neither. For command information, refer to 'cper-generate --help'.\n");
		if (sections) {
			free(sections);
		}
		return -1;
	}

	//Close & free remaining resources.
	fclose(cper_file);
	if (sections != NULL) {
		free(sections);
	}
}

//Prints command help for this CPER generator.
void print_help()
{
	printf(":: --out cper.file [--sections section1 ...] [--single-section sectiontype]\n");
	printf("\tGenerates a pseudo-random CPER file with the provided section types and outputs to the given file name.\n\n");
	printf("\tWhen the '--sections' flag is set, all following arguments are section names, and a full CPER log is generated\n");
	printf("\tcontaining the given sections.\n");
	printf("\tWhen the '--single-section' flag is set, the next argument is the single section that should be generated, and\n");
	printf("\ta single section (no header, only a section descriptor & section) CPER file is generated.\n\n");
	printf("\tValid section type names are the following:\n");
	for (size_t i = 0; i < generator_definitions_len; i++) {
		printf("\t\t- %s\n", generator_definitions[i].ShortName);
	}
	printf("\t\t- unknown\n");
	printf("\n:: --help\n");
	printf("\tDisplays help information to the console.\n");
}