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

#include "keyword.h"
void PrintListAll(char *keyword, int level);
int PrintEachLineAll(char *keyword, char *line, int level, int prevLevel);
void DelSpace(char *line);

int flag = NO;	/* For PrintEachLineAll() only */
int lang = 0;	/* 0 for English, 1 for Korean */

int main()
{
	getentry	entries[10];

	char		keyword[128];
	register	int	i, x;
	int		level;

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

#ifdef FILEOUT
	printf("Debugging Mode\n");
	i = 1;
	strcpy(entries[0].name, "keyword");
	strcpy(entries[0].val, "all");
	strcpy(entries[1].name, "level");
	strcpy(entries[1].val, "0");
#else
	i = ProcessGetMethod( entries );
#endif

	for (x = 0; x <= i; x++)
	{
		if (!strcmp(entries[x].name, "keyword"))
		{
			strcpy(keyword, entries[x].val);
			trim(keyword);
			continue;
		}
		if (!strcmp(entries[x].name, "level"))
		{
			level = atoi(entries[x].val);
			continue;
		}
		if (!strcasecmp(entries[x].name, "Lang"))
		{
			if (!strcasecmp(entries[x].val, "Korean"))
				lang = 1;
			continue;
		}
	}

	printf("<html>\n");
	printf("<head>\n");
	printf("<title>Keywords For Animal Names</title>\n");
	printf("</head>\n");
	printf("<body bgcolor=\"white\">\n");
	printf("<center>\n");
	printf("<iframe src=/animal/AD/ad.cgi marginwidth=0 marginheigt=0\n");
	printf("scrolling=no frameborder=0 width=480 height=80>\n");
	printf("<center>\n");
	printf("<a href=http://www.exchange-it.com/click.go?b112425=1 ");
	printf("target=_top><img src=http://www.exchange-it.com/image.go?b112425=1 ");
	printf("width=468 height=60 border=1 ALT=\"Click to Visit\"></a>\n");
	printf("</center>\n");

	printf("</iframe></center>\n");
/*
	printf("<center><img src=\"/animal/APAkeywords.gif\"></center>\n");
*/
	printf("<hr noshade width=540><p>\n");

	if (!strcasecmp(keyword, "all"))
		PrintListAll(keyword, -1);
	else
		PrintListAll(keyword, level);

	printf("<hr>\n<center>\n<p><font size=+1>");
	printf("<a href=\"%s\">%s</a><p>\n", URL_HOME, URL_HOME_INFO);
	printf("</font>\n</center>\n</body>\n");
	printf("</html>\n");

	exit(0);
}

void PrintListAll(keyword, level)
	char	*keyword;
	int	level;
{
	FILE		*fp;
	char		buf[256];
	int		prevLevel = 0;
	int		i;

	if ((fp=fopen(KEYWORD_FILE, "r")) == NULL)
	{
		printf("<center><font color=\"red\" size=+1>File Open Error.</font></center>br>\n");
		return;
	}

	while (fgets(buf, 256, fp))
	{
		buf[strlen(buf)-1] = '\0';
		prevLevel = PrintEachLineAll(keyword, buf, level, prevLevel);
	}

	fclose(fp);
}

int PrintEachLineAll(keyword, line, level, prevLevel)
	char	*keyword;
	char	*line;
	int	level;
	int	prevLevel;
{
	char buf[256];
	int curLevel;
	int i;

	for (i=0; i < strlen(line); i++)
	{
		if (line[i] != ' ')
			break;
	}

	curLevel = i/4;

	if (level == -1)
		flag = YES;
	else if (curLevel == level && strstr(line, keyword))
	{
		flag = YES;
	}
	else if (flag && (curLevel <= level))
	{
		for (i=0; i<prevLevel-curLevel; i++)
			printf("</ul>\n");
		flag = NO;
	}

	if (flag && (curLevel > prevLevel && curLevel > level))
	{
		for (i=0; i<curLevel-prevLevel; i++)
			printf("<ul>\n", i, curLevel, prevLevel);
	}
	else if (flag && (curLevel < prevLevel))
	{
		for (i=0; i<prevLevel-curLevel; i++)
			printf("</ul>\n");
	}

	strcpy(buf, line);
	trim(buf);
	DelSpace(buf);
	trim(line);

	if (flag)
	{
		if (curLevel != level && curLevel != 0)
			printf("<li>");
		else if (prevLevel == 0)
			printf("<p>\n");

		if (buf[0] != '*') {
			if (lang == 1) {
				printf("<a href=APAsrch3.cgi?qt=%s&Lang=Korean>%s</a>\n", buf, line);
				printf(" &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -------- <font size=-1> [<a href=SearchName.cgi?qt=%s>ÇÑ±ÛÀÌ¸§Ã£±â</a>]</font>\n", buf);
			}
			else
				printf("<a href=APAsrch3.cgi?qt=%s>%s</a>\n", buf, line);
		}
		else
			printf("%s\n", line+1);
	}

	return curLevel;
}

void DelSpace(string)
	char	*string;
{
	int i;

	for (i=0; i < strlen(string); i++)
	{
		if (string[i] == ' ') string[i] = '+';
	}
/*
	char *ptr;

	while (ptr = (char *) strstr(string, " "))
	{
		*(ptr) = '\0';
		strcat(string, ptr+1);
		DelSpace(string);
	}
*/
}

/* 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;
}
