{ Clean deleted messages out of a news file }
{ Restricted to use on the logged-in filestore }

%include "News_S:News.Inc"
%include "Inc:Util.Imp"

%external %string (80) Copyright %alias "NEWS_CRUNCH_(C)_FDC_85" = -
   "Copyright (C) 1985 Frank D. Cringle"

%record %format Index Block fm (%record (Index Block fm) %name Next,
                                %record (Index fm) %array Index(0:Entries - 1))

%constant %string Temp File = "Crunch_Temp"

%begin
   %own %string (255) Topic = ""
   %own %string (255) New Name
   %own %record (Index Block fm) Root = 0
   %own %record (Index Block fm) %name Current

   %routine Crunch File

      %predicate Build Index
         %integer Base, Last, Number, Position, l, i
         %string (255) New Name

         %routine Clear Current
            %integer i

            %for i = 0, 1, Entries - 1 %cycle
               Current_Index(i)_Number = 0
               Current_Index(i)_Length = 0
               Current_Index(i)_Position = 0
            %repeat
         %end {Clear Current}

         %on %event 3 %start
            Select Output (0)
            Print String ("Sorry, only authorised wizards can crunch topics")
            Newline
            %stop
         %finish

         %if %not Open News File (Topic, 1) %start
            Newline
            PrintString(Topic." in use")
            %false
         %else
            i = 0
            Number = 0
            Position = 0
            Base = Blocksize
            Current == Root
            Clear Current
            Last = Last Message + 1
            Select Input (News Stream)
            l = Move to Message (Last, backwards)
            %while Number < Last %cycle
               %if i > Entries - 2 %start
                  Current_Index(Entries - 1)_Position = Base
                  Base = Base + Blocksize
                  %if Current_Next == Nil %start
                     Current_Next == New (Root)
                     Current_Next_Next == Nil
                  %finish
                  Current == Current_Next
                  Clear Current
                  i = 0
               %finish
               l = Move to Message (Number, forwards)
               Current_Index(i)_Number = Number
               Current_Index(i)_Length = l
               Current_Index(i)_Position = Position
               Position = Position + l
               i = i + 1
            %repeat
         %finish
         Current == Root
         %while Current ## Nil %cycle
            %for i = 0, 1, Entries - 2 %cycle
               %exit %if Current_Index(i)_Number = 0
               Current_Index(i)_Position = Current_Index(i)_Position + Base
            %repeat
            Current_Index(Entries - 1)_Number = Last Message
            Current == Current_Next
         %repeat
         %true
      %end {Build Index}


      %routine Copy Messages
         %integer i, l, Number

         Open Output (2, News Directory.Temp File)
         Select Output (2)
         Current == Root
         %while Current ## Nil %cycle
            %for i = 0, 1, Blocksize - 1 %cycle
               Print Symbol (Byte Integer (Addr (Current_Index) + i))
            %repeat
            Current == Current_Next
         %repeat
         Number = 0
         %cycle
            Select Input (News Stream)
            l = Move to Message (Number, forwards)
            %exit %if l = 0
            %while l > 0 %cycle
               Read Symbol (i)
               Print Symbol (i)
               l = l - 1
            %repeat
         %repeat
         Close Output
         New Name = Topic.".Bak"
         l = Length (New Name)
         New Name = Substring(New Name, l - 11, l) %if l > 12
         Close News File
         Rename (News Directory.Topic, New Name)
         Rename (News Directory.Temp File, Topic)
      %end {Copy Messages}
   
      Copy Messages %if Build Index

   %end {Crunch File}


   {MAIN PROGRAM}

   Home Host = Current Host
   News Host = Home Host

   Define Param ("Topic", Topic, PAM Major!PAM No Default)

   Process Parameters (CLI Param)

   Crunch File

%end
