/*  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 <gd.h>
#include "gdfontl.h"
#include "gdfontmb.h"
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include "dates.h"
#include "meminfo.h"
//#include "doubletoint.h"
#include "gdfonts.h"
#include "gdfontt.h"
#include "gdfontl.h"
//gdfontt.h", "gdfonts.h", "gdfontmb.h", "gdfontl.h" and "gdfontg.h"


//int error()
//{
//	printf("Error, out of bounds exception!");
//}


int memarray1[9];

int main() {
	gdImagePtr im; 
	FILE *out; 
	int black,white,red,blue,green,yellow,orange,purple,brown,grey;
	FILE* lsmemlog;  

	im = gdImageCreate(1620,255); 

	white = gdImageColorAllocate(im, 255, 255, 255);
	black = gdImageColorAllocate(im, 0, 0, 0); 
	//white = gdImageColorAllocate(im, 255, 255, 255);	
	red = gdImageColorAllocate(im, 255, 0, 0);
	blue = gdImageColorAllocate(im, 0, 0, 255);
	green = gdImageColorAllocate(im, 0, 255, 0);
	yellow = gdImageColorAllocate(im, 255, 255, 0);
	orange = gdImageColorAllocate(im, 255, 128, 0);
	purple = gdImageColorAllocate(im, 255, 0, 255);
	brown = gdImageColorAllocate(im, 128, 64, 0);
	grey = gdImageColorAllocate(im, 128, 128, 128);

        int styleDashed[8];
        styleDashed[0] = blue;
        styleDashed[1] = blue;
        styleDashed[2] = orange;
        styleDashed[3] = orange;
        styleDashed[4] = gdTransparent;
        styleDashed[5] = yellow;
        styleDashed[6] = gdTransparent;
        styleDashed[7] = yellow;
        
        gdImageSetStyle(im, styleDashed, 8);



	////declare variables

        char *token;
        char *line;
        char *search = " ";

	char linestring[1000];
	char copystr[1000];
        char hourstr[3];
        char minstr[3];
        char secstr[3];
        char labeldate2[12]; 
	char temp[10];
	char labelstring[10];
	char datestrcurrent[30];	
	char label1[10];
	//char label2[10];
	//char label3[10];	
	//char label4[10];

	int i;
	int lastline;
	int isam;
	int minsince;
	int minsince2;
	//int minsince3;
	int lastminsince;
	
	int hour, min, sec;
	int labeldate1;
	int counter, counter2, counter3;
	int modulo4;
	int coordinate = 60;
	int emptyvarstr;
        double difference = 0;
        int lastdate = 0;  
        int stagger = 230; 

	double num3 = 210; 
	double num4, num5;
	double memsize;
	double memfree;
	double memused;
	double cached;
	double buffers;

	//coordinate
	double downnum = 210;
	//points
	int num6 = 0;

	meminfo(1,memarray1);
	
	
        struct tm *local;
        time_t t;

	memsize = memarray1[1] / 1024;

	int tenth = memsize / 10; 

	//////////////

	emptyvarstr = sprintf( labelstring, "%d", num6);


	gdImageLine(im, 50, 10,50,210, black);
	gdImageLine(im, 10, 210,1500,210, black);
	gdImageString(im, gdFontMediumBold,   5   ,   5        , "MB" , black);
	gdImageString(im, gdFontMediumBold,   1520   ,  200        , "Memory Usage" , black);
	gdImageString(im, gdFontMediumBold,   50   ,   240    , "Hours" , black);
	
	for (i = 0; i < 11; ++i)
	{
		gdImageString(im, gdFontSmall, 20 , num3 , labelstring , black);
		//gdImageLine(im, 25, num3, 30 , num3 , black);
		gdImageLine(im, 25, num3, 1500 , num3 , gdStyled);

        	num6 = num6 + tenth;
        	num3 = num3 - 20;
		emptyvarstr = sprintf( labelstring, "%d", num6);
	}

        if( ( lsmemlog = fopen( "/usr/lib/lsnet/lsmem.log", "r" ) ) == NULL ) {
                fprintf( stderr, "Error opening /usr/lib/lsnet/lsmem.log\n" );
                exit( 1 );
        }

        t = time(NULL);
        local = localtime(&t);
        strcat(datestrcurrent, asctime(local));
        datestrcurrent[strlen(datestrcurrent)-1]='\0';
	minsince2 = dates(datestrcurrent);
	//lastminsince = minsince;
	i = 1440;
	//Tue Feb 24 08:32:44 2009 2075412 405356 710596 249184 1670056 10980276 10604964 375312
        while( fgets(linestring, sizeof(linestring), lsmemlog) != NULL)
        {
			strcpy(copystr, linestring);		
			minsince = dates(copystr);
			if (i == 1440)
			{
				lastminsince = minsince;
			}
			if (minsince >= (minsince2 - 1439) && coordinate <= 1500)
			//if (minsince >= minsince2 - 1440 && coordinate <= 1500)
			{ 
				if (i >= 0)
				{
					//memsize, memfree, buffers, cached, memused
					strcpy(copystr, linestring);
					token = strtok(copystr, search);
					token = strtok(NULL, search);
					token = strtok(NULL, search);
					token = strtok(NULL, search);
					
					temp[0]=token[0];
					temp[1]=token[1];
					temp[2]=0;
					hour = atoi(&temp);

					temp[0]=token[3];
					temp[1]=token[4];
					temp[2]=0;
                        		min = atoi(&temp);

					temp[0]=token[6];
					temp[1]=token[7];
					temp[2]=0;
                        		sec = atoi(&temp);
					
					token = strtok(NULL, search);
                                        token = strtok(NULL, search);
					memsize = atof(token);
                                        token = strtok(NULL, search);
					memfree = atof(token);
                                        token = strtok(NULL, search);
					buffers = atof(token);
                                        token = strtok(NULL, search);
					cached = atof(token);
                                        token = strtok(NULL, search);
					memused = atof(token);     

                        		//#total used 
                        		num3 = memused / memsize;
                        		num3 = num3 * 200;
                        		num3 = 200 - num3;
                        		num3 = num3 + 10;
                        
                        		//#cached
                        		num4 = cached / memsize;
                        		num4 = num4 * 200;
                        		num4 = 200 - num4;
                        		num4 = num4 + 10;
                        
                        		//#buffers used
                        		num5 = buffers + cached;
                        		num5 = num5 / memsize;
                        		num5 = num5 * 200;
                        		num5 = 200 - num5;
                        		num5 = num5 + 10;
					
                                	//if (minsince > minsince2 - 1440)
                                	//{
                                        	difference = minsince - lastminsince;
						//printf("difference: %lf\n", difference);
						difference = ceil(difference);  
                                		//printf("Difference: %d Minsince: %d Lastminsince: %d\n", difference, minsince, lastminsince);
                                        	if (difference >= 2)
                                        	{
                                                	//difference = lastminsince - minsince;
							//printf("minsince: %d lastminsince: %d\n", minsince, lastminsince);
							//printf("coordinate before adj: %d\n", coordinate);
                                                	//coordinate = coordinate + 
							//difference = ceil(difference);
							//printf("after: %d difference: %lf difference c: %lf\n", coordinate, difference, ceil(difference));
                                                	//#leave a blank line for each 1min lost
                                                	for (counter3 = 1; counter3 < difference; counter3++)
                                                	{
                                                        	coordinate++;
                                                	}
							//printf("after: %d difference: %lf difference c: %lf\n", coordinate, difference, ceil(difference));
                                        	}
                                	//}

					//if multiple of 60 drawl date labels
                                        modulo4 = coordinate % 60;
                                        if (modulo4 == 0)
                                        {
                                        	labeldate1 = hourhash(hour);
                                        	isam = ampm(hour);
                                                if (labeldate1 < 10)
                                                {
                                                	emptyvarstr = sprintf( hourstr, "0%d", labeldate1);
                                                }else{   
                                                	emptyvarstr = sprintf( hourstr, "%d", labeldate1);
                                                }
                                                if (min < 10)
                                                {
                                                	emptyvarstr = sprintf( minstr, "0%d", min);
                                                }else{
                                                	emptyvarstr = sprintf( minstr, "%d", min);
                                                }
                                                if (sec < 10)
                                                {
                                                	emptyvarstr = sprintf( secstr, "0%d", sec);
                                                }else{
                                                	emptyvarstr = sprintf( secstr, "%d", sec);
                                                }

                                        	if (isam == 1)
                                        	{
							emptyvarstr = sprintf( labeldate2, "%s:%s:%s AM", hourstr, minstr, secstr);
                                         	}else{
							emptyvarstr = sprintf( labeldate2, "%s:%s:%s PM", hourstr, minstr, secstr);
                                         	}
                                         	if (stagger == 230)
                                         	{
                                               		stagger = 220;
                                         	}else{
                                                 	stagger = 230;
                                         	}
						gdImageString(im, gdFontSmall,   coordinate - 30   ,  stagger  , labeldate2 , black);
						gdImageLine(im, coordinate, 210, coordinate , stagger, black);
                                	}

                                        //#total used
					gdImageLine(im, coordinate, num3,coordinate,210, red);
                                        //#buffers
					gdImageLine(im, coordinate, num4,coordinate,num5, yellow);
                                        //#cached
					gdImageLine(im, coordinate, 210,coordinate,num4, orange);
                                        //#free
					gdImageLine(im, coordinate, 10,coordinate,num3, green);

					coordinate++;
					lastminsince = minsince;
					i--;
				}
			}	
	}

	gdImageString(im, gdFontSmall,   1540   ,   10  , "Free" , black);
       	gdImageString(im, gdFontSmall,   1540   ,   60  , "Other" , black);
        gdImageString(im, gdFontSmall,   1540   ,   110  , "Buffers" , black);
        gdImageString(im, gdFontSmall,   1540   ,   160  , "Cached" , black);

	emptyvarstr = sprintf( label1, "%.02lfMB", memfree / 1024);
	gdImageString(im, gdFontSmall,   1540   ,   25  , label1 , black);
	emptyvarstr = sprintf( label1, "%.02lfMB", (memused - buffers - cached) / 1024);
	gdImageString(im, gdFontSmall,   1540   ,   75  , label1 , black);
	emptyvarstr = sprintf( label1, "%.02lfMB", buffers / 1024);
	gdImageString(im, gdFontSmall,   1540   ,   125  , label1 , black);
	emptyvarstr = sprintf( label1, "%.02lfMB", cached / 1024);
	gdImageString(im, gdFontSmall,   1540   ,   175  , label1 , black);

	//gdImageString(im, gdFontSmall,   1540   ,   25  , label1 , black);
	//gdImageString(im, gdFontSmall,   1540   ,   75  , label2 , black);
	//gdImageString(im, gdFontSmall,   1540   ,   125  , label3 , black);
	//gdImageString(im, gdFontSmall,   1540   ,   175  , label4 , black);


        gdImageLine(im, coordinate, 10, 1540 , 10, green);
        gdImageLine(im, coordinate, num3, 1540 , 60, red);
       	gdImageLine(im, coordinate, num5, 1540 , 110, yellow);
       	gdImageLine(im, coordinate, num4, 1540 , 160, orange);

//plot date/time for last entry
        if (modulo4 != 0)
        {
        	labeldate1 = hourhash(hour);
        	isam = ampm(hour);
        	if (labeldate1 < 10)
        	{                        
        		emptyvarstr = sprintf( hourstr, "0%d", labeldate1);
        	}else{
        		emptyvarstr = sprintf( hourstr, "%d", labeldate1);
        	}                        
        	if (min < 10)
        	{                        
        		emptyvarstr = sprintf( minstr, "0%d", min);
        	}else{
        		emptyvarstr = sprintf( minstr, "%d", min);
        	}                         
        	if (sec < 10)
        	{                        
        		emptyvarstr = sprintf( secstr, "0%d", sec);
        	}else{
        		emptyvarstr = sprintf( secstr, "%d", sec);
        	}                        

        	if (isam == 1)
        	{
        		emptyvarstr = sprintf( labeldate2, "%s:%s:%s AM", hourstr, minstr, secstr);
        	}else{
        		emptyvarstr = sprintf( labeldate2, "%s:%s:%s PM", hourstr, minstr, secstr);
        	}
        	if (stagger == 230)
        	{
        		stagger = 220;
        	}else{
        		stagger = 230;
        	}
        	gdImageString(im, gdFontSmall,   1470   ,  stagger  , labeldate2 , black);
        	gdImageLine(im, coordinate, 210, 1500 , stagger, black);
	}
//
	out = fopen("/var/www/htdocs/lsnet/mem.png", "w"); 
	//gdImageJpeg(im, out, -1); //write the image to thefile using the default quality setting
	gdImageInterlace(im, 1);
	gdImagePng(im, out);
	fclose(out); 
	gdImageDestroy(im);
	return (0);
}

