/*
 * 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[256];
    register	int	i, x;
    int		level;

    FILE	*fhtml;
    char	*ptr;
    char	line[1024];
    
    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;
	}
    }
    
    if (lang == 1)
	fhtml = fopen(HTML_FILE_ENG, "r");
    else
	fhtml = fopen(HTML_FILE_ENG, "r");
    if (fhtml == NULL) {
	printf("HTML template open failed<br>\n");
	exit(-1);
    }

    while (fgets(line, 1024, fhtml))
    {
	if ( (ptr=(char *)strstr(line, "__QUERY__")) != NULL) {
	    *ptr = '\0';
	    printf("%s%s%s", line, keyword, ptr+9);
	}
	else if ( (ptr=(char *)strstr(line, "__URL_QUERY__")) != NULL) {
	    *ptr = '\0';
	    printf("%s%s%s", line, esc_url(keyword), ptr+13);
	}
	else if ( (ptr=(char *)strstr(line, "__RESULT_HERE__")) != NULL) {
	    *ptr = '\0';
	    printf("%s", line);
	    if (!strcasecmp(keyword, "all")) PrintListAll(keyword, -1);
	    else PrintListAll(keyword, level);
	    printf("%s", ptr+15);
	}
	else if ((ptr=(char *)strstr(line, "__IMG_SEARCH_LINK__")) != NULL ) {
	    *ptr = '\0';
	    printf("%s", line);
	    WebSearch(keyword, "image");
	    printf("%s", ptr+19);
	}
	else if ((ptr = (char *)strstr(line, "__WEB_SEARCH_LINK__")) != NULL ) {
	    *ptr = '\0';
	    printf("%s", line);
	    WebSearch(keyword, "web");
	    printf("%s", ptr+19);
	}
	else if ((ptr =(char *)strstr(line, "__AUDIO_SEARCH_LINK__")) != NULL) {
	    *ptr = '\0';
	    printf("%s", line);
	    WebSearch(keyword, "audio");
	    printf("%s", ptr+21);
	}
	else if ((ptr =(char *)strstr(line, "__VIDEO_SEARCH_LINK__")) != NULL) {
	    *ptr = '\0';
	    printf("%s", line);
	    WebSearch(keyword, "video");
	    printf("%s", ptr+21);
	}
	else printf("%s", line);
    }
    fclose(fhtml);
    
    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);
	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", esc_url(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=/list.php?qry=%s>%s</a>\n", esc_url(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);
	}
}
*/
