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


//#define round(x) ((x) < LONG_MIN-0.5 ||(x) > LONG_MAX+0.5?\ error(): ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))


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


int memarray1[9];
//int memfrequency = 1;
//int memarray1[9]; 

int main() {
	gdImagePtr im; //declaration of the image
	FILE *out; //output file
	int black,white,red,blue,green,yellow,orange,purple,brown,grey;
	FILE* lsmemlog;  

	im = gdImageCreate(1620,155); //create an image, 100by 100 pixels

	white = gdImageColorAllocate(im, 255, 255, 255);
	black = gdImageColorAllocate(im, 0, 0, 0); //allocate black color
	//white = gdImageColorAllocate(im, 255, 255, 255);	//allocate white color	
	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 hourstr[3];
        char minstr[3];
        char secstr[3];
        char labeldate2[12]; 
	char label1[12];


	char linestring[1000];
	char copystr[1000];
	char swapusedstr[20];
	char temp[10];
	int i;
	int lastline;
	int lastminsince;
	int isam;
	int minsince;
	int minsince2;
	int hour, min, sec;
	double swapused;
	int counter, counter2, counter3;
	//coordinate
	double downnum = 110;
	//points
	int num4 = 0;
	meminfo(1,memarray1);
	int modulo4;
	int labeldate1;
	//char labeldate2[30];
        struct tm *local;
        time_t t;
	local = localtime(&t);
	char datestrcurrent[30];

	double swaptotal = memarray1[6] / 1024;
	//printf("swaptotal: %lf\n", swaptotal);
	//int swaptotal2 = doubletoint(swaptotal);
	//printf("swaptotal2: %d\n", swaptotal2);
 
	//swaptotal = swaptotal / 10;
	//int tenth = doubletoint(swaptotal);	
	//swaptotal = swaptotal * 10;
	int tenth = swaptotal / 10; 

	//int tenth = memarray1[1] / 10;
	char labelstring[10];
	int coordinate = 60;
	int emptyvarstr;
	//doubletoint ()
	emptyvarstr = sprintf( labelstring, "%d", num4);
	double difference = 0;           
	int lastdate = 0;
	int stagger = 130;


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




	gdImageLine(im, 50, 10,50,110, black);
	gdImageLine(im, 10, 110,1500,110, black);
	gdImageString(im, gdFontMediumBold,   5   ,   5        , "MB" , black);
	gdImageString(im, gdFontMediumBold,   1530   ,  100        , "Swap Usage" , black);
	gdImageString(im, gdFontMediumBold,   50   ,   140    , "Hours" , black);

	for (i = 0; i < 11; ++i)
	{
        	//$im->string(gdSmallFont,20,$num3,$num4,$black);
		//gdImageStringFT(im,brect,red,"/usr/X11R6/lib/X11/fonts/TTF/luxisr.ttf",20,0,0,num3,"num4");
		//if(err)	fprintf(stderr,"Error : %s\n",err);

		//char *s = "Hello.";
		gdImageString(im, gdFontSmall,   20   ,   downnum        , labelstring , black);



        	//$im->line(20,$num3,30,$num3,$black);
		gdImageLine(im, 20, downnum,30,downnum, black);
	
        	//$im->setStyle($yellow,$yellow,$yellow,$yellow,$blue,$blue,$blue,$blue,gdTransparent,gdTransparent);
        	//$im->dashedLine(30,$num3,1500,$num3,gdStyled);
		gdImageLine(im, 30, downnum,1500,downnum, gdStyled);

        	num4 = num4 + tenth;
        	//num4 = ceil(num4);
        	downnum = downnum - 10;
		emptyvarstr = sprintf( labelstring, "%d", num4);
	}

        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);
	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)
			{ 
				if (i >= 0)
				{
					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);
                                        token = strtok(NULL, search);
                                        token = strtok(NULL, search);
                                        token = strtok(NULL, search);
                                        token = strtok(NULL, search);     
                                        token = strtok(NULL, search);
                                        token = strtok(NULL, search);
					token = strtok(NULL, search);

					//printf("token: %s\n", token);
					//token[strlen(token)-1]='\0';
					swapused = atof(token);
					//printf("swapused: %lf\n", swapused);

					//swapused = swapused / 1024;
					//printf("swapused: %d\n", swapused);
					//printf("swapused: %d swaptotal2: %d downnum: %d\n", swapused, swaptotal2, downnum);
					//printf("swaptotal2: %d\n", swaptotal2);
					//printf("downnum: %d\n", downnum);
                        		//downnum = (swapused / swaptotal2); 
					//printf("downnum: swapused divided by swaptotal2: %d\n", downnum);
					//downnum = downnum / 1024.0;
					//printf("downnum divided by 1024: %d\n", downnum);
					
					downnum = (swapused / swaptotal) / 1024;
					//printf("swapused: %lf\n", swapused);
					//printf("swaptotal: %lf\n", swaptotal);


                        		downnum = downnum * 100;
                        		downnum = 100 - downnum;
                        		downnum = downnum + 10;
					//printf("downnum: %lf\n", downnum);

					//if multiple of 60 drawl date labels
                                        difference = minsince - lastminsince;
                                        difference = ceil(difference);
                                        if (difference >= 2)
                                        {
                                                //#leave a blank line for each 1min lost
                                                for (counter3 = 1; counter3 < difference; counter3++)
                                                {
                                                        coordinate++;
                                                }
                                        }

                                        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 == 130)
                                         	{
                                               		stagger = 120;
                                         	}else{
                                                 	stagger = 130;
                                         	}
						gdImageString(im, gdFontSmall,   coordinate - 30   ,  stagger  , labeldate2 , black);
						gdImageLine(im, coordinate, 110, coordinate , stagger, black);
                                	}

                                        //#total free
					gdImageLine(im, coordinate, 10,coordinate,110, green); 
                                        //#used
					gdImageLine(im, coordinate, downnum ,coordinate,110, red);
					lastminsince = minsince;
					coordinate++;
					i--;
				}
			}	
	}



	gdImageString(im, gdFontSmall,   1540   ,   10  , "Free" , black);
       	gdImageString(im, gdFontSmall,   1540   ,   60  , "Used" , black);

	emptyvarstr = sprintf( label1, "%.02lfMB", (swaptotal - (swapused / 1024)));

	gdImageString(im, gdFontSmall,   1540   ,   25  , label1 , black);
	emptyvarstr = sprintf( label1, "%.02lfMB", swapused / 1024);
	gdImageString(im, gdFontSmall,   1540   ,   75  , label1 , black);

       	gdImageLine(im, coordinate, 10, 1540 , 10, green);
       	gdImageLine(im, coordinate, downnum, 1540 , 60, red);

//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 == 130)
                {       
                        stagger = 120;
                }else{  
                        stagger = 130;
                }
                gdImageString(im, gdFontSmall,   1470   ,  stagger  , labeldate2 , black);
                gdImageLine(im, coordinate, 110, 1500 , stagger, black);
        }
//               


	out = fopen("/var/www/htdocs/lsnet/swap.png", "w"); //open a file
	//gdImageJpeg(im, out, -1); //write the image to thefile using the default quality setting
	gdImageInterlace(im, 1);
	gdImagePng(im, out);
	fclose(out); 
	gdImageDestroy(im);
}

