/*  This file is part of lsnet. 

    Lsnet is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Lsnet is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with lsnet.  If not, see <http://www.gnu.org/licenses/>.
*/

//int maxtextbuffer = sizeof(char)*250;
//#define SIZE sizeof(char)*250
#define SIZE 30

int cpuinfo() {
        FILE* fp;
        int count = 0;
	int count2 = 0;
        //int coresdetected = 0;

	char buf[SIZE];
        //char cpuarray1[SIZE];

	//double cpuarray1[2][cores][8];

	//char cpuarray2[SIZE];
	//char *cpustring;
	//char *token;
	//char *line;
	//char *search = " ";

	char temp[3];
        char bufstr[SIZE];

	double mhz = 0;
        if( ( fp = fopen( "/proc/cpuinfo", "r" ) ) == NULL ) {
                fprintf( stderr, "Error opening /proc/cpuinfo.\n" );
                exit( 1 );
        }
        while( fgets(buf, sizeof(buf), fp) != NULL)
        {
		if (count == 0)
		{

			//cpu MHz		: 3223.990
			//line = buf;
			//token = strtok(line, search);
			//printf("token1: %s\n", token);
			count2=0;
                        while(buf[count2] == ' '|| buf[count2] == '\t') {
	                        count2++;
                        }

                        if (buf[count2] == 'c'){
                                count2++;
                                if (buf[count2] == 'p'){
                                        count2++;   
					if (buf[count2] == 'u'){
						count2++;
						while(buf[count2] == ' '|| buf[count2] == '\t') {
							count2++;
						}
						if (buf[count2] == 'M'){
							count2++;
							if (buf[count2] == 'H'){
								count2++;
								if (buf[count2] == 'z'){
									count2++;
//mhz token: 		:
//printf("FOUND cpu MHZ\n");
									while(buf[count2] == ' '|| buf[count2] == '\t' || buf[count2] == ':' ) {
										count2++;
									}
/*
									while(buf[count2] == ':') {
                                                                                count2++;
                                                                        }
                                                                        while(buf[count2] == ' ') { 
                                                                                count2++;
                                                                        }


*/
									//count2++;
//printf("SHOULD be getting numbers now\n");
//printf("count2: %d\n", count2);
//printf("buf: %s\n", buf);
									//for( (count2 = count2); (count2 <= strlen(line) && line[count2] != ' '); count2++){
	                                                                while((buf[count2] == '.' ||buf[count2] == '0' || buf[count2] == '1' || buf[count2] == '2' || buf[count2] == '3' || buf[count2] == '4' ||buf[count2] == '5' || buf[count2] == '6' || buf[count2] == '7'||buf[count2] == '8' || buf[count2] == '9') && (buf[count2] != ' ')) {
										memset ((void*) temp, '\0', sizeof (temp));
										temp[0]=buf[count2];
										temp[1]=0;
										strcat(bufstr,temp);
										//printf("bufstr: %s\n", bufstr);
										//printf("temp: %s\n", temp);
										count2++;
									}

									//token = strtok(NULL, search);
									//token = strtok(NULL, search);
									//token = strtok(NULL, search);
									count++;
									mhz = atof(bufstr);
//mhz token: z		:
									//memset ((void*) bufstr, '\0', sizeof (bufstr));
									//printf( "mhz token: %s\n", bufstr );
								}
							}
						}			
					}
				}
			}
								

				//printf( "mhz: %d\n", mhz );

		}
		//linenum++;			
	}
	fclose( fp );

        return (mhz);
}

