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

#include "ViewImg.h"
#include "NameSearch/AD_Buf.h"

int lang = 0;	/* 0 for English, 1 for Korean */

int main()
{
	FILE		*ftemplate;
	FILE		*ftmp;
	char		srcFile[256];
	char		srcPath[256];
	char		srcFileDesc[256];
	getentry	entries[5];

	char		buf[256];
	char		*ptr;
	register	int	i, x;

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

#ifdef FILEOUT
	printf("Debugging Mode\n");
	i = 2;
	strcpy(entries[0].name, "img");
	strcpy(entries[0].val, "animal3/Dolphins-s095239.jpg");
#else
	i = ProcessGetMethod( entries );
#endif

	for (x = 0; x <= i; x++)
	{
		if (!strcasecmp(entries[x].name, "img")) {
			strcpy(buf, entries[x].val);
			while (ptr = (char *)strstr(buf, "/")) {
				strcpy(buf, ptr+1);
			}
			strcpy(srcFile, buf);
			strcpy(buf, entries[x].val);
			ptr = (char *) strstr(buf, srcFile);
			*ptr = '\0';
			strcpy(srcPath, buf);
			continue;
		}
		if (!strcasecmp(entries[x].name, "Lang")) {
			if (!strcasecmp(entries[x].val, "Korean"))
				lang = 1;
			continue;
		}
	}

	if (lang == 1)
		ftemplate = fopen(TEMPLATE_HAN, "r");
	else
		ftemplate = fopen(TEMPLATE_ENG, "r");
	if (ftemplate == NULL) {
		printf("template open error!!!.");
		exit(0);
	}

	while ( fgets(buf, 256, ftemplate) ) {
		if ( ptr = (char *)strstr(buf, "___IMAGE_PATH___") ) {
			*ptr = '\0';
			printf("%s%s%s%s", buf, srcPath, srcFile, ptr+16);
		}
		else if ( ptr = (char *)strstr(buf, "___IMAGE_NAME___") ) {
			*ptr = '\0';
			printf("%s%s%s", buf, srcFile, ptr+16);
		}
		else if ( ptr = (char *)strstr(buf, "___DESCRIPTION_FILE___") ) {
			*ptr = '\0';
			printf("%s", buf);
			PrintDescFile(srcPath, srcFile);
			printf("%s", ptr+22);
		}
		else if ( ptr = (char *)strstr(buf, "___NODL_JAVASCRIPT___") ) {
		    char tmpbuf[256];
		    if ( strstr(srcFile, "Jose_Sierra_Jr")) {
			printf("<meta test=jose's image : %s>\n", srcFile);
			ftmp = fopen("HTMLSrc/NoD_L.javascript", "r");
			if (ftmp == NULL) continue;
			while ( fgets(tmpbuf, 256, ftmp) ) {
			    printf("%s", tmpbuf);
			}
			fclose(ftmp);
		    }
		}
		else
			printf("%s", buf);
	}
	fclose(ftemplate);

	exit(0);
}

int PrintDescFile(srcPath, srcFile)
char *srcPath;
char *srcFile;
{
	FILE		*fp;
	char srcFileDesc[256];
	char buf[256];
	char		buf2[256];
	int i;

	strcpy(buf, srcFile);
	for (i = strlen(srcFile); i > 0; i--)
	{
		if (buf[i] == '.')
		{
			buf[i] = '\0';
			break;
		}
	}

	while (1)
	{
		strcpy(srcFileDesc, srcPath);
		strcat(srcFileDesc, buf);
		strcat(srcFileDesc, ".txt");
		if ( (fp=fopen(srcFileDesc, "r")) == NULL ) ;
		else
		{
			printf("<pre>\n");
			while(fgets(buf2, 256, fp))
				PrintLink(buf2);
			printf("</pre>\n");
			fclose(fp);
			break;
		}

		strcpy(srcFileDesc, srcPath);
		strcat(srcFileDesc, buf);
		strcat(srcFileDesc, ".TXT");
		if ( (fp=fopen(srcFileDesc, "r")) == NULL ) ;
		else
		{
			printf("<pre>\n");
			while(fgets(buf2, 256, fp))
				PrintLink(buf2);
			printf("</pre>\n");
			fclose(fp);
			break;
		}

		strcpy(srcFileDesc, srcPath);
		strcat(srcFileDesc, buf);
		strcat(srcFileDesc, ".html");
		if ( (fp=fopen(srcFileDesc, "r")) == NULL ) ;
		else
		{
			printf("<p><table width=460><tr><td>\n");
			while(fgets(buf2, 256, fp))
				if (!(strstr(buf2, "<img ") || strstr(buf2, "<IMG ")))
					printf("%s", buf2);
			printf("</table>\n");
			fclose(fp);
			break;
		}
		break;
	}
	printf("</table>\n");

	sprintf(buf, "%s%s", srcPath, srcFile);
	if ( (fp=fopen(buf, "r")) == NULL )
	{
		printf("<font size=+1>Cannot open file <b><tt>%s</tt></b>\n", buf);
		printf("<br>Back and try again.<br>\n</font>\n");
		printf("<p><hr>\n<center>\n<font size=+1>\n");
		printf("<p><a href=/animal/>Go Back To Home</a><p>\n");
		printf("</font>\n</center>\n</body>\n");
		printf("</html>\n");
		exit(1);
	}

	return 1;
}


