/*
 * NewImg.c
 * for Bioinfo Animal Pictures Archive
 * jskim@bulls.kordic.re.kr
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

#define NEW_IMG_DIR "New"
#define HTML_TEMPLATE "HTMLSrc/NewImg.html"


#define CGI_PREVIEW "preview.cgi"
#define CGI_VIEW "ViewImg.cgi"
#define CGI_MAIL "uu2mail.cgi"

main()
{
	FILE	*fp;

	DIR *dirp;
	struct dirent *direntp;

	char *ptr;
	char line[256];

	printf("content-type: text/html\n\n");	/* HTML Header */

	fp = fopen(HTML_TEMPLATE, "r");
	if (fp == NULL) { printf("HTML template error\n"); exit(0); }

	while (fgets(line, 256, fp)) {
		if (!strncasecmp(line, "__RESULT__", 10)) {
			printf("<ol>\n");
			dirp = opendir( NEW_IMG_DIR );
			while ( (direntp = readdir( dirp )) != NULL ) {
				ptr = (char *) strstr(direntp->d_name, ".");
				if (!strcasecmp(ptr+1, "JPG") || !strcasecmp(ptr+1, "gif"))
				PrintImgInfo(direntp->d_name);
			}
		}
		else printf("%s", line);
	}
	printf("</ol>\n");

	closedir( dirp );
	fclose(fp);

	exit(0);
}

int PrintImgInfo(char *name)
{
	char path[256];	/* path */

	struct stat stat;

	sprintf(path, "%s/%s", NEW_IMG_DIR, name);


	lstat(path, &stat);

	printf( "<li><a href=%s?img=%s><img src=/~jskim/img/preview.gif></a>\n", CGI_PREVIEW, path);
	printf( "\t<a href=%s?img=%s>\n", CGI_VIEW, path);
	printf( "\t<b>%s</b></a> (%dbytes)\n", name, stat.st_size );
	printf( "[<a href=%s?email=&srcFile=%s>mail it</a>]\n", CGI_MAIL, path);

	return 0;
}
