PROGRAM RECOVER;

CONST
       END_OF_FILE	=    $1A;
       SPACE   		=    ' ';
       (* change size paramater for larger buffer *)
       SIZE             =   2047;
       SEQUENTIAL       =  -1; 

TYPE 
     PSTRG     =  ^STRING;
     BUFFER_ARRAY = packed ARRAY [1..size] OF char;

VAR  
     COMMAND_LINE,INPUT_NAME,OUTPUT_NAME,EXTENSION    	 
			 : STRING;
     I,J,K               : INTEGER;
     IOR                 : INTEGER;
     block               : Integer;
     POINTER_TO_COMMAND  :    PSTRG ;
     infile              : file of buffer_array;
     outfile             : file of char;
     crt                 : text; 
     value               : char;
     abort               : boolean;

EXTERNAL FUNCTION @CMD   : PSTRG;

BEGIN
   (* DECODE THE COMMAND LINE   *)
   POINTER_TO_COMMAND := @CMD;
   COMMAND_LINE :=     POINTER_TO_COMMAND^      ;
   EXTENSION := '.DBF';

   assign(crt,'CON:');
   reset(crt);
   writeln(crt,'RECOVER - a program to recover trashed dBASEII tm databases');  
   writeln(crt,'Brett Bonner - Memphis, TN');
   if (length(command_line) = 0) then
      begin
        writeln(crt,'To use RECOVER supply a origin file and a destination file.');
        writeln(crt,'  RECOVER will automatically place the .dbf extension');
        writeln(crt,'  on the file names');
        writeln(crt);
        writeln(crt,'  ex. d>RECOVER d:infile d:outfile');
        writeln(crt);
        writeln(crt,'  RECOVER will remove all end of file markers in a database,');
        writeln(crt,'  and change the number of records to 65532. ');
        writeln(crt,'  You will have some garbage at the end of your file so');
        writeln(crt,'  after using RECOVER:');
        writeln(crt,'    1.  go into dbase');
        writeln(crt,'    2.  DELETE the garbage');
        writeln(crt,'    3.  and COPY to a newfile');
        exit
      end;  

   WHILE COMMAND_LINE[1] = SPACE DO DELETE(COMMAND_LINE,1,1);
   INPUT_NAME := COPY(COMMAND_LINE,1,POS(SPACE,COMMAND_LINE)-1);
   INPUT_NAME := CONCAT(INPUT_NAME,EXTENSION);
   DELETE(COMMAND_LINE,1,POS(SPACE,COMMAND_LINE));
   WHILE COMMAND_LINE[1] = SPACE DO DELETE(COMMAND_LINE,1,1);
   OUTPUT_NAME := CONCAT(COMMAND_LINE,EXTENSION);
   writeln(crt,'Reading ',input_name,' and writeing ',output_name);
   (* ASSIGN FILES *)
   writeln(crt,'Assigning files');
   open(infile,INPUT_NAME,ior);
   if ior = 255 then
     begin
	writeln(crt,input_name,' does not exist - RECOVER aborted');
        exit
     end;
   assign(outfile,output_name);
   rewrite(outfile);
   writeln(crt,'Beginning transfer');
   abort := false;
   i := 1; 
   WHILE (NOT EOF(infile)) and (not abort) DO
      BEGIN
         value := gnb(infile);
         if value = chr(end_of_file) then value := space;
         case i of 
           1 : i := i + 1;
           2 : begin
		value := chr($ff);
                i := i+1;
                end;
           3 : begin
		value := chr($ff);
                i := i+1;
	       end;	
          end;     

         IF wnb(outfile,value) then
           begin
             writeln(crt,'Error writing character - RECOVER aborted');
             abort := true; 
	   end;
       END;
    CLOSE(outfile,IOR); 
    if (ior = 255) and (not abort) then
     writeln(crt,'Error closing ',output_name) else
     writeln(crt,'RECOVER compeleted normally');

END.

