#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>


void title() {
        printf("\tThis is a product of the linuxsociety team and licensed under GPLv3\n");
        printf("\tThis is version 0.0.1 dated 08/12/2009\n");
        printf("\n");
return;
}

void usage() {
        printf("\n");
	printf("Usage options:\n");
	printf("lotto [option] [choice]\n\n");
	printf("Options:\n");
	printf("mega\n");
	printf("super\n\n");

	printf("Choices:\n");
	printf("random\n");
	printf("most\n");
	printf("least\n");
	printf("\n\n");
	
	printf("For everything\n");
	printf("lotto all\n\n");
}

int k;
//void Random(int k);
//void Most(int k);
//void Least(int k);


int main(int argc, char **argv)
{
	int i; 
	int j=0;
	//int k=0;
	//double percentage1;
	//double rate1;
	//double percentage2;
	//double variable1;
	char option[10];

	//strcpy (option, argv[i]);

	if(argc == 2){
	        for(i=0;i<argc;i++)
        	{
			strcpy (option, argv[i]);
			if (option[0] == 'a' || option[0] == 'A'){
				if (option[1] == 'l' || option[1] == 'L'){
					if (option[2] == 'l' || option[2] == 'L'){

						printf("\n\t\tNumbers:\tMega:\n\n");
						
						printf("MegaMillions\n");
						//printf("\tNumbers:\tMega:\n");
						//printf("\tRandom\n");
	                                	Random(1);
						//printf("\tMost\n");
						Most(1);
						//printf("\tLeast\n");
						Least(1);

						printf("\nSuperLotto\n");
						//printf("\tNumbers:\tMega:\n");
						//printf("\tRandom\n");
						Random(2);
						//printf("\tMost\n");
						Most(2);
						//printf("\tLeast\n");
						Least(2);

						return 0;
					}
				}
			}
		}
		usage();
		return 0;
	}



	if(argc != 3){
		printf("\nError, incorrect usage!\n\n");

		usage();
		return 0;
	}


	for(i=0;i<argc;i++)
	{


		if(i==1){
			strcpy (option, argv[i]);
                        if (option[0] == 'm'){
                                if (option[1] == 'e'){
                                        if (option[2] == 'g'){
                                                if (option[3] == 'a'){
                                                        k=1;
                                                }
                                        }
                                }
                        }
			//printf("i: %d\n", i);

                        if (option[0] == 's'){
                                if (option[1] == 'u'){
                                        if (option[2] == 'p'){
                                                if (option[3] == 'e'){
                                                        k=2;
                                                }
                                        }
                                }
                        }
		}
		//printf("i: %d\n", i);


		if(i==2){
			//printf("i2: %d\n", i);
			//percentage1 = atof(argv[i]);
			//printf("argv: %s\n", argv[i]);
        		strcpy (option, argv[i]);   
        		//printf("option: %s\n", option);

			//JUST USE strcmp instead ....
			//for (j=0; j<6; j++){
			if (option[0] == 'r'){	
				if (option[1] == 'a'){
					if (option[2] == 'n'){
						if (option[3] == 'd'){
							j=1;
						}
					}
				}
			}

                        if (option[0] == 'm'){
                                if (option[1] == 'o'){
                                        if (option[2] == 's'){
                                                if (option[3] == 't'){
                                                        j=2;
                                                }
                                        }
                                }
                        }  

                        if (option[0] == 'l'){
                                if (option[1] == 'e'){
                                        if (option[2] == 'a'){
                                                if (option[3] == 's'){
							if (option[4] == 't'){
                                                        	j=3;
							}
                                                }
                                        }
                                }
                        }  



			if(j==1){
				//printf("Random, megamillions\n");
				Random(k);
			}else if(j==2){
				//printf("Most, megamillions\n");
				Most(k);
			}else if(j==3){
				//printf("Least, megamillions\n");
				Least(k);
			}else{
				printf("\nInvalid option!\n");
				usage();
				return 0;
			}


		}
	
	}


	

	return 0;
}


