#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

void Usage(void){
	printf("Sequence v0.0.1\n");
	printf("Usage:\n");
	printf("sequence <-D datafile> <-I idfile> <-L label> <-O outfile> [-v/-h] [-f]\n");
	printf("\t[optional]\n");
	printf("\t<required>\n");
	printf("\tdatafile\t= file name of data/full file.\n");
	printf("\tidfile\t= file name for file containing IDs to search\n");
	printf("\tlabel\t= label to be appended to the ID in the full file\n");
	printf("\toutfile\t= backup file to load. Default to a fresh simulation\n");
	printf("\t-v/-h\t= print this help menu\n");
	printf("\t-f\t= force execution, do not prompt for continuation\n");
}

//1) full file name, 2) ID file name, 3) label to be appended to the ID in the full file.
int main(int argc, char **argv){
	if(argc < 9 || argc > 10){
		Usage();
		exit(1);
	}
	uint32_t n1 = 0;
	uint32_t n2 = 0;
	uint32_t *options = (uint32_t *)malloc( sizeof(uint32_t) * 5 );
	(void)memset((void *)options, '\0', sizeof(uint32_t) * 5 );
	//check that flags requiring an argument aren't the last option
	for(n1=1; n1<argc; n1++){
		if( strcmp( (const char *)argv[n1], "-D") == 0){
			options[0] = n1+1;
			if(n1+1 > argc-1)
				n2 = 1;
		}
		if( strcmp( (const char *)argv[n1], "-I") == 0){
			options[1] = n1+1;
			if(n1+1 > argc-1)
				n2 = 1;
		}
		if( strcmp( (const char *)argv[n1], "-L") == 0){
			options[2] = n1+1;
			if(n1+1 > argc-1)
				n2 = 1;
		}
		if( strcmp( (const char *)argv[n1], "-O") == 0){
			options[3] = n1+1;
			if(n1+1 > argc-1)
				n2 = 1;
		}
		if( strcmp( (const char *)argv[n1], "-h") == 0){
			n2 = 1;
		}
		if( strcmp( (const char *)argv[n1], "-v") == 0){
			n2 = 1;
		}
		if( strcmp( (const char *)argv[n1], "-f") == 0){
			options[4] = 1;
		}
	}
	if(n2){
		free(options);
		exit(1);
	}
	char *file_data_in  = NULL;
	char *file_ids      = NULL;
	char *append_label  = NULL;
	char *file_data_out = NULL;
	char tmp[3];

	if(options[0] != 0){
		file_data_in  = (char *)malloc( sizeof(char) * strlen(argv[options[0]]) + 1 );
		(void)strcpy(file_data_in, (const char *)argv[options[0]]);
	}
	if(options[1] != 0){
		file_ids      = (char *)malloc( sizeof(char) * strlen(argv[options[1]]) + 1 );
		(void)strcpy(file_ids, (const char *)argv[options[1]]);
	}
	if(options[2] != 0){
		append_label  = (char *)malloc( sizeof(char) * strlen(argv[options[2]]) + 1 );
		(void)strcpy(append_label, (const char *)argv[options[2]]);
	}
	if(options[3] != 0){
		file_data_out = (char *)malloc( sizeof(char) * strlen(argv[options[3]]) + 1 );
		(void)strcpy(file_data_out, (const char *)argv[options[3]]);
	}

	if(!options[4]){
		printf("You've entered the following:\n");
		printf("file_data_in: %s\n", file_data_in);
		printf("file_ids: %s\n", file_ids);
		printf("append_label: %s\n", append_label);
		printf("file_data_out: %s\n", file_data_out);
		printf("If this is correct Press \"c\"  key to continue!\n");
		printf("Press any other key to exit!\n");
		if(fgets(tmp, 2, stdin) == NULL){
			printf("An error occured, exiting!\n");
			free(options);
			free(file_data_in);
			free(file_ids);
			free(append_label);
			free(file_data_out);
			exit(1);
		}
		//printf("%c %d\n", tmp[0], tmp[0]);
		if(tmp[0] != 99){
			printf("Exiting\n");
			free(options);
			free(file_data_in);
			free(file_ids);
			free(append_label);
			free(file_data_out);
			exit(1);
		}
	}

	free(options);
	printf("Beginning Search!\n");
	FILE *fp_data_in  = fopen( file_data_in, "r" )
	if(fp_data_in  == NULL){
		printf("Error Opening %s Exiting\n", file_data_in);
		free(file_data_in);
		free(file_ids);
		free(append_label);
		free(file_data_out);
		exit(1);
	}
	FILE *fp_ids      = fopen( file_ids, "r" )
	if(fp_ids      == NULL){
		printf("Error Opening %s Exiting\n", fp_ids);
		fclose(fp_data_in);
		free(file_data_in);
		free(file_ids);
		free(append_label);
		free(file_data_out);
		exit(1);
	}
	FILE *fp_data_out = fopen( file_data_out, "w" )
	if(fp_data_out == NULL){
		printf("Error Opening %s Exiting\n", fp_data_out);
		fclose(fp_data_in);
		fclose(fp_ids);
		free(file_data_in);
		free(file_ids);
		free(append_label);
		free(file_data_out);
		exit(1);
	}
	uint32_t read_buffer_size = strlen("@HWUSI-EAS690_0003:6:1:1044:17226#0/1");
	char *read_buffer = (char *)malloc(read_buffer_size + 1);
	(void)memset( (void *)read_buffer, '\0', read_buffer_size + 1);
	//@HWUSI-EAS690_0003:6:1:1044:17226#0/1
	//GAACTGAGAGATTCCACTGGCCTTAGTGGATAGNNNNCNAGAATGTTGCNAAATATCCNNCNNNANNNNNNNNNNNNNNNNNNNNN
	//@HWUSI-EAS690_0003:6:1:1044:17226#0/1

	//
	while( fgets(read_buffer, read_buffer_size, fp_ids) != NULL){


	}
	printf("Exiting\n");
	fclose(fp_data_in);
	fclose(fp_ids);
	fclose(fp_data_out);
	//free(options);
	free(file_data_in);
	free(file_ids);
	free(append_label);
	free(file_data_out);
	return 0;
}