#include <string.h>
#include "cgic.h"

/* This filename must be changed. The file it points to
	must contain a list of GIF files on your system,
	specified by complete paths. */

#define GIF_LIST_FILE "/CHANGE/THIS/PATH/giflist.txt"

int cgiMain()
{
	FILE *in;
	int choice = 0;
	int count = 0;
	in = fopen(GIF_LIST_FILE, "r");
	if (strlen(cgiPathInfo)) {
		/* Ignore the leading slash */
		choice = atoi(cgiPathInfo + 1);
	}
	while (1) {
		char s[256];
		char *filename;
		if (!fgets(s, 256, in)) {
			fclose(in);
			break;
		}
		/* The filename is everything up to the line break */
		filename = strtok(s, "\r\n\t ");
		if (count == choice) {
			/* If there will be another, output a Refresh */
			char s2[256];
			if (fgets(s2, 256, in)) {
				fprintf(cgiOut, 
					"Refresh: 10; URL=%d.gif\n", 
					choice + 1);
			}
			/* Now output the file */
			cgiHeaderContentType("image/gif");
			fclose(in);
			in = fopen(filename, "rb");
			while (1) {
				int ch;
				ch = getc(in);
				if (ch == EOF) {
					break;
				}
				putc(ch, cgiOut);
			}	
			fclose(in);
			break;
		}
		count++;	
	}
	return 0;		
}
