program add_delete_topic(input,output,tu);

{This program adds and deletes protected topics and  corresponding uic numbers,
the BB_MANAGER  privileged codes, and all boards that are not protected.  The
boards which are not protected have a USER_GROUP  number and a MEMBER_GROUP
number of -1111.  These codes  are stored in a file called TOPIC_USERS.DAT. 
This program  must be run each time a topic is added to the bulletin board.

This program normally resides in the BB manager's area.  Edit the open
statement to reflect the data area where the file TOPIC_USERS.DAT
resides.}

const
  topic_length = 16;
  key_length = 36;
  group_size = 10;

type
  topic_uic = record
              topic : packed array [1..topic_length] of char;
              user_group : integer;
              member_group : integer;
              key : [key(0)] packed array[1..key_length] of char;
            end;
   group_char = varying[group_size] of char;

var
  tu : file of topic_uic;
  temp : topic_uic;
  tutemp : topic_uic;
  action : char;
  found : boolean;
  temp_group : group_char;
  temp_member : group_char;
  i : integer;

begin

  open(tu,'system3:[bb$program.data]topic_users.dat',
       access_method:=keyed,organization:=indexed,
       history:=unknown);
  
  writeln('ENTER A FOR ADD OR D FOR DELETE (ANY OTHER KEY TO EXIT)');
  readln(action);
  while (action in ['a','A','d','D']) do
    begin

      {Read topic and uic number to add or delete from current list.}
      writeln('ENTER TOPIC');
      readln(temp.topic);
      for i:=1 to length(temp.topic) do 
        if temp.topic[i] in ['a'..'z'] then 
          temp.topic[i]:=chr(ord(temp.topic[i])-32);
      writeln('ENTER USER GROUP NUMBER');
      readln(temp.user_group);
      writeln('ENTER MEMBER GROUP NUMBER');
      readln(temp.member_group);

      {Internal read to convert to characters}
      writev(temp_group,temp.user_group);
      writev(temp_member,temp.member_group);

      {Format information to create key for accessing TU}
      temp.key:=temp.topic+temp_group+temp_member;

      {Search current list for key TEMP.KEY which contains topic, 
      user group number, and member group number.  If matching record
      is found, UFB(TU) returns false.}
      findk(tu,0,temp.key,eql);

      if (action in ['a','A']) then
        begin

          {If action is ADD, add topic and uic to file TU if new topic was 
          specified.}
          if (not ufb(tu)) then
            begin
              writeln(temp.topic,temp.user_group,temp.member_group);
              writeln('IS ALREADY ON THE LIST')
            end
          else 
            begin
              write(tu,temp);
              writeln(temp.topic,temp.user_group,temp.member_group);
              writeln('HAS BEEN ADDED');
            end;
        end
      else
        begin

          {If action is delete, delete topic and uic number
          from file TU (if it is there)}
          if (not ufb(tu)) then
            begin
              delete(tu);
              writeln(temp.topic,temp.user_group,temp.member_group);
              writeln('HAS BEEN DELETED');
            end
          else 
            begin
              writeln(temp.topic,temp.user_group,temp.member_group);
              writeln('WAS NOT FOUND');
            end;
        end;

      {Get next action to perform}
      writeln('ENTER A FOR ADD OR D FOR DELETE (ANY KEY TO EXIT)');
      readln(action);

    end;  {while}

  close(tu);
end.
