/*  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/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include  <sys/types.h>
#include <dirent.h>
#include "cores.h"
#include "processes.h"
//#include "cpuinfo.h"
//#include "frequency.h"
#include "meminfo.h"
//#include "netinfo.h"

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

int count, count2, count3;
char *cpu = "cpu";
char *mem = "mem";
char *net = "net";

//double cpuarray1[3][64][10];
//double minutearray[61][64][9];
//double minutearray2[64][9];
//double walltime[61];
//double tenminutearray[64][9][10];

int memarray1[9];
//int memarray2[60][9];
//int memtenarray2[10][9];

//double netarray1[2][9];
//double netarray2[4];


main()
{
        FILE* fp;
        int count = 0;
        int coresdetected = 0;
        char buf[SIZE];
        char temp[10];
	char gdrun[10];
	int counter = 0;
        if( ( fp = fopen( "/etc/lsnet.conf", "r" ) ) == NULL ) {
                fprintf( stderr, "Error opening file: /etc/lsnet.conf\n" );
                exit( 1 );
        }

        while( fgets(buf, sizeof(buf), fp) != NULL)
        {
                if (buf[count] == 'R' || buf[count] == 'r'){
                        if (buf[count+1] == 'u' || buf[count+1] == 'U'){
                                if (buf[count+2] == 'n' || buf[count+2] == 'N'){
                                        if (buf[count+4] == 'G' || buf[count+4] == 'g'){ 
						if (buf[count+4] == 'D' || buf[count+4] == 'd'){
							if (buf[count+4] == 's' || buf[count+4] == 'S'){
								counter = 6;
                                                                while(buf[counter] == ' ' && counter <= strlen(buf)) {
                                                                        counter++;
                                                                }
								temp[0]=buf[counter];
								temp[1]=0;
								strcpy(gdrun, temp);
								for( (counter = counter + 1); (counter <= strlen(buf) && buf[counter] != ' '); counter++){
									temp[0]=buf[counter];
									temp[1]=0;
									strcat(gdrun,temp);
								}
								
							//}else{
							//	gdrun[0] = "0";
							//}
						}
					}
				}
			}
		}

	}
        fclose( fp );


	if ( (gdrun[0] == 'y' || gdrun[0] == 'Y') && (gdrun[1] == 'e' || gdrun[1] == 'E') && (gdrun[0] == 's' || gdrun[0] == 'S') )
	{
		system(/usr/lib/lsnet/bin/gdcpu);
		system(/usr/lib/lsnet/bin/gdcpu);
		system(/usr/lib/lsnet/bin/gdcpu);
		system(/usr/lib/lsnet/bin/gdcpu);
	}
	//Run GDs: yes

	system(rm /var/www/htdocs/lsnet/index.html);
        if( ( fp = fopen( "/etc/lsnet.conf", "a" ) ) == NULL ) { 
                fprintf( stderr, "Error opening file: /var/www/htdocs/lsnet/index.html\n" );
                exit( 1 );
        }
                                                           
        fprintf(fp, "<html>\n");
	fprintf(fp, "<head>\n");
	fprintf(fp, "<title>Lsnet Stats 0.3.0</title>\n");
	fprintf(fp, "<\/head>\n");
	fprintf(fp, "<body>\n");
	fprintf(fp, "<hr size=\"10%%\" width=\"75%%\" noshade>\n");
	fprintf(fp, "<div align=\"center\">\n");
	fprintf(fp, "<font size=\"+1\"><span style=\"color: rgb(2, 2, 255)\;font-size: 20\;\">LSNET STATS 0.3.0<br></font></span><br></div>\n");
	fprintf(fp, "<table cellpadding=\"3\" cellspacing=\"3\" border=\"3\" align=\"center\" width=\"75%%\">\n");
	fprintf(fp, "<tr>\n");
	fprintf(fp, "<td width=\"100%%\" height=\"\" align=\"center\" valign=\"center\">\n");
	fprintf(fp, "<img src=\"\/var\/www\/htdocs\/lsnet\/cpu.jpg\" width=\"1620\" height=\"955\" border=\"0\" alt=\"CPU STATS 1 DAY\">\n");
	fprintf(fp, "<img src=\"\/var\/www\/htdocs\/lsnet\/swap.jpg\" width=\"1620\" height=\"155\" border=\"0\" alt=\"SWAP STATS 1 DAY\">\n");
	fprintf(fp, "<<img src=\"\/var\/www\/htdocs\/lsnet\/mem.jpg\" width=\"1620\" height=\"255\" border=\"0\" alt=\"MEM STATS 1 DAY\">\n");
	fprintf(fp, "<img src=\"\/var\/www\/htdocs\/lsnet\/net.jpg\" width=\"1620\" height=\"255\" border=\"0\" alt=\"NET STATS 1 DAY\">\n");
	fprintf(fp, "<br>\n");
	fprintf(fp, "<\/td>\n");
	fprintf(fp, "<\/tr>\n");


	fprintf(fp, "<\/table>\n");
	fprintf(fp, "<\/body>\n");
	fprintf(fp, "<\/html>\n");
	fclose( fp );

	return 0;
}
