#include "filedoor_structs_defines.h"
#include "filedoor_protos.h"

#include <proto/dos.h>
#include <strings.h>
#include <ctype.h>
#include <stdio.h>

/* returnerer true hvis fundet */
/* argumentet cont angiver om der fortsættes en gammel søgning. alternativt startes en ny */
BOOL Search(BOOL cont)
{
static BOOL previous=FALSE;
static BOOL filematch;
static BOOL global;
static BOOL descmatch;
static char searchpattern[200];
static char unparsed[51];
static char tempstr[80];
struct Filemem *temp,*temp2;
struct Areamem *area;
BOOL found;
int i;

  /** Find fil, global wildcard søgning **/
  /* Forsøg på at fortsætte på noget der ikke er begyndt */
  if(!previous && cont) return(TRUE);
  ClearMenuWindow("Search for file");
  if(cont)
  {
    temp=MoveForward(topfile,current+1);
    area=activearea;
  }
  else
  {
    strcpy(unparsed,"#?");
    prompt(MENU"\n"RI"Enter search pattern:"FG7,unparsed+2,50);
    strcat(unparsed,"#?");
    ParsePatternNoCase(unparsed,searchpattern,200);
    ClearMenuWindow("Search for file");
    hotkey(KNAP"\n"RI"(g)"MENU" Global or "KNAP"(L)"MENU" Local search",unparsed);
    if(toupper(unparsed[0])=='G')
    {
      PurgeFiles(activearea);
      area=firstarea;
      FindFiles(area);
      global=TRUE;
    }
    else
    {
      global=FALSE;
      area=activearea;
    }
    msg(ESC"[2;2H"KNAP"(f)"MENU" Match file "KNAP"(d)"MENU" Match desc ");
    hotkey(KNAP"(A)"MENU" Match anything",unparsed);
    switch(toupper(unparsed[0]))
    {
      case 'F':
      filematch=TRUE;
      descmatch=FALSE;
      break;
      
      case 'D':
      filematch=FALSE;
      descmatch=TRUE;
      break;
      
      case 'A':
      filematch=descmatch=TRUE;
      break;
      
      default:
      filematch=descmatch=TRUE;
      break;
    }
    temp=(struct Filemem *)area->files.mlh_Head;
  }
  found=FALSE;
  while(!found && area!=NULL)
  {
    sprintf(tempstr,ESC"[2;2H"FG3"Searching area : %-32.32s",area->area.name);
    msg(tempstr);
    while(temp->n.mln_Succ!=NULL)
    {
      if(filematch && MatchPatternNoCase(searchpattern,temp->f.name)) break;
      if(descmatch && MatchPatternNoCase(searchpattern,temp->f.shortdesc)) break;
      temp=(struct Filemem *)temp->n.mln_Succ;
    }
    if(temp->n.mln_Succ!=NULL)
    { /* Vi fandt en.. */
      activearea=area;
      found=TRUE;
      previous=TRUE;
      i=0;
      topfile=temp2=(struct Filemem *)area->files.mlh_Head;
      while(temp!=temp2)
      {
        temp2=(struct Filemem *)temp2->n.mln_Succ;
        i++;
        if(i%visiblelines==0) topfile=temp2;
      }
      current=i%visiblelines;
    }
    else
    {
      if(global)
      {
        PurgeFiles(area);
        area=area->next;
        FindFiles(area);
        temp=(struct Filemem *)area->files.mlh_Head;
        previous=FALSE;
      }
      else break;
    }
  }
  if(!found)
  {
    if(global) PurgeFiles(area);
    previous=FALSE;
    SearchHelp();
    FindFiles(activearea);
    topfile=(struct Filemem *)activearea->files.mlh_Head;
    current=0;
    return(FALSE);
  }
  else
  {
    return(TRUE);
  }
}
