/*
 * identify.c
 * jskim@www.best5.net
 */

#include "version3/web.h"
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/file.h>
#include <stdlib.h>

#define DATA_DIR "IDENTIFY"

int GetCommentFileName(imgFile, FileName)
	char *imgFile;
	char *FileName;
{
	int		i;

	sprintf(FileName, "%s/%s.dsc", DATA_DIR, imgFile);

	return (int) 1;
}

int main()
{
	FILE		*fp;
	FILE *fdsc;
	char *ptr;
	char		comFile[256], email[256];	/* file to identify */
	char		tmpFile[256];
	char		myDate[256];
	char		*comment;
	postentry	entries[10];

	char		buf[256];
	int	i, x;
	int fd;
	int sendNow = NO;

	printf("content-type: text/html\n\n");	/* HTML Header */
	printf("<body bgcolor=black>\n");

	i = ProcessPostMethod( entries );


	email[0] = '\0';
	for (x = 0; x <= i; x++)
	{
		if (!strcasecmp(entries[x].name, "comFile"))
		{
			strcpy(comFile, entries[x].val);
			continue;
		}
		if (!strcasecmp(entries[x].name, "email"))
		{
			strcpy(email, entries[x].val);
			continue;
		}
		if (!strcasecmp(entries[x].name, "comment"))
		{
			comment = (char *) malloc(strlen(entries[x].val)+1);
			strcpy(comment, entries[x].val);
			continue;
		}
		if (!strcasecmp(entries[x].name, "submit") && \
			(!strcasecmp(entries[x].val, "send") ||
			!strcasecmp(entries[x].val, "Identify This")) )
		{
			sendNow = YES;
			continue;
		}
	}

	strcpy(buf, comFile);
	ptr = (char *)strchr(buf, '/');
	strcpy(buf, ptr+1);

	GetCommentFileName(buf, tmpFile);

	if (email[0] == '\0') sendNow = NO;
	if (sendNow)
	{
		if ( (fp=fopen(tmpFile, "a")) == NULL )
		{
			printf("File Open Error<br>\n");
			exit(1);
		}
		else
		{
			getdate(myDate);

			fd = fileno(fp);
			flock(fd, LOCK_EX);

			fprintf(fp, "Comments\n========\n");
			fprintf(fp, "From: %s\n", email);
			fprintf(fp, "Date: %s\n", myDate);
			fprintf(fp, "File To Comment: %s\n\n", comFile);
			fprintf(fp, "%s\n", comment);
			flock(fd, LOCK_UN);
			fclose(fp);
		}
		
		printf("<table width=100%c height=100%c\n>", '%', '%');
		printf("<tr><th valign=middle>\n");
		printf("<table cellpadding=20 border=0 bgcolor=#BBBBBB>\n");
		printf("<tr><th bgcolor=#dddddd>\n");
		printf("<center>Your comments were successfuly added.\n");
		printf("<P><font size=+3 color=blue>Thanks.</font><br></center>\n");
		printf("<form action=identify.cgi method=post>\n");
		printf("<input type=hidden name=comFIle value=%s>\n", comFile);
		printf("<input type=submit value=\"Go Back\">\n");
		printf("</th></tr></table>\n");
		printf("</th></tr></table>\n");
		exit(0);
	}
	else
	{
	    strcpy(buf, comFile);
	    ptr = (char *)strchr(buf, '/');
	    strcpy(buf, ptr+1);
	    for (i=strlen(buf); i > 0; i--) {
		if (buf[i] == '.') {
		    if (!strcasecmp(buf+i, ".jpg"))
			strcpy(buf+i, "-s160.jpg");
		    else if (!strcasecmp(buf+i, ".gif"))
			strcpy(buf+i, "-s160.gif");
		    break;
		}
	    }

	    printf("<center><br><a href=/animal/><font color=yellow>A. P. A. Home</font></a>\n");
	    printf("<form action=\"identify.cgi\" method=\"POST\">\n");
	    printf("<table cellpadding=1 border=1 bgcolor=#BBBBBB>\n");
	    printf("<tr><th><a href=/animal/ViewImg.cgi?img=%s>\n", comFile);
	    printf("<img border=3 src=PREVIEW/%s></a>\n", buf);
	    printf("<th><font size=+2>Identify This Animal</font><p>\n");
	    printf("<font size=-1>%s</font><p>\n", comFile);
	    printf("<font size=-1 color=#777777>Click the left thumbnail to view full image.</font><br>\n", comFile);

	    strcpy(buf, comFile);
	    for (i=strlen(buf); i > 0; i--) {
		if (buf[i] == '.') {
		    strcpy(buf+i, ".txt");
		    break;
		}
	    }
	    fdsc = fopen(buf, "r");
	    if (fdsc != NULL) {
		printf("<tr><td colspan=2>\n");
		while(fgets(buf, 256, fdsc)) {
		    printf("%s<br>", buf);
		}
		fclose(fdsc);
		printf("</td></tr>\n");
	    }

	    fdsc = fopen(tmpFile, "r");
	    if (fdsc != NULL) {
		printf("<tr><td colspan=2>\n");
		while(fgets(buf, 256, fdsc)) {
		    if (!strcmp(buf, "Comments\n")) {
			printf("<tr><td colspan=2>\n"); continue;
		    }
		    else if (!strcmp(buf, "========\n")) continue;
		    else if (!strncmp(buf, "File To Comment", 15)) continue;
		    else if (!strncmp(buf, "From: ", 6)) {
			printf("%s<br>\n", buf); continue;
		    }
		    else if (!strncmp(buf, "Date: ", 6)) {
			printf("%s<br>\n", buf); continue;
		    }
		    else
			printf("%s<br>\n", buf);
		}
		fclose(fdsc);
		printf("</td></tr>\n");
	    }

	    printf("<input type=hidden name=comFile value=\"%s\">\n", comFile);
	    printf("<tr><td colspan=2>\n");
	    printf("<br>Your Email Address: <input type=\"text\" name=\"email\" value=\"%s\" size=40>\n", email);
	    printf("<tr><td colspan=2>\n");
	    printf("<br><b>Comments:</b><br><textarea name=\"comment\" cols=60 rows=10></textarea>");
	    printf("<tr><th colspan=2>\n");
	    printf("<input type=\"submit\" name=\"submit\" value=\"Send\"> \n");
	    printf("<input type=\"reset\" value=\"Clear\"> \n");
	    printf("</table>\n</form>\n");
	}

	free(comment);

	exit(0);
}

