[INHERIT('BB.PEN')]
module create_message_dir_list;

[global] 
procedure create_message_dir_list;

{Create a linked list of all the messages in the selected topic.  Each
message is marked as NEW if added since the last access of the topic by
the user, SKIPPED if not selected to read on a previous access of the
topic, or READ if previously read.}

var
  message_class : integer;
  skip_before, read_before : integer;

begin

  {Delete nodes from old topic list if there is one.}
  dir_curr := dir_root^.next;
  while (dir_curr <> nil) do
    begin
      dir_prev := dir_curr;
      dir_curr := dir_curr^.next;
      dispose(dir_prev);
    end;

  {Initialize the head node of the topic list.}
  dir_root^.next := nil;
  dir_curr := dir_root;
  message_count := 0;
  new_message_count := 0;
  skip_before := 0;
  read_before := 0;

  {Locate the first message in this topic if there are messages for the
   topic selected.  If topic contains messages, build linked list of
   header information for the message menu screen.}
  findk(bbfile,1,current_topic+null_date,gtr,error:=continue);
  if not ufb(bbfile) then
    begin
      with current_record do
        begin
          repeat 
            count := 0;
            repeat
              read(bbfile,current_record,error:=continue);
              if (topic = current_topic) and (status(bbfile) < 1) then
                begin
                  message_count := message_count + 1;
                  count := count+1;
                  dir_prev := dir_curr;
                  new(dir_curr);
                  dir_prev^.next := dir_curr;
                  dir_curr^.previous := dir_prev;
                  dir_curr^.number := message_count;
                  dir_curr^.date := date;
                  dir_curr^.topic := topic;
                  dir_curr^.poster := poster;
                  dir_curr^.subject := subject;
                  dir_curr^.last_date_accessed := date_posted;


                  {Calculate what type message we are looking at:
                 	Read    = 1
                 	New     = 2
                 	Skipped = 3

                   Message logged into the users area?}
                   findk(newuserfile,0,
                         current_record.date_posted+current_record.topic,
                         eql,error:=continue);
  
                   if not(ufb(newuserfile)) then     {message read before}
                     dir_curr^.class := 1
                   else 
                     if (topic_list^.last_date_accessed < current_record.date) then 
                       dir_curr^.class := 2   {new message}
                     else      {message is a Skipped one}
                       dir_curr^.class := 3;

                  if (dir_curr^.class = 1) then
                    read_before := read_before + 1
                  else
                    if (dir_curr^.class = 2) then
                      new_message_count := new_message_count + 1
                    else
                      skip_before := skip_before + 1;

                  {Get next message in this topic if there is one.}
                  findk(bbfile,1,topic+date,gtr,error:=continue);

                end;
            until (count = display_height) or (topic <> current_topic) or
                  ufb(bbfile) or (status(bbfile) > 0);
          until (topic <> current_topic) or ufb(bbfile) or (status(bbfile) > 0);
        end;
    end;

  {Set counters in head node.}
  topic_list^.message_counters.read_message := read_before;
  topic_list^.message_counters.skipped_message := skip_before;
  topic_list^.message_counters.new_message := new_message_count;
  topic_list^.message_counters.total_message := new_message_count +
                                                skip_before +
                                                read_before;

  dir_curr^.next := nil;
  actual_message_count := topic_list^.message_counters.total_message;
  dir_curr := dir_root;

  if (select_first_time = true) then
    begin
      {This finds the first new record in the selection.  On the first
       message menu screen after selection of a topic, the menu begins
       with the first new message if there is one.  If there is no new
       message, then the message list will start with message one.}
      while (dir_curr^.next <> nil) and (dir_curr^.class <> 2) do
        dir_curr := dir_curr^.next;
      if (dir_curr^.class <> 2) then dir_curr:=dir_root;
      dir_list := dir_curr;
      dir_start := dir_curr;
      select_first_time := false;
    end;

end;
end.
