#include <stdio.h>
#include <ctype.h>
#include <strings.h>
#include "hash.h"

#define LIBFILE "/afs/sipb/project/sipb/lib/thesaurus/thesaurus.dat"
#define INDXFILE "/afs/sipb/project/sipb/lib/thesaurus/thesaurus.indx"

#define openlib() (libfptr = fopen(LIBFILE, "r"))
#define closelib() (fclose(libfptr))

FILE *libfptr;
FILE *indxfptr = NULL;

void usage() {
  output("usage: thesaurus [-help] [-info] [word] [word] ...\n");
}

void info() {

  output("This Thesaurus uses a data file from an MS-DOS program THESAUR by\n\
derrick burgess.  It is available by ftp from msdos.archive.umich.edu.\n");

}

char *lookup(char *word) ;
main(int argc, char *argv[]) 
{
  char *str;
  char input[80];

  if( !parse_args(argc, argv) ) {
    /* error */
    exit(-1);
  }

  if ( openlib() == NULL ) {
    output("Can't open thesaurus library file\n");
    exit(-1);
  }
  argc--;
  argv++;
  if(argc) 
    while(argc) {
      
      str = lookup(*argv);
      output( str ); 
      free(str); 
      argc--;
      argv++;
      
    }
  else 
    while(1) {
      printf("Thesaurus: ");
      fflush(stdout);
      if(fgets(input, 80, stdin) == NULL) break;
      strtok(input, "\n\r");
      str = lookup(input);
      output( str ); 
      free(str);
    }

  closelib();
}

parse_args(int argc, char *argv[])
{
  char **arg;

  arg = argv;
  *arg = *argv;
  arg++;
  argv++;

  while(--argc) {

    if( !strcmp( *argv, "-help" ) ) {
      usage();
      argv++;
      continue; 
    } else if ( !strcmp (*argv, "-info") ) {
      info();
      argv++;
      continue;
    } else if ( **argv == '-') {
      usage();
      return NULL;
    }

    *arg = *argv;
    arg++;
    argv++;
    
  }

  return 1;
}

char *lookup(char *word) 
{

  long indx = NULL;
  char *str;

  lower(word);
  if ( isliteral (word) ) {
    indx = literal_search(word);

    /* not found */
    if (indx == -1) {
      str = (char *)malloc(80);
      sprintf(str, "%s: No matches found\n", word);
      return str;
    }

    debug_printf("%d is the index\n", indx);

    fseek(libfptr, indx, 0);
    str = (char *)malloc(1024);
    fgets(str, 1024, libfptr);

    debug_printf("%s is the fgets string\n",str);
    return str;

  } else {

    indx = glob_search(word);
    return "Glob searching not implemented yet\n";
  }
  return("shouldn't get here");
  
}

output(char *str) 
{
  printf(str);
}


literal_search(char *word)
{
  int limit; 
  int indx;

  if((indxfptr = fopen(INDXFILE, "r")) == NULL) {
    output("Can't open thesaurus index file\n");   
    exit(-1);
  }

  fseek(indxfptr, 0, 2);
  debug_printf("%d = size\n", ftell(indxfptr));
  limit = ftell(indxfptr) / sizeof(struct entry_s);

  debug_printf("%d = limit\n", limit);

  indx = getindex(indxfptr, word, limit); 
  fclose(indxfptr);
  return indx;
}


/* dummies */
int isliteral (char *word)
{
  char *ptr;

  char *globs = "*?";

  while(*globs) {
    ptr = (char *)index(word, *globs);
    if(ptr) return 0;
    globs++;
  }
  
  return 1;
}

glob_search()
{
}