void Random(int k){
	//printf("\nRandom\n\n");

	int count3, count4;
        if(k==1){
                //runtype="mega.txt";
                count3=56;
                count4=46;
        }
        if(k==2){
                //runtype="super.txt";
                count3=47;
                count4=27;
        }


	int rand1[7];
	int i;

	/* initialize random seed: */
	srand ( time(NULL) );

	for(i=0; i<5; i++){
		//while(rand1[i] != 
		rand1[i] = rand() % count3 + 1;
		//HAS to be a CLEANER way
		while(rand1[i] == rand1[i-1] ||rand1[i] == rand1[i-2] ||rand1[i] == rand1[i-3] ||rand1[i] == rand1[i-4] && i != 0){
			rand1[i] = rand() % count3 + 1;
		} 
	}
	rand1[5] = rand() % count4 + 1;
	
	//printf("Numbers:\tMega:\n");
	printf("\tRandom:\t%d %d %d %d %d\t%d\n", rand1[0], rand1[1], rand1[2], rand1[3], rand1[4], rand1[5]); 

return;
}


void Most(int k){
	char linestring[100];
	char copystr[100];
	int line=0;
	int count1=0;
	int count2=0;
	int count3=0;
	int count4=0;
	char temp[2];
	char buf[sizeof(temp)*12];
	int numbers[110];
	//int megas[100];
	int result[10];
	memset ((void*) numbers, '\0', sizeof(numbers));
	//memset ((void*) megas, '\0', sizeof(megas));
	memset ((void*) result, '\0', sizeof(result));
        //printf("\nMost often chosen numbers\n\n");
	char *runtype;

	if(k==1){
		runtype="mega.txt";
		count3=56;
		count4=46;
	}
	if(k==2){
		runtype="super.txt";
		count3=47;
		count4=27;
	}
     
        FILE* fp;
        if( ( fp = fopen( runtype, "r" ) ) != NULL ) {
	        while( fgets(linestring, sizeof(linestring), fp) != NULL)
	        {
			//line++;
			//printf("%d - %s\n", line, linestring);
			memset ((void*) buf, '\0', sizeof(buf));
			//strcpy(copystr, linestring);
			//linestring[strlen(linestring)-1]='\0';
			if(line<count3){
				//printf("%d - %s\n", line, linestring);
				count2 = 0;
				while(linestring[count2] != ' ' && linestring[count2] != NULL) {
					temp[0]=linestring[count2];
					temp[1]='\0';
					strcat(buf,temp);
					count2++;					

				}


				numbers[line]=atoi(buf); 		
				//line++;
				//printf("%d\n", numbers[line]);
			}
			if(line>count3-1){
				//printf("%d - %s\n", line, linestring);
				count2 = 0;
                                while(linestring[count2] != ' ' && linestring[count2] != NULL) {
                                        temp[0]=linestring[count2];
                                        temp[1]='\0';
                                        strcat(buf,temp);
                                        count2++;
                                 
                                }


				numbers[line]=atoi(buf);
				//printf("%d\n", numbers[line]);
				//line++;
			}	
			line++;			
			
		}	

        }else{
	        fprintf( stderr, "\tError: file %s not accessible!\n", runtype );
                return 0;
        }
        fclose( fp );
/*
	line = 0;
	while( line < count3){
		//printf("%d\n", numbers[line]);
		line++;
	}

	printf("\n\n");
	line = count3;
        while(line < (count3+count4+1)){
                //printf("%d\n", numbers[line]);
		line++;
        }

*/
	
	//gets the most popular number
	count1 = 0;
	for(line=0; line<count3; line++){
		if(numbers[line]>count1){
			result[0] = line+1;
			count1 = numbers[line];
		}
	}

        count1 = 0;
        for(line=0; line<count3; line++){
                if(numbers[line]>count1 && line != result[0]-1){  
                        result[1] = line+1;
                        count1 = numbers[line];
                }
        }

        count1 = 0;
        for(line=0; line<count3; line++){
                if(numbers[line]>count1 && line != result[0]-1  && line != result[1]-1){  
                        result[2] = line+1;
                        count1 = numbers[line];
                }
        }
        count1 = 0;
        for(line=0; line<count3; line++){
                if(numbers[line]>count1 && line != result[0]-1  && line != result[1]-1 && line != result[2]-1){  
                        result[3] = line+1;
                        count1 = numbers[line];
                }
        }
        count1 = 0;
        for(line=0; line<count3; line++){
                if(numbers[line]>count1 && line != result[0]-1  && line != result[1]-1 && line != result[2]-1 && line != result[3]-1){  
                        result[4] = line+1;
                        count1 = numbers[line];
                }
        }


        count1 = 0;
        for(line=count3; line<(count3+count4-1); line++){    
                if(numbers[line]>count1){
                        result[5] = line-(count3-1);
                        count1 = numbers[line];
                }
        }
	//printf("Numbers:\tMega:\n");
	printf("\tMost:\t%d %d %d %d %d\t%d\n", result[0], result[1], result[2], result[3], result[4], result[5]);
                                
return;                              
}         


