/*
 *  counter - A rudimentary web page counter. 
 *  Written by Mike Henderson, 1995
 *  Copyright Mike Henderson, 1995
 *  
 */
#include <stdio.h> 
#define width 114 /*  19*6  */ 
#define height 25
#define htroot "/n/leadbelly/belly1/www/htdocs"
int DEBUG= 0;
main(){
	char str[20], comments[80], filename[20];
	char pbmtype[20], cntr_file[120], cntr_gif[120], digits_path[120];
	char cntr_gif2[120];
	char *user, *page, *fname, *caller;
	int X, Y, MaxVal, x, y, i, tmp;
	unsigned char image[2850];
	unsigned char red[256], grn[256], blu[256];
	long count;
	FILE *fp, *fp_out;
	
	/* 
	 *  Print out the neccessary text for server to recognize this as html
	 */
	printf("Content-type: text/html\n\n");


	/*
	 *  We need to determine which user's index.shtml script has invoked this
	 *  program. I'd prefer not the trust the user to supply that info as in
	 *  the PATH_INFO env variable. Rather, for server side includes, the
	 *  additional variable DOCUMENT_URI can be used. It will contain the path
	 *  of the html file that called this program (e.g. /~mgh/index.shtml)
	 *  Just need to parse it properly. This is all done in get_user()
	 */
	caller = (char *)getenv("DOCUMENT_URI");


	/*
	 *  Set some filenames and paths that we need.
	 */
	sprintf(cntr_file,   "%s/%s_counter", htroot, caller);
	sprintf(cntr_gif,    "%s/%s_counter.gif", htroot, caller);
	sprintf(cntr_gif2,    "%s_counter.gif", caller);
	sprintf(digits_path, "/n/leadbelly/belly1/www/cgi-bin/CCR_forms/counter");



	/*
	 *  Open the .counter file and increment count.
	 *  If it doesnt exist, then count is 0 and the file will get created below.
	 */
	count = 0;
	if ((fp = fopen(cntr_file, "r")) != NULL){
		fscanf(fp, "%d", &count);
		fclose(fp);
	}
	++count;
	sprintf(str, "%06d", count);



	/*
	 *  generate a grey scale color table. Needed for GIF image. Note that
	 *  the images I use are not just BW because they are antialiased!
	 */
	for(i=0; i<256; ++i){
		red[i] = (unsigned char)i;
		grn[i] = (unsigned char)i;
		blu[i] = (unsigned char)i;
	}


	/* 
	 *   for each digit, read in the appropriate file
	 *   and paste into the image array.
	 *   Could be sped up quite a bit by not reading files
	 *   that have already been read. Also could store digits in
	 *   binary to speed up. Image could also use some beautifying...
	 *   Motif style 3D frame would be easy. Also, Tekton font would
	 *   look alot better than Tex's CM fonts! Could allow coice of
	 *   several fonts? This loop needs error checking on file I/O ...
	 */
	for(i=0; i<6; ++i){
		sprintf(filename, "%s/%1d_tekton.pgm", digits_path, str[i]-48);
		fp = fopen(filename, "r");
                /* must be a PPM file to work for now */
                fscanf(fp, "%s", pbmtype);
                fgets(comments, 80, fp);
                fgets(comments, 80, fp);
                fscanf(fp, "%d %d", &X, &Y);
                fscanf(fp, "%d", &MaxVal);
                for (y=0; y<Y; ++y){
                	for (x=0; x<X; ++x){
                                fscanf(fp, "%d", &tmp);
				*(image + x+i*19 + y*width) = tmp;
                        }
                }
                close(fp);
	}



	/*
	 *  Put a border around the image
	 */
	for (y=0; y<height; ++y){
		*(image + y*width) = 150;
		*(image + width-1 + y*width) = 150;
	}
	for (x=0; x<width; ++x){
		*(image + x) = 150;
		*(image + x + (Y-1)*width) = 150;
	}


	
	/* 
	 *   Now dump the pgm file 
	 */
/* for debugging...
	fp_out = fopen("counter.pgm", "w");
	fprintf(fp_out, "P2\n#\n#\n");
	fprintf(fp_out, "%d %d\n", width, height);
	fprintf(fp_out, "255\n");
	for (y=0; y<height; ++y){
		for (x=0; x<width; ++x){
			fprintf(fp_out, "%d\n", *(image + x+i*19 + y*width));
		}
	}
	close(fp_out);
*/


	/*
	 *  Now Dump the GIF file. Needs error checking.....
	 */
	fp = fopen(cntr_gif, "w");
	WriteGIF(fp, image, 0, width, height, red, grn, blu, 256, 0, "");
	fclose(fp);


	printf("<img src=""%s"" align=absmiddle>\n", cntr_gif2);



/*
	printf("%06d\n", count);
*/

	if ((fp = fopen(cntr_file, "w")) != NULL){
		fprintf(fp, "%06d\n", count);
		fclose(fp);
	}
	else{
		printf("could not write to file: %s\n", cntr_file);
	}
}