PROGRAM AddCun;

USES Dos;

CONST BufSize = 60000;

VAR InpFile,
    OutFile    : FILE;
    DataBuf    : ARRAY[0..BufSize-1] OF BYTE;
    BytesRead  : WORD;
    First      : BOOLEAN;
    Search     : SearchRec;
    TestString : STRING[12];
    Zoek,
    Path       : STRING[79];

BEGIN
     WriteLn ('AddCun v1.0  (c) Ramon van der Winkel ''93');
     IF (ParamCount < 2) THEN
     BEGIN
          WriteLn;
          WriteLn ('Adds the "#! cunbatch." to the .DAT file header');
          WriteLn;
     END;

     IF (ParamCount = 0) THEN
     BEGIN
          WriteLn ('Parameter required: .DAT filename');
          Halt;
     END;

     Zoek:=ParamStr (1);
     Path:='';
     WHILE (Pos ('\',Zoek) > 0) DO
     BEGIN
          Path:=Path+Copy (Zoek,1,Pos ('\',Zoek));
          Delete (Zoek,1,Pos ('\',Zoek));
     END;

     Assign (InpFile,Path+Zoek);
     Reset (InpFile,1);

     Assign (OutFile,Path+Copy (Zoek,1,Pos ('.',Zoek)-1)+'.$$$');
     ReWrite (OutFile,1);

     WriteLn ('Processing '+Path+Zoek);

     First:=TRUE;
     REPEAT
           BlockRead (InpFile,DataBuf,BufSize,BytesRead);

           IF First THEN
           BEGIN
                Move (DataBuf[0],TestString[1],12);
                TestString[0]:=#12;
                IF (TestString <> '#! cunbatch'#10) THEN
                BEGIN
                     TestString:='#! cunbatch'#10;
                     BlockWrite (OutFile,TestString[1],12);
                END;
                First:=FALSE;
           END;

           IF (BytesRead <> 0) THEN
              BlockWrite (OutFile,DataBuf,BytesRead);
     UNTIL (BytesRead = 0);

     Close (OutFile);
     Close (InpFile);
     Erase (InpFile);
     Rename (OutFile,Path+Zoek);
END.
