PROGRAM X4NEWS;

{ dit programma maakt .X files aan voor news batches (.D) files die zijn }
{ blijven staan en waarvan geen .X file overgekomen is.                  }

USES Dos;

{---------------------------------------------------------------------------}
{ SearchDFile                                                               }
{                                                                           }
{ Deze routine doorzoekt de huidige directory naar .D files.                }
{                                                                           }
PROCEDURE SearchDFiles (UUCPName : STRING);

VAR Search : SearchRec;
    InFile : FILE;
    IORes  : BYTE;
    Buffer : ARRAY[1..50] OF CHAR;
    Read   : WORD;
    MakeX  : BOOLEAN;
    DName,
    XName  : STRING[40];
    XFile  : TEXT;
    Lp     : BYTE;

BEGIN
     { lowcase uucpname }
     FOR Lp:=1 TO Length (UUCPName) DO
         IF (UUCPName[Lp] IN ['A'..'Z']) THEN
            UUCPName[Lp]:=Chr (Ord (UUCPName[Lp])+32);

     FindFirst ('*.D',$3F,Search);
     WHILE (DosError = 0) DO
     BEGIN
          Assign (InFile,Search.Name);
          {$I-} Reset (InFile,1); {$I+} IORes:=IOResult;
          IF (IORes = 0) THEN
          BEGIN
               BlockRead (InFile,Buffer,50,Read);
               Close (InFile);

               MakeX:=FALSE;
               IF (Buffer[1] = '#') AND (Buffer[2] = '!') AND (Buffer[3] = ' ') THEN
               BEGIN
                    IF (Buffer[4] = 'r') AND (Buffer[5] = 'n') AND (Buffer[6] = 'e') THEN
                       MakeX:=TRUE; { #! rnews }

                    IF (Buffer[4] = 'c') AND (Buffer[5] = 'u') AND (Buffer[6] = 'n') THEN
                       MakeX:=TRUE; { #! cunbatch }

                    IF (Buffer[4] = 'g') AND (Buffer[5] = 'u') AND (Buffer[6] = 'n') THEN
                       MakeX:=TRUE; { #! gunbatch }
               END;

               IF (Buffer[1] = #31) AND (Buffer[2] = #157) THEN
                  MakeX:=TRUE; { compress }

               IF (Buffer[1] = #31) AND (Buffer[2] = #139) THEN
                  MakeX:=TRUE;

               IF MakeX THEN
               BEGIN
                    XName:=Copy (Search.Name,1,Pos ('.',Search.Name))+'X';
                    WriteLn ('  ',Search.Name,' -> making ',XName);

                    DName:='D.'+Copy (UUCPName,1,6)+Copy (Search.Name,2,Pos ('.',Search.Name)-2);
                    { vanaf positie 2 uit search.name, omdat de eerste }
                    { positie een extra teken ivm het mungen is.       }

                    Assign (XFile,XName);
                    ReWrite (XFile);
                    WriteLn (XFile,'U makex ',UUCPName);
                    WriteLn (XFile,'Z');
                    WriteLn (XFile,'F ',DName);
                    WriteLn (XFile,'I ',DName);
                    WriteLn (XFile,'C rnews');
                    Close (XFile);
               END;
          END;

          FindNext (Search);
     END;
END;


{---------------------------------------------------------------------------}
{ main                                                                      }
{                                                                           }

VAR Search : SearchRec;
    OldDir,
    NewDir : STRING;

BEGIN
     WriteLn ('X4NEWS v1.0.950319 - Make .X files for left news batches');
     WriteLn ('Written by Ramon van der Winkel');
     WriteLn;

     IF (ParamCount <> 1) THEN
     BEGIN
          WriteLn ('Usage:   X4NEWS <spool path>');
          WriteLn ('Example: X4NEWS C:\SPOOL');
          Halt;
     END;

     FindFirst (FExpand (ParamStr (1))+'\*.*',$3F,Search);
     WHILE (DosError = 0) DO
     BEGIN
          IF (Search.Name[1] <> '.') AND ((Search.Attr AND $10) <> 0) THEN
          BEGIN
               NewDir:=FExpand (ParamStr (1))+'\'+Search.Name;
               WriteLn (NewDir);

               GetDir (0,OldDir);
               ChDir (NewDir);
               SearchDFiles (Search.Name);
               ChDir (OldDir);
          END;

          FindNext (Search);
     END; { while }
END.
