#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <gd.h>
#include "gdfonts.h"
#include "gdfontt.h"
#include "gdfontl.h"
#include "gdfontl.h"
#include "gdfontmb.h"



int main() {
        gdImagePtr im; //declaration of the image
 	FILE *out; //output file
        im = gdImageCreate(285,45); //create an image, 100by 100 pixels

//gdImageColorTransparent(im, 1);
        int white = gdImageColorAllocate(im, 255, 255, 255);
        int black = gdImageColorAllocate(im, 0, 0, 0);

        int green = gdImageColorAllocate(im, 0, 255, 0);


if (white != (-1)) {
	/* Make it transparent */
	gdImageColorTransparent(im, white);
}



	FILE* fp1;
	char linestring[1000];
	int temparray[5];
	int i = 0;
	int j;
	int emptyvar;
	int unique = 0;
	int nonunique = 0;
	int writeok = 1;

	char sincedate[30];

        const char *token;
        const char *line;
        const char *search1 = " ";

        const char *token2;
        const char *line2;
        const char *search2 = ".";

	const char *search3 = "[";

	int linenumber = 0;
        if( ( fp1 = fopen( "/var/log/httpd/access_log", "r" ) ) == NULL ) {
                fprintf( stderr, "Error opening /var/log/httpd/access_log\n");
                //return; 
                //exit( 1 );
        }

        while( fgets(linestring, sizeof(linestring), fp1) != NULL)
        {
		//208.80.195.37 - - [25/Jul/2009:05:15:19 -0700] "GET / HTTP/1.0" 200 1878
		if(linenumber == 0){
			token = strtok(linestring, search3);
			token = strtok(NULL, search3);
			token = strtok(token, search1);

			//token = strtok(NULL, search1);
//printf("%s\n", token);
			//token = strtok(NULL, search1);
			strcpy(sincedate, token);
			
			//printf("%s\n", sincedate);
		}

		linenumber++;
	}
	fclose(fp1);

	int iparray[linenumber][4];

        if( ( fp1 = fopen( "/var/log/httpd/access_log", "r" ) ) == NULL ) {      
                fprintf( stderr, "Error opening /var/log/httpd/access_log\n");
                //return;   
                //exit( 1 );
        }
        while( fgets(linestring, sizeof(linestring), fp1) != NULL)
        {
		token = strtok(linestring, search1);
//printf("%s\n", token);

		token = strtok(token, search2);		
		temparray[0] = atoi(token);
		token = strtok(NULL, search2);
		temparray[1] = atoi(token);
		token = strtok(NULL, search2);
		temparray[2] = atoi(token);
		token = strtok(NULL, search2);
		temparray[3] = atoi(token);

		//if ip is not equal to any previous array members then add it 
		for(i=0; i<linenumber; i++){
		               
			//for(j = 0; j<4; j++){
			//if no match has been found yet keep searching entire array 
			if(writeok == 1 && temparray[0] == iparray[i][0] && temparray[1] == iparray[i][1] &&
				temparray[2] == iparray[i][2] && temparray[3] == iparray[i][3])
			{
				writeok = 0; //set if a match is found
			}

		}

			
		if(writeok == 1){
			for (j = 0; j<4; j++){
				iparray[unique][j] = temparray[j]; //write ip to array
				//unique++; 			
			}
			 unique++;
		}else{

			writeok = 1;
			nonunique++;
		}			


		//nonunique++;
		//linenumber++;
        }
	fclose(fp1);


	//count and return number of unique ip's
	//i=0;
	//while(i<linenumber && iparray[i][0] != NULL){
		//iparray[i][0]
	//	unique++;
	//}

//	printf("\nWebhits Since: %s PST\n", sincedate);
//	printf("number of nonunique: %i\n", nonunique); 
//	printf("number of unique: %i\n", unique);

	emptyvar = sprintf(linestring, "Webhits Since: %s PST", sincedate);
	gdImageString(im, gdFontMediumBold,   5   ,   5        , linestring , green);

        emptyvar = sprintf(sincedate, "Unique: %i", unique);
        gdImageString(im, gdFontMediumBold,   5   ,   15        , sincedate , green);

        emptyvar = sprintf(sincedate, "Nonunique: %i", nonunique);
        gdImageString(im, gdFontMediumBold,   5   ,   25        , sincedate , green);

out = fopen("hits.png", "w");
        gdImageInterlace(im, 1);
        gdImagePng(im, out);

	//fprintf(stderr, im);

        fclose(out);

//printf("%s", im->png);
        gdImageDestroy(im);




//<div style="text-align: center; color: rgb(255, 255, 255);"><img alt="webhits" title="webhits" src="cgi-bin/hits.png"><br>
//printf("<div style=%%"text-align: center%%; color: rgb(255, 255, 255)%%;%%"><img alt=%%"webhits%%" title=%%"webhits%%" 
//src=%%"cgi-bin/hits.png%%"><br>\n");

printf("<div style=\"text-align: center; color: rgb(255, 255, 255);\"><img alt=\"webhits\" title=\"webhits\" src=\"cgi-bin/hits.png\"><br>");


	return 0;
}
