#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, *gifIn;
	int choice = 0;
	int count = 0;
	fprintf(cgiOut, "HTTP/1.0 200 OK\n");
	cgiHeaderContentType(
		"multipart/x-mixed-replace;boundary=goober\n\n");

	in = fopen(GIF_LIST_FILE, "r");
	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 ");
		fprintf(cgiOut, "\n--goober\n");
		/* Now output the file */
		gifIn = fopen(filename, "rb");
		if (!gifIn) {
			/* Missing frame, skip it */
			break;
		}
		cgiHeaderContentType("image/gif");
		while (1) {
			int ch;
			ch = getc(gifIn);
			if (ch == EOF) {
				break;
			}
			putc(ch, cgiOut);
		}	
		fclose(gifIn);
		/* A short delay to make sure each frame is appreciated */
		sleep(1);
		count++;	
	}
	fclose(in);
	/* Now terminate the server push sequence altogether */
	fprintf(cgiOut, "\n--goober--\n");
	return 0;		
}
