PROGRAM RAFind;

Uses
  Dos,
  Crt;

{$I-}

Type
  FlagType       = array[1..4] of Byte;
  FILESrecord    = record
                     Name           : String[30]; 
                     Typ            : Byte; 
                     FilePath,    
                     ListPath       : String[40];
                     Security       : Word; 
                     Flags          : FlagType; 
                     PrivateSecurity : Word;
                     PrivateFlags   : FlagType;
                   end; 


Var
  FileArea  : FilesRecord;
  Files     : File of FilesRecord;
  Found     : Integer;
  FilesFile : Text;


function itoa(i: longint): string;
var s : string;
begin
    str(i,s);
    itoa:=s;
end;



Function UpperCase(a:string):string;
Var
  i       : integer;
  b       : string;
Begin
  b := a;
  For i:= 0 to length(a) do
    b[i] := upcase(a[i]);
  UpperCase := b;
End;

Procedure Usage;
Begin
  Writeln(#7+'Usage :    RAFIND FILENAME.EXT');
  WriteLn;
  WriteLn('           RAFIND /K SEARCHSTRING');
  WriteLn;
  Halt(1);
End;



Procedure Find_KeyWord;
Var
  FilesFile   : Text;
  Temp,Srch   : string;
  I           : Integer;
Begin
  srch := '';
  If paramcount < 2 then Usage;
  for i:= 2 to paramcount do
  Begin
    srch := srch + paramstr(i);
    if i < paramcount then srch := srch + ' ';
  End;
  If FileArea.ListPath = '' then FileArea.ListPath := FileArea.FilePath + 'FILES.BBS';
  Temp := FileArea.ListPath;
  assign(filesfile,Temp);
  reset(filesfile);
  if IOResult > 0 then exit;
  while not eof(filesfile) do
  begin
    readln(filesfile,temp);
    i := pos(UpperCase(srch),UpperCase(temp));
    if i > 0 then
    Begin
      WriteLn;
      WriteLn(UpperCase(FileArea.FilePath));
      WriteLn(temp);
      Inc(Found);
    End;
  End;
End;


Procedure Find_File;

VAR
  TestFile : Searchrec ;

BEGIN
  If UpperCase(paramstr(1)) = '/K' then
  Begin
    Find_KeyWord;
    Exit;
  End;
  FindFirst(FileArea.FilePath+paramstr(1),ANYFILE,TestFile) ;
  IF DosError = 0 THEN
  Begin
    WriteLn(UpperCase(FileArea.FilePath+TestFile.Name));
    Inc(Found);
    While DosError = 0 DO
    Begin
      FindNext(TestFile);
      If DosError = 0 then
      Begin
        WriteLn(UpperCase(FileArea.FilePath+TestFile.Name));
        Inc(Found);
      End;
    End;
  End;
END ;



Procedure Get_File_Areas;

Var
  Path      : String[45];

Begin
  ASSIGN(Files,GETENV('RA') + '\FILES.RA');
  RESET(Files);
  If IOResult <> 0 then
  Begin
    Writeln('Cannot Find FILES.RA!'+#7);
    Halt(1);
  End;
  While Not EOF(Files) do
  Begin
    Read(Files,FileArea);
    If FileArea.Name <> '' then Find_File;
  End;
  Close(Files);
End;



BEGIN
  Assign(input,'');
  ReSet(Input);
  Assign(OutPut,'');
  ReWrite(OutPut);
  FileMode := 66;
  Writeln;
  WriteLn('  Remote Access BBS File Finder  -  RAFIND V1.00');
  WriteLn('      Copyright (C) 1990 Motor City Software');
  WriteLn;
  If ParamCount = 0 then Usage;
  Found := 0;
  Get_File_Areas;
  WriteLn;
  Writeln('Found '+itoa(Found)+' Matching Files.');
  WriteLn;
End.


