#include "bbs.h"

extern BOOL DupeYes;/* Inserted By the Hodge */
extern int NumFiles;
int Dupy=0;

static UBYTE *myzip;
static void ClearZip(void);
#define ln(x,y) current=myzip+(y*100)+x;

static int zippy(char *fname, char *search_string)
{
   FILE *fi;
   UBYTE *current;
   char image[102];
   register int found=0;
   int x=1;
   int GI1;
   myzip=AllocMem(10000L,MEMF_CLEAR|MEMF_PUBLIC);
   ln(1,1);
   strupr(search_string);
   fi=fopen(fname,"r");
   if(fi==NULL) { FreeMem(myzip,10000L); return(0); }
   while(fgets(image,96,fi)!=NULL)
   {
if(CheckInput()) {
			GI1=ReadChar(1L);
			switch(GI1) {
				case '\023':	/* ctrl-s */
					GI1=ReadChar(KEYBOARD_TIMEOUT);
					if(GI1<0) {
						fclose(fi);
                              FreeMem(myzip,10000L);
						return(GI1);
					}
					break;
				case '\003':	/*  ctrl-c */
					fclose(fi);
					AEPutStr("**Break\r\n");
                         FreeMem(myzip,10000L);
					return(FAILURE);
					break;
				}
			}
     StripReturn(image);
     if(image[0]!=' ')
     {
       ln(1,x); *current='\0';
       if(found)
       {
         found=1; 
         while(1) { ln(1,found); if(*current=='\0') break; AEPutStr(current); AEPutStr("\r\n"); found++;
                    GI1=FlagPause(1);
                    if(GI1<0) {
				   fclose(fi);
                       FreeMem(myzip,10000L);
				   return(GI1); 
                    }
         }
        }
       found=0; x=1; ln(1,x);
     }
     strcpy(current,image);
     strupr(image); if(strstr(image,search_string)) found=1;
      x++;
     ln(1,x); 
  }
    ln(1,x); *current='\0';
    if(found)
       {
         found=1; while(1) { ln(1,found); if(*current=='\0') break; AEPutStr(current); AEPutStr("\r\n"); found++; 
                    GI1=FlagPause(1);
                    if(GI1<0) {
				   fclose(fi); FreeMem(myzip,10000L);
				   return(GI1); 
                    }
         }
       }
   
   fclose(fi);
   AEPutStr("\r\n");
   FreeMem(myzip,10000L);
   return(SUCCESS);
}

static void ClearZip(void)
{
   register int i;
   UBYTE *current;
 
   for(i=1;i<20;i++) { ln(1,i); *current='\0'; }
}


/***************************************************************************\
*                                                                           *
*                          int SearchString(void)                           *
*                                                                           *
\***************************************************************************/

int SearchString(void)
{
int stat,FLLoop,mystat;
char str[200],ss[80],ray[200];
char tempfile[256];
int fcopy = FALSE;

LineCount=gnsflag=0;

 AEPutStr("\r\n");
 if(MaxDirs==0) {
	    MyError(5); //Sorry();
	    return(FAILURE);
	}

 Dupy=0;
 mystat=CommandSplit();
 if(mystat>1) { stccpy(ss,Command[1],80); goto Skip1; }

 AEPutStr("Enter string to search for: ");
 stat=LineInput("",ss,78,KEYBOARD_TIMEOUT);
 if(stat<0) { return(NO_CARRIER); }
 if(strlen(ss)==0) { AEPutStr("\r\n"); return(SUCCESS); }

Skip1:
 (void)strupr(ss);
 if(mystat>2)
		stat=GetDirSpan(Command[2]);

 else	stat=GetDirSpan("\0");      /* chg to "A' to search all dirs */

 if(stat==FAILURE) return(SUCCESS);

 gnsflag=CheckForNS(MAINMENU_Li);

 if(strlen(ss)>0) {

	    FLLoop=StartDir;
	    while(FLLoop<=DirScan) {
		    strcpy(str,Conference_Location);   /* get BBS conf locale dir */
		    if(DirScan!=(-1)) {                /* add 'DIR' */
//(RTS) buff copy
         if(FLLoop == MaxDirs) {    /* at upload dir */
            strcat(str,"DIR");
	     	   sprintf(ray,"%d",FLLoop);
		       strcat(str,ray);
 		   sprintf(ray,"Scanning directory %d\r\n",FLLoop);
            AEPutStr(ray);
          /*  sprintf(tempfile,"T:tdir.%d",Cmds->AcLvl[LVL_NODE_NUMBER]);
            /* now copy to T: and use the T: */
            if((FileCopy(str,tempfile))) {
                 strcpy(str,tempfile);
                 fcopy = TRUE;
            }
          */
 		} else {

			    strcat(str,"DIR");
			    sprintf(ray,"%d",FLLoop);      /* add dir Number */
			    strcat(str,ray);               /* add path & name */
			    sprintf(ray,"Scanning directory %d\r\n",FLLoop);
			    AEPutStr(ray);
        }
			  LineCount++;
	    } 
			 if(DirScan==-1) {
			    strcat(str,"hold/held");
			    AEPutStr("Scanning directory HOLD\r\n");
			}
		    stat=zippy(str,ss);
		    if(stat<0) {
			    AEPutStr("\r\n");
             if(fcopy)      DeleteFile(tempfile);  //(RTS)
			    return(stat);
			}
          
          FLLoop +=1;
		//if(DirScan!=-1) FLLoop+=1;
		}
	}
 AEPutStr("\r\n");
 if(fcopy)      DeleteFile(tempfile);  //(RTS)
 return(SUCCESS);
}
