
/****

* FILE: jwais-html.c
* Description:
* Part of NCSA Doc Finder suite.
* 
* This is a filter for the output from a wais search. The resulting
* file names are converted to links by this program.
* 
* Compile as:
#    cc jwais-html-c -o jwais-html
* 
* NOTES: You will need to change the MYHOST and PATHPREFIX defines.
* 
* 
* by Jason NG NCSA May 1994
* This is public domain software from NCSA
*
**/

#include <stdio.h>
#include <string.h>

#define SLEN 15000
char *searchword = NULL;

#define MYHOST "http://www.ncsa.uiuc.edu"
#define PATHPREFIX "/afs/ncsa.uiuc.edu/common/doc/web"

static int pathprefixlen = sizeof(PATHPREFIX) - 1;

main(ac,av) int ac; char**av;
{
  FILE *f = stdin;
  char buf[SLEN];
  char *sp;
  int count=0;

  searchword = av[1];
  fprintf(stdout,"<b> SEARCH FOR [%s] </b>\n", searchword);
  while(NULL!=fgets(buf,SLEN,f)) {
	 /* discard prompt line from waissearch */
	 if (NULL != strstr(buf,"View")) continue;

	 fprintf(stdout,"<dt> ");
    if (NULL!= (sp=strstr(buf,"no result"))) {/* check for "no result" line */
		  char *sp = strchr(buf,'.');
		  if(sp) *sp = '\0';
		  fprintf(stdout,"<b>");
		  fprintf(stdout,buf);
		  fprintf(stdout,"</b>");
		  fprintf(stdout," <p>Click on the BACK button to get back. ");
		  exit();
		 }
	 else if(NULL != strstr(buf,"Score")) { count++; genanchor(buf); }
	 else fprintf(stdout,buf);
	 }

   if(count==0) fprintf(stdout,"<p> <b> No documents found </b> ");
}

genanchor(ss) char *ss;
  
{
  char *sp;
  char fname[500], fpath[500], fnumber[20];
  char buf[500];

  sp = strstr(ss,"'");
  sscanf(sp,"\'%s %s\'", fname, fpath);

  /* strip quote from fpath */
  sp = strstr(fpath,"'");
  *sp = '\0';

  /*  fprintf(stdout,"ORIG [%s]",fpath ); */  
  /* now chop off front prefix */
  sp = strstr(fpath,PATHPREFIX);
  sprintf(buf,"%s",&sp[pathprefixlen]);
  strcpy(fpath,buf);
  /**
  fprintf(stdout,"FPATH IS [%s]", fpath);
  fprintf(stdout,"<p>");
  fprintf(stdout,"<b> HREF=\"%s%s%s\"> %s </b>", MYHOST, fpath, fname, fname);
  */

  fprintf(stdout,"<b> ");
  fprintf(stdout,"<a HREF=\"%s%s%s\"> %s </a>", MYHOST, fpath, fname, fname);
  fprintf(stdout,"</b> ");
  
  sp = strchr(ss,'\'');
  if(sp) { *sp='\0'; }
  fprintf(stdout, ss);
  fprintf(stdout, "\n");

}