/* NCSA Web Library */
void 
getword(char *word, char *line, char stop)
{
	int             x = 0, y;

	for (x = 0; ((line[x]) && (line[x] != stop)); x++)
		word[x] = line[x];

	word[x] = '\0';
	if (line[x])
		++x;
	y = 0;

	while (line[y++] = line[x++]);
}

char           *
makeword(char *line, char stop)
{
	int             x = 0, y;
	char           *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));

	for (x = 0; ((line[x]) && (line[x] != stop)); x++)
		word[x] = line[x];

	word[x] = '\0';
	if (line[x])
		++x;
	y = 0;

	while (line[y++] = line[x++]);
	return word;
}

char           *
fmakeword(FILE * f, char stop, int *cl)
{
	int             wsize;
	char           *word;
	int             ll;

	wsize = 102400;
	ll = 0;
	word = (char *) malloc(sizeof(char) * (wsize + 1));

	while (1)
	{
		word[ll] = (char) fgetc(f);
		if (ll == wsize)
		{
			word[ll + 1] = '\0';
			wsize += 102400;
			word = (char *) realloc(word, sizeof(char) * (wsize + 1));
		}
		--(*cl);
		if ((word[ll] == stop) || (feof(f)) || (!(*cl)))
		{
			if (word[ll] != stop)
				ll++;
			word[ll] = '\0';
			return word;
		}
		++ll;
	}
}

char 
x2c(char *what)
{
	register char   digit;

	digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A') + 10 : (what[0] - '0'));
	digit *= 16;
	digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A') + 10 : (what[1] - '0'));
	return (digit);
}

void 
unescape_url(char *url)
{
	register int    x, y;

	for (x = 0, y = 0; url[y]; ++x, ++y)
	{
		if ((url[x] = url[y]) == '%')
		{
			url[x] = x2c(&url[y + 1]);
			y += 2;
		}
	}
	url[x] = '\0';
}

void 
plustospace(char *str)
{
	register int    x;

	for (x = 0; str[x]; x++)
		if (str[x] == '+')
			str[x] = ' ';
}

int 
rind(char *s, char c)
{
	register int    x;
	for (x = strlen(s) - 1; x != -1; x--)
		if (s[x] == c)
			return x;
	return -1;
}

int 
getline(char *s, int n, FILE * f)
{
	register int    i = 0;

	while (1)
	{
		s[i] = (char) fgetc(f);

		if (s[i] == CR)
			s[i] = fgetc(f);

		if ((s[i] == 0x4) || (s[i] == LF) || (i == (n - 1)))
		{
			s[i] = '\0';
			return (feof(f) ? 1 : 0);
		}
		++i;
	}
}

void 
send_fd(FILE * f, FILE * fd)
{
	int             num_chars = 0;
	char            c;

	while (1)
	{
		c = fgetc(f);
		if (feof(f))
			return;
		fputc(c, fd);
	}
}

/* coded by armian@www.kordic.re.kr */
int
ProcessGetMethod(entries)
getentry	*entries;
{
	char	*cl;
	int	x, m;

	if (strcmp(getenv("REQUEST_METHOD"), "GET"))
        {
                printf("This script should be referenced with a METHOD of GET.\n");
                printf("If you don't understand this, see this ");
                printf("<A HREF=\"http://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/fill-out-forms/overview.html\">forms overview</A>.%c", 10);
                exit(1);
        }

        cl = getenv("QUERY_STRING");
        if (cl == NULL)
        {
                printf("No query information to decode.\n");
                exit(1);
        }

        for (x = 0; cl[0] != '\0'; x++)
        {
                m = x;
                getword(entries[x].val, cl, '&');
                plustospace(entries[x].val);
                unescape_url(entries[x].val);
                getword(entries[x].name, entries[x].val, '=');
        }

	return m;
}
