/* $VER: MsgSplit 37.2 (19.8.95)
**
** Written by Stelios Melissakis for Thor 2.x.
**
** Many-many thanks to Kjell Irgens & the rest of Thor Team :)
**
**/

Options Results

Arg MaxSize .

If (MaxSize = '' | MaxSize = '?') Then
Do
  Say 'MsgSplit v37.1 (19.8.95)'
  Say 'Usage: MsgSplit MAXSIZE/A'
  Exit(0)
End


p = Address()||' '||Show('P',,)
If Pos('THOR.',p)>0 Then 
   ThorPort = Word(SubStr(p,Pos('THOR.',p)),1)
Else 
Do
   Say 'No THOR port found!'
   Exit(0)
End
     


If ~Show('p', 'BBSREAD') Then
Do
  Address Command 
  "Run >NIL: `GetEnv THOR/THORPath`bin/LoadBBSRead"
  "WaitForPort BBSREAD"
End



Address(ThorPort)

Drop CurStem.

CurrentSystem CurStem
If (rc = 30) Then
Do
  Say Thor.LastError
  Exit(0)
End

Eve_EnterMsg = 0
Eve_ReplyMsg = 1
EDB_Deleted = 0
UT_MsgFile = 1

Drop BBSData.
Drop EventData.
Drop EventTags.
Drop UniqueStem.

CR = '0a'x

ContNextMsg = CR || '<< Continued in Next Message >>' || CR
ContPrevMsg = '<< Continued from Previous Message >>'


Address("BBSREAD")

GetBBSData '"'CurStem.BBSName'"' Stem BBSData
If (rc ~= 0) Then
Do
   Say BBSREAD.LASTERROR
   Exit(0)
End

If BBSData.NumEvents = "BBSDATA.NUMEVENTS" Then Exit(0)

Address(ThorPort)
CurMsg = 0
OpenProgress Title '"Splitting messages..."' Total BBSData.NumEvents At "_Abort" ProgressCharWidth 40
If rc = 0 Then 
   Window = Result
Else 
Do
   RequestNotify '"OpenProgress:\n'Thor.LastError'"' '"_Abort"'
   Exit(0)
end

Address("BBSREAD")
Do n = BBSData.FirstEvent To BBSData.LastEvent

   CurMsg = CurMsg + 1

   ReadBREvent bbsname '"'CurStem.BBSName'"' EventNR n TagsStem EventTags DataStem EventData
   If (rc ~= 0) Then 
   Do
      Say BBSRead.LastError
      Exit(0)
   End
   
   If (~BITTST(EventData.Flags, EDB_Deleted) & ((EventData.EventType = Eve_EnterMsg) | (EventData.EventType = Eve_ReplyMsg))) Then 
   Do
      FName = BBSData.BBSPath || EventTags.MsgFile
      
      FSize = FileSize(FName)

      If FSize > MaxSize Then 
      Do
         Drop WtStem.
         
         Pieces = FSize % MaxSize
         
         If (((FSize / MaxSize) - (FSize % MaxSize)) ~= 0) Then Pieces = Pieces + 1
           
         bc = 1
         
         If Open(Source, FName, 'R') Then
         Do
            Do Until EOF(Source)
               If ~Open(Dest, 'T:Thor_TMP_Msg', 'W') Then
               Do
                  Say 'I can not open temporary file.'
                  Call Close(Source)
                  Exit(30)
               End
            
               If bc > 1 Then 
               Do
                  Call WriteLn(Dest, ContPrevMsg || ' (' || bc || '/' || Pieces || ')' CR)
               End
               
               TotSize = 0
               
               Do Until (TotSize > (MaxSize - 170)) | EOF(Source)
                  TotSize = TotSize + WriteLn(Dest, ReadLn(Source))
               End
               
               If ~EOF(Source) Then Do
                  Call WriteLn(Dest, ContNextMsg)
               End
               
               Call Close(Dest)
               
               UniqueMsgFile BBSName '"'CurStem.BBSName'"' Stem UniqueStem UseTag UT_MsgFile FromFile 'T:Thor_TMP_Msg'
               If (rc ~= 0) Then
               Do
                 Say BBSRead.LastError
                 Exit(0)
               End
               
               WtStem.ToName = EventTags.ToName
               WtStem.Subject = EventTags.Subject
               WtStem.Conference = EventTags.Conference
               WtStem.MsgFile = UniqueStem.FilePart
               WtStem.Private = EventTags.Private
               WtStem.Date = EventTags.Date
               WtStem.RefNr = EventTags.RefNr
               WtStem.RefOrginalNr = EventTags.RefOrginalNr
               
               If EventTags.PGPSignID ~= "EVENTTAGS.PGPSIGNID" Then
                  WtStem.PGPSignID = EventTags.PGPSignID
               
               If EventTags.PGPEncryptID ~= "EVENTTAGS.PGPENCRYPTID" Then
                  WtStem.PGPEcryptID = EventTags.PGPEcryptID
               
               WriteBREvent BBSName '"'CurStem.BBSName'"' Event EventData.EventType Stem WtStem
               If (rc ~= 0) Then
               Do
                 Say BBSRead.LastError
                 Exit(0)
               End
               
               bc = bc + 1
            End

            Call Close(Source)
         End
         Else
         Do
            Say 'I can not open file ' FName
         End
         
         UpdateBREvent BBSname '"'CurStem.BBSName'"' EventNr n SetDeleted
         If (rc ~= 0) Then
         Do
           Say BBSRead.LastError
           Exit(0)
         End
      End
   End
   Else CurMsg = CurMsg + 1
   Address(ThorPort)
   UpdateProgress Req Window Current CurMsg PT '"Message 'CurMsg' of 'BBSData.NumEvents'(OrgMsg:'n')"'
   If rc ~= 0 Then Exit
   Address("BBSREAD")   
End

If Window ~= 0 Then 
   If Window ~= 'WINDOW' Then 
   Do
     Address(ThorPort)
	CloseProgress Req Window
	Window = 0
   End
Exit

/***************************************************************************/
/*                               Procedures                                */
/***************************************************************************/

FileSize: Procedure
  Arg fn
  FData = StateF(fn)
  FSpc = Pos(' ', FData)
  LSpc = Pos(' ', FData, FSpc + 1)
Return SubStr(FData, FSpc+1, LSpc-FSpc-1)

/***************************************************************************/
