[inherit('sys$library:starlet','lib$spawn','smgdefs','smg$repaint_line',
'BB.PEN')]
module update_last_access;

[global] procedure update_last_access(updater : bbrec);

{Procedure updates the topic last access date in the user's file.}

var
   last_access_topic : newuserrec;
   look_for_rec,curr_user_file_topic : string;
   topic_long : integer;

begin
  $setprv(1,prv$m_sysprv);

  {Make a record for the topic's last access by the user.}
  last_access_topic.message_id := 
                         null_date+'LAST_ACCESS_'+updater.topic+'        ';

  {Find record in the user file area.}
  findk(newuserfile,0,last_access_topic,eql,error:=continue);

  if (not ufb(newuserfile)) then
    begin
      {Get record containing users last date of access for this topic.}
      topic_long := length(updater.topic);
      look_for_rec := 'LAST_ACCESS_'+updater.topic;

      repeat
        get(newuserfile);
        if (length(newuserfile^.message_id) >= (21+topic_long)) then
          curr_user_file_topic := 
                          substr(newuserfile^.message_id,9,12+topic_long);
      until (curr_user_file_topic = look_for_rec) or (status(newuserfile)<>0);

      last_access_topic.message_id := 
                          updater.date_posted+'LAST_ACCESS_'+updater.topic;
      {This updates the file.}
      if (status(newuserfile) = 0) then          
        delete(newuserfile,error:=continue);
      if (status(newuserfile) <= 0)then 
        write(newuserfile,last_access_topic,error:=continue);
      if (status(newuserfile) > 0)then 
        write_error('User''s info file not updated.');
    end;
  smg$erase_display(error_display);
  $setprv(0,prv$m_sysprv);
end;

end.
