Program NetMailTosser;
Uses Crt, Dos;

Const
                         Ext = '.msg';
      CfgFileExist : Boolean = True;
                  UserPiece1 = 'Sashka Yackubtc';
                  UserPiece2 = 'Mitya Ostrovsky';
                  UserPiece3 = 'Anna Yackubtchi';
                  MailPath1  = 'SWIN\';
                  MailPath2  = 'MITYA\';
                  MailPath3  = 'ANNA\';

Var

                         LTF : Text;
                       F0,F2 : Text;
         NumRead, NumWritten : Word;
                         Buf : Array[1..8192] of Char;
                    DT1, DT2 : DateTime;
                      T1, T2 : LongInt;
                          SR : SearchRec;
                    MailPath : String[80];

Procedure GetMesgAndHalt;
 Begin
  WriteLn(#7#7#7,' IO ERROR ! PROGRAM HALTED ! ');
  Halt;
 End;

Procedure ReWriteCfgFile;
 begin
  ReWrite( LTF );
   If IOResult<>0 then GetMesgAndHalt;
   CfgFileExist := False;
    GetTime(D1.Hour,D1.Minute,D1.Second,D1.Sec100);
    If (DT1.Hour=23) and (DT1.Minute=59) and (DT1.Second>50) then SetTossTime;
    PackTime( DT1, T1 );
    WriteLn( LTF, T1 );}
    WriteLn( LTF, 'CFG FILE FOR MAILTOSS.EXE.');
    WriteLn( LTF, 'DO NOT ERASE ! ');
    Close( LTF );
    If IOResult<>0 then GetMesgAndHalt;
 end;

Procedure GetTossTime;
 Begin
  Assign( LTF, 'lasttoss.cfg');
  If FSearch('lasttoss.cfg','')='' then ReWriteCfgFile;
   Reset( LTF );
   If IOResult<>0 then GetMesgAndHalt;
     GetFTime ( LTF, T1 );
  End;

Procedure CopyFile (  Var Fs, Fd : File );
 Begin
    Reset( Fs, 1 );
    ReWrite( Fd, 1 );
    Repeat
     BlockRead(Fs, Buf, SizeOf(Buf), NumRead);
     BlockWrite(Fd, Buf, NumRead, NumWritten);
    Until (NumRead = 0) or (NumWritten <> NumRead);
    Close( Fs );
    Close( Fd );
 End;

Procedure LookAndCopy( FileName : String );
Const
       StringSize = 15;
Var
        Addressee, Sender : String[StringSize];
                        N : Word;
                      Buf : Char;
                   F01,F21: File;
 Begin

 Reset(F0);
 Addressee := '';
  Sender := '';
  For N := 1 to StringSize do
                            begin
                             Read(F0,Buf);
                             Sender := Sender + Buf;
                            end;
  For N := 1 to 36-StringSize do Read(F0, Buf);
  For N := 1 to StringSize do
                            begin
                             Read(F0,Buf);
                             Addressee := Addressee + Buf;
                            end;

   Close(F0);
   Write(FileName+' - ');
   WriteLn('From :',Sender,'  To: ',Addressee);
   MailPath := MailPath1;
   If (Addressee = UserPiece2) or (Sender=UserPiece2)
                              then MailPath := MailPath2;
   If (Addressee = UserPiece3) or (Sender = UserPiece3)
                           then MailPath := MailPath3;
   Assign(F01,FileName);
   Assign(F21, MailPath+FileName);
   CopyFile(F01, F21);
 End;


Procedure WorkWith( FileName : String );
 Begin
  Assign( F0, FileName );
  If not CfgFileExist then LookAndCopy( FileName )
                      else
                       begin
                       Reset(F0);
                        GetFTime(F0, T2);
                        Close(F0);
                        If T2 > T1 then LookAndCopy( FileName );
                       end;
 End;

Procedure Scan;
 Begin
  FindFirst('*'+Ext,Archive, SR );
   while DosError = 0 do
    begin
      WorkWith( SR.Name );
      FindNext( SR );
    end;
 End;


 Begin
  GetTossTime;
  Scan;
  ReWriteCfgFile
 End.