{$IFDEF WtrGate}{$IFDEF UseOvr}{$O+,F+}{$ENDIF}{$ENDIF}
PROGRAM DelCun;

USES Dos;

CONST BufSize = 10000;

VAR InpFile,
    OutFile    : FILE;
    DataBuf    : ARRAY[0..BufSize-1] OF BYTE;
    BytesRead  : WORD;
    StartPos   : WORD;
    Search     : SearchRec;
    TestString : STRING[12];
    Zoek,
    Path       : STRING[79];
    Dir        : DirStr;
    Name       : NameStr;
    Ext        : ExtStr;
    Ok         : BOOLEAN;

BEGIN
     WriteLn ('DelCun v1.4  (c) Ramon van der Winkel ''93-''96');
     WriteLn;

     IF (ParamCount <> 1) THEN
     BEGIN
          WriteLn ('Removes the "#! [cgz]unbatch[<CR>]<LF>" from the .D file header');
          WriteLn;
          WriteLn ('Usage: delcun [<.d filename>]');
          WriteLn;
          WriteLn ('Example: delcun 0a24ef.d');
          WriteLn ('Example: delcun *.d');
          WriteLn;
          WriteLn ('The processed file will be stored as .dz');
          Halt;
     END;

     Zoek:=FExpand (ParamStr (1));
     FSplit (Zoek,Dir,Name,Ext);

     WriteLn ('Searching for ',Zoek);

     FindFirst (Zoek,$3F,Search);
     WHILE (DosError = 0) DO
     BEGIN
          IF ((Search.Attr AND $10) <> 0) THEN
          BEGIN
               FindNext (Search);
               Continue;
          END;

          WriteLn ('Processing '+Dir+Search.Name);
          Assign (InpFile,Dir+Search.Name);
          Reset (InpFile,1);

          Assign (OutFile,Dir+Copy (Search.Name,1,Pos ('.',Search.Name))+'DZ');
          ReWrite (OutFile,1);

          StartPos:=12;
          REPEAT
                BlockRead (InpFile,DataBuf,BufSize,BytesRead);

                IF (StartPos > 0) THEN
                BEGIN
                     Move (DataBuf[0],TestString[1],12);
                     TestString[0]:=#12;

                     Ok:=(Copy (TestString,1,3) = '#! ') AND
                         (Copy (TestString,5,7) = 'unbatch');

                     IF (TestString[12] = #13) THEN
                        Inc (StartPos); { CR,LF }

                     IF (NOT Ok) THEN
                        StartPos:=0; { laten staan }
                END;

                IF (BytesRead <> 0) THEN
                BEGIN
                     BlockWrite (OutFile,DataBuf[StartPos],BytesRead-StartPos);
                     StartPos:=0;
                END;
          UNTIL (BytesRead = 0);

          Close (OutFile);
          Close (InpFile);
          Erase (InpFile);

          FindFirst (Zoek,$3F,Search);
     END; { while }
END.
