#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "generate.h"
//#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
//#define SIZE 1000
//int maxtextbuffer = sizeof(char)*250;
//#define SIZE sizeof(char)*250
#define SIZE 255

int main() {
	FILE* fp1;
	int count1 = 0; 
	int detected = 0;
	char buf[SIZE];
	char temp[30];
	int empty;
	int error=0;
	int error2=1;
	char remoteip[30];

    //char Buffer[512]; 
    //detected = atoi( getenv("INPUT") ); 
    //fread( Buffer, InputLength, 1, stdin ); 

	//buf = getenv("QUERY_STRING");

	//empty = sprintf(buf, "%s", getenv("Duration") );
	empty = sprintf(buf, "%s", getenv("QUERY_STRING") );

	empty = sprintf(remoteip, "%s", getenv("REMOTE_ADDR") );

printf( "Content-type: text/html\n\n");
printf( "<html>");
//printf( "</title></head>");
printf( "<body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#FF0000\"link=\"#800000\">");
//printf( "<span style=\"color: rgb(2, 2, 255) font-size: 12\">");
printf( "<font size=\"-3\">");   


//<       
//tag: mar1
//question: should marijuana be legalized?
//options: yes, no, maybe
//>  




	if (buf[count1] == 'a'){
		if (buf[count1+1] == 'n'){
			if (buf[count1+2] == 's'){
				if (buf[count1+3] == 'w'){
					if (buf[count1+4] == 'e'){
						if (buf[count1+5] == 'r'){
							//if (buf[count1+6] == '='){

								//if (buf[7] == buf[7]) {
									temp[0]=buf[7];
									temp[1]=0;
									detected = atoi(temp);

								if (detected == detected) {
									//printf("choice: %c", buf[count1+7]);
									//buf[8]=0;
									//strcpy(buf, buf[count1+7]);
									//error=0;
									if(detected == 0 || detected == 1 || detected == 2) {
										error=0;
									}else{
										error=1;
										printf("syntax error, invalid number option");
									}
								} else {
									error=1;
									printf("syntax error, NaN");
								}


							//}
						}else{ error=1; printf("syntax error");}
					}else{ error=1; printf("syntax error");}
				}else{ error=1; printf("syntax error");}
			}else{ error=1; printf("syntax error");}
		}else{ error=1; printf("syntax error");}
	}else{ error=1; printf("syntax error");}



if(error != 1) {


        if( ( fp1 = fopen( "./ipaddress", "r" ) ) == NULL ) {
                //fprintf( stderr, "Error opening\n" );
                //exit( 1 );
		error=1;
		printf("server: error opening file.");
        }
        while( fgets(buf, sizeof(buf), fp1) != NULL)
        {
		count1=0;
		while(buf[count1] != '\n' && buf[count1] != ' '){
			temp[count1]=buf[count1];
			count1++;
		}
		temp[count1]=0;
		error2=strcmp(remoteip, temp);
		//printf("error2: %d\n", error2);
		if(error2 == 0) {
			error = 1;
                	printf("You Have already voted");
        	}

	}


fclose( fp1 );

	//if(error2 == 0) {
	//	printf("You Have already voted");
	//}

}


if(error != 1) {
	if ( ( fp1 = fopen("./ipaddress", "a") ) ) {
		fprintf(fp1, "%s %d\n", remoteip, detected );
	}
	fclose( fp1 );

	//generate image here
	generate();
	printf( "<img alt=\"poll\" title=\"poll\" src=\"http://sterlingdesktops.com/poll.png\">" );
}



printf( "</font>");

//printf( "</span>");

printf( "</body></html>");



	return 0;
}