void Least(int k){
        //printf("\nLeast often chosen numbers\n\n");     
        char linestring[100];                           
        char copystr[100];
        int line=0;
        int count1=0;
        int count2=0;
        int count3=0;
        int count4=0;
        char temp[2];
        char buf[sizeof(temp)*12]; 
        int numbers[110];
        //int megas[100];
        int result[10];
        memset ((void*) numbers, '\0', sizeof(numbers));        
        //memset ((void*) megas, '\0', sizeof(megas));
        memset ((void*) result, '\0', sizeof(result));

        char *runtype;
        
        if(k==1){
                runtype="mega.txt";
                count3=56;
                count4=46;
        }
        if(k==2){
                runtype="super.txt";
                count3=47;
                count4=27;
        }



        FILE* fp;
        if( ( fp = fopen( runtype, "r" ) ) != NULL ) {
                while( fgets(linestring, sizeof(linestring), fp) != NULL)
                {
                        //line++;
                        //printf("%d - %s\n", line, linestring);
                        memset ((void*) buf, '\0', sizeof(buf));
                        //strcpy(copystr, linestring);
                        //linestring[strlen(linestring)-1]='\0';
                        if(line<count3){
                                //printf("%d - %s\n", line, linestring);
                                count2 = 0;
                                while(linestring[count2] != ' ' && linestring[count2] != NULL) {
                                        temp[0]=linestring[count2];
                                        temp[1]='\0';    
                                        strcat(buf,temp);
                                        count2++;

                                }
                
                 
                                numbers[line]=atoi(buf);
                                //line++;
                                //printf("%d\n", numbers[line]);
                        }
                        if(line>(count3-1)){
                                //printf("%d - %s\n", line, linestring);
                                count2 = 0;
                                while(linestring[count2] != ' ' && linestring[count2] != NULL) {
                                        temp[0]=linestring[count2];
                                        temp[1]='\0';
                                        strcat(buf,temp);
                                        count2++;
                                        
                                }
                                 
                                
                                numbers[line]=atoi(buf);
                        }
                        line++;
        
                }
         
        }else{  
                fprintf( stderr, "\tError: file %s not accessible!\n", runtype );
                return 0;
        }
        fclose( fp );

/*
        line = 0;  
        while( line < count3){
                //printf("%d\n", numbers[line]);
                line++;
        }
                 
        printf("\n\n");
        line = count3;
        while(line < (count3+count4+1)){
                //printf("%d\n", numbers[line]);
                line++;
        }
*/
        //gets the least popular number
        count1 = 1000;
        for(line=0; line<count3; line++){
                if(numbers[line]<count1){
                        result[0] = line+1;
                        count1 = numbers[line];
                }
        }
         
        count1 = 1000;
        for(line=0; line<count3; line++){
                if(numbers[line]<count1 && line != result[0]-1){
                        result[1] = line+1;
                        count1 = numbers[line];
                }
        }
         
        count1 = 1000;
        for(line=0; line<count3; line++){
                if(numbers[line]<count1 && line != result[0]-1  && line != result[1]-1){
                        result[2] = line+1;
                        count1 = numbers[line];
                }
        }
        count1 = 1000;
        for(line=0; line<count3; line++){
                if(numbers[line]<count1 && line != result[0]-1  && line != result[1]-1 && line != result[2]-1){
                        result[3] = line+1;
                        count1 = numbers[line];
                }
        }
        count1 = 1000;
        for(line=0; line<count3; line++){
                if(numbers[line]<count1 && line != result[0]-1  && line != result[1]-1 && line != result[2]-1 && line != result[3]-1){
                        result[4] = line+1;
                        count1 = numbers[line];
                }
        }
        count1 = 1000;
        for(line=count3; line<(count3+count4-1); line++){
                if(numbers[line]<count1){
                        result[5] = line-(count3-1);
                        count1 = numbers[line];
                }
        }

	//printf("Numbers:\tMega:\n");         
        printf("\tLeast:\t%d %d %d %d %d\t%d\n", result[0], result[1], result[2], result[3], result[4], result[5]);
                            
return;                               
}         




/*
int main(int argc, char *argv[]) {
	int blank;
	int num1;

	num1 = atoi(argv);
	printf("argv: %d\n", num1);
	strcat (option, argv);
	printf("argv: %d\n", num1);

}
*/
