C
C  BULLETIN0.FOR, Version 12/27/94
C  Purpose: Contains subroutines for the BULLETIN utility program.
C  Environment: VAX/VMS
C  Programmer: Mark R. London
C
C  Copyright (c) 1990
C  Property of Massachusetts Institute of Technology, Cambridge MA 02139.
C  This program cannot be copied or distributed in any form for non-MIT
C  use without specific written approval of MIT Plasma Fusion Center
C  Management.
C
	SUBROUTINE DELETE_MSG
C
C  SUBROUTINE DELETE_MSG
C
C  FUNCTION:  Deletes a bulletin entry from the bulletin file.
C
	IMPLICIT INTEGER (A - Z)

	COMMON /POINT/ BULL_POINT

	COMMON /BULLPAR/ BULL_PARAMETER,LEN_P
	CHARACTER*64 BULL_PARAMETER

	COMMON /NODE_INFO/ NODES,LOCAL_NODE_FOUND,NODE_NUM,
     &				NODE_ERROR,POINT_NODE
	CHARACTER*32 NODES(10)
	LOGICAL LOCAL_NODE_FOUND,NODE_ERROR

	COMMON /DECNET/ DECNET_PROC,ERROR_UNIT
	LOGICAL DECNET_PROC

	INCLUDE 'BULLDIR.INC'

	INCLUDE 'BULLUSER.INC'

	INCLUDE 'BULLFOLDER.INC'

	COMMON /REMOTE_FOLDER/ REMOTE_SET,REMOTE_UNIT

	EXTERNAL CLI$_ABSENT

	CHARACTER ANSWER*4,REMOTE_USER*12,SUBJECT*56

	IMMEDIATE = 0
	IF (CLI$PRESENT('IMMEDIATE')) THEN
	   IF (REMOTE_SET.EQ.4) THEN
	      WRITE (6,'('' IMMEDIATE not valid for news group.'')') 
	      RETURN
	   ELSE
	      IMMEDIATE = 1
	   END IF
	END IF

	IF (CLI$PRESENT('NODES')) THEN	! Delete messages on DECNET node?
	   CALL DELETE_NODE		! Yes...
	   RETURN
	ELSE IF (DECNET_PROC) THEN	! Is this from remote node?
	   IER = CLI$GET_VALUE('SUBJECT',SUBJECT,SLEN)
	   CALL STR$UPCASE(SUBJECT,SUBJECT)
	   CALL OPEN_BULLDIR
	   CALL READDIR(0,IER)
	   DEL_BULL = 0
	   IER = 1
	   DO WHILE (DEL_BULL+1.EQ.IER)
	      DEL_BULL = DEL_BULL + 1
	      CALL READDIR(DEL_BULL,IER)
	      CALL STR$UPCASE(DESCRIP,DESCRIP)
	      IF (DEL_BULL+1.EQ.IER.AND.USERNAME.EQ.FROM
     &		   .AND.INDEX(DESCRIP,SUBJECT(:SLEN)).GT.0) THEN
	         CALL REMOVE_ENTRY(DEL_BULL,DEL_BULL,DEL_BULL,IMMEDIATE)
		 CALL CLOSE_BULLDIR
	         WRITE (5,'(''END'')')	! Tell DECNET that delete went ok.
		 RETURN
	      END IF
	   END DO
	   CALL CLOSE_BULLDIR		! Specified message not found,
	   WRITE(ERROR_UNIT,1030)	! so error out.
	   RETURN
	END IF

C
C  Get the bulletin number to be deleted.
C

	IER = CLI$GET_VALUE('BULLETIN_NUMBER',BULL_PARAMETER,LEN_P)
	IF (IER.NE.%LOC(CLI$_ABSENT)) THEN	! Was bulletin specified?
	   CALL GET_2_VALS(BULL_PARAMETER,LEN_P,SBULL,EBULL,IER)
	ELSE IF (CLI$PRESENT('ALL')) THEN
	   SBULL = 1
	   EBULL = F_NBULL
	   IER = 0
	ELSE IF (BULL_POINT.EQ.0) THEN	! No.  Have we just read a bulletin?
	   WRITE(6,1010)		! No, then error.
	   RETURN
	ELSE
	   SBULL = BULL_POINT		! Delete the file we are reading
	   EBULL = SBULL
	   IER = 0
	END IF

	IF (SBULL.LE.0.OR.IER.NE.0) THEN
	   WRITE (6,1020)
	   RETURN
	ELSE IF (EBULL.GT.F_NBULL.AND..NOT.REMOTE_SET.AND.
     &						SBULL.NE.EBULL) THEN
	   WRITE (6,'('' Last message specified > number in folder.'')')
	   WRITE (6,'('' Do you want to delete to end of folder? '',$)')
	   READ (5,'(A)',IOSTAT=IER) ANSWER
	   CALL STR$UPCASE(ANSWER,ANSWER)
	   IF (ANSWER(:1).NE.'Y') THEN
	      WRITE (6,'('' Deletion aborted.'')')
	      RETURN
	   ELSE
	      EBULL = F_NBULL
	   END IF
	END IF

C
C  Check to see if specified bulletin is present, and if the user
C  is permitted to delete the bulletin.
C

	IF (REMOTE_SET) THEN
	   IF (SBULL.NE.EBULL) THEN
	      WRITE (6,1025)
	      RETURN
	   END IF
	   IF (SBULL.NE.BULL_POINT) CALL READDIR(SBULL,IER)
	   CALL REMOTE_DELETE(SBULL,IMMEDIATE,DESCRIP,I,FOLDER1_COM,IER)
	   IF (IER.EQ.0.AND.REMOTE_SET.LT.3) THEN
	      IF (I.EQ.LEN(FOLDER1_COM)) THEN
	         IER = SYS$ASCTIM(,INPUT,F1_NEWEST_BTIM,)
	         NEWEST_EXDATE = INPUT(:11)
	         NEWEST_EXTIME = INPUT(13:23)
	         NBULL = F1_NBULL
	   	 CALL UPDATE_FOLDER
	      ELSE
	  	 WRITE (6,'(1X,A)') FOLDER1_COM(:I)
	      END IF
	   ELSE IF (IER.NE.0) THEN
	      CALL DISCONNECT_REMOTE
	   END IF
	   RETURN
	END IF

	CALL OPEN_BULLDIR

	CALL READDIR(0,IER)

	BULL_DELETE = SBULL - 1
	DO WHILE (BULL_DELETE.LT.EBULL)
	   BULL_DELETE = BULL_DELETE + 1
	   DO WHILE (BULL_DELETE+1.NE.IER)
	      CALL READDIR(BULL_DELETE,IER)	! Get info for bulletin
	      IF (IER.NE.BULL_DELETE+1) THEN	! Was bulletin found?
	         IF (REMOTE_SET.EQ.4) THEN
	            BULL_DELETE = BULL_DELETE + 1
	            IF (BULL_DELETE.GT.EBULL) THEN
		       CALL CLOSE_BULLDIR
		       RETURN
		    END IF
	         ELSE
	            IF (.NOT.CLI$PRESENT('ALL')) WRITE(6,1030) 
	            CALL CLOSE_BULLDIR		! If not, then error out
	            RETURN
	         END IF
	      END IF
	   END DO

	   IF (USERNAME.NE.FROM.OR.(REMOTE_SET.EQ.4.AND.
     &		.NOT.TEST_NEWS_OWNER())) THEN	! If not owner of message
	      CALL STR$UPCASE(REMOTE_USER,FROM)
	      IF ((.NOT.SETPRV_PRIV().AND..NOT.FOLDER_SET).OR.    ! Privileges?
     &	       (.NOT.FOLDER_ACCESS(USERNAME,FOLDER_FLAG,FOLDER_OWNER)
     &		.AND.FOLDER_SET)) THEN
	         WRITE(6,1040)		! No, then error out.
	         CALL CLOSE_BULLDIR
		 RETURN
	      ELSE IF (SBULL.EQ.EBULL) THEN
		 IF (TRIM(FROM).EQ.1) THEN
		    CALL OPEN_BULLFIL
	            ILEN = LINE_LENGTH + 1
	            CALL GET_BULL_LINE(BLOCK,LENGTH,INPUT,ILEN)
	            CALL CLOSE_BULLFIL
	            ASK = ILEN.EQ.0.OR.INPUT(:6).NE.'From: '
		 ELSE
		    ASK = REMOTE_USER.EQ.FROM
		 END IF
		 IF (ASK) THEN
	            CALL CLOSE_BULLDIR
	            WRITE (6,1050)	! Make sure user wants to delete it
	            READ (5,'(A)',IOSTAT=IER) ANSWER
	            CALL STR$UPCASE(ANSWER,ANSWER)
	            IF (ANSWER(:1).NE.'Y') RETURN
	            CALL OPEN_BULLDIR
	            CALL READDIR(BULL_DELETE,IER)
	            IF (IER.NE.BULL_DELETE+1) THEN	! Was bulletin found?
	               WRITE(6,1030)	! If not, then error out
	               CALL CLOSE_BULLDIR
		       RETURN
	            END IF
	         END IF
	      END IF
	   END IF

C
C  Delete the bulletin directory entry.
C
	   CALL REMOVE_ENTRY(BULL_DELETE,SBULL,EBULL,IMMEDIATE)
	   IF (REMOTE_SET.EQ.4) THEN
	      IF (.NOT.CLI$PRESENT('LOCAL').AND.TEST_NEWS_OWNER()) THEN
	         CALL REMOTE_DELETE
     &		    (BULL_DELETE,IMMEDIATE,DESCRIP,I,FOLDER1_COM,IER)
	      END IF
	   END IF
	END DO

	CALL CLOSE_BULLDIR
	RETURN

1010	FORMAT(' ERROR: You are not reading any message.')
1020	FORMAT(' ERROR: Specified message number has incorrect format.')
1025	FORMAT(' ERROR: Cannot delete multiple messages in remote folder.')
1030	FORMAT(' ERROR: Specified message was not found.')
1040	FORMAT(' ERROR: Message was not deleted. Not owned by you.')
1050	FORMAT(' Message is not owned by you.',
     &	       ' Are you sure you want to delete it? ',$)

	END



	SUBROUTINE REMOVE_ENTRY(BULL_DELETE,SBULL,EBULL,IMMEDIATE)

	IMPLICIT INTEGER (A-Z)

        INCLUDE 'BULLFOLDER.INC'

	INCLUDE 'BULLDIR.INC'

	COMMON /POINT/ BULL_POINT

	COMMON /REMOTE_FOLDER/ REMOTE_SET,REMOTE_UNIT

	INTEGER NOW(2),EX(2)

	IF (IMMEDIATE.EQ.1) THEN		! Delete it immediately

	   CALL DELETE_ENTRY(BULL_DELETE)	! Delete the directory entry

	   IF ((SYSTEM.AND.4).EQ.4) THEN	! Was entry shutdown bulletin?
	      SHUTDOWN = SHUTDOWN - 1		! Decrement shutdown count
	   END IF
	ELSE				! Delete it eventually
C
C  Change year of expiration date of message to 100 years less,
C  to indicate that message is to be deleted.  Then, set expiration date
C  in header of folder to 15 minutes from now.  Thus, the folder will be
C  checked in 15 minutes (or more), and will delete the messages then.
C
C  NOTE: If some comic set their expiration date to > 1999, then
C  the deleted date will be set to 1899 since can't specify date <1859.
C

	   IF ((SYSTEM.AND.7).LE.1) THEN	! General or System message
	      EXDATE = EXDATE(:7)//'18'//EXDATE(10:)
	      IF (EXDATE(10:10).LT.'6') EXDATE(10:11) = '99'
	   ELSE				! Permanent or Shutdown
	      IF (EXDATE(2:2).EQ.'-') THEN
	         EXDATE = EXDATE(:6)//'19'//EXDATE(9:)
	      ELSE
	         EXDATE = EXDATE(:7)//'19'//EXDATE(10:)
	      END IF
	   END IF

	   CALL WRITEDIR(BULL_DELETE,IER)	! Update message expiration date

	   IER = SYS$BINTIM('0 0:15',EX)	! Get time 15 minutes from now
	   IER = SYS$GETTIM(NOW)
	   IER = LIB$SUBX(NOW,EX,EX)
	   IER = SYS$ASCTIM(,INPUT,EX,)

	END IF

	IF (IMMEDIATE.NE.1.AND.BULL_DELETE.EQ.EBULL) THEN
	   CALL READDIR(0,IER)			! Get header

	   NEWEST_EXDATE = INPUT(:11)		! and store new expiration date
	   NEWEST_EXTIME = INPUT(13:23)

	   CALL WRITEDIR(0,IER)
	   IF (REMOTE_SET.EQ.4) THEN
	      CALL OPEN_BULLNEWS_SHARED
	      CALL READ_FOLDER_FILE_KEYNUM(FOLDER_NUMBER,IER)
              CALL GET_MSGKEY(NEWEST_EXBTIM,NEWS_F_EXPIRED_DATE)
	      CALL REWRITE_FOLDER_FILE(IER)
	      CALL CLOSE_BULLNEWS 
	   END IF
	ELSE IF (BULL_DELETE.EQ.EBULL) THEN
	   IF (REMOTE_SET.NE.4) CALL CLEANUP_DIRFILE(SBULL)
				! Reorder directory file

	   CALL UPDATE_ALWAYS	! Somewhat a kludgey way of updating latest
				! bulletin and expired dates.

	   IF (REMOTE_SET.NE.4.AND.SBULL.LE.BULL_POINT) THEN
	      IF (BULL_POINT.GT.EBULL) THEN
	         BULL_POINT = BULL_POINT - (EBULL - SBULL + 1)
	      ELSE
		 BULL_POINT = SBULL - 1
	      END IF
	   END IF		! Readjust where which bulletin to read next
				! if deletion causes messages to be moved.
	END IF

	RETURN
	END





	SUBROUTINE GET_2_VALS(INPUT,ILEN,SVAL,EVAL,IER)

	IMPLICIT INTEGER (A-Z)

	INCLUDE 'BULLFOLDER.INC'

	COMMON /POINT/ BULL_POINT

	CHARACTER*(*) INPUT

	DELIM = MAX(INDEX(INPUT,':'),INDEX(INPUT,'-'))

	IF (DELIM.EQ.0) THEN
	   DECODE(ILEN,'(I<ILEN>)',INPUT,IOSTAT=IER) SVAL
	   EVAL = SVAL
	ELSE
	   DECODE(DELIM-1,'(I<DELIM-1>)',INPUT,IOSTAT=IER) SVAL
	   CALL STR$UPCASE(INPUT,INPUT)
	   IF (IER.NE.0) THEN
	      IF (INDEX('CURRENT',INPUT(:DELIM-1)).EQ.1) THEN
		 SVAL = BULL_POINT
		 IER = 0
	      END IF
	   END IF
	   IF (IER.EQ.0) THEN
	      ILEN = ILEN - DELIM
	      DECODE(ILEN,'(I<ILEN>)',INPUT(DELIM+1:),IOSTAT=IER) EVAL
	      IF (IER.NE.0) THEN
	         IF (INDEX('LAST',INPUT(DELIM+1:TRIM(INPUT))).EQ.1) THEN
		    EVAL = F_NBULL
		    IER = 0
                 ELSE IF (INDEX('CURRENT',
     &                  INPUT(DELIM+1:TRIM(INPUT))).EQ.1) THEN
                    EVAL = BULL_POINT
                    IER = 0
                 END IF
 	      END IF
	   END IF
	   IF (EVAL.LT.SVAL) IER = 2
	END IF

	RETURN
	END

 

	SUBROUTINE DIRECTORY(DIR_COUNT)
C
C  SUBROUTINE DIRECTORY
C
C  FUNCTION: Display directory of messages.
C
	IMPLICIT INTEGER (A - Z)

	INCLUDE 'BULLDIR.INC'

	INCLUDE 'BULLUSER.INC'

	INCLUDE 'BULLFOLDER.INC'

	COMMON /PAGE/ PAGE_LENGTH,PAGE_WIDTH,PAGING
	LOGICAL PAGING

	DATA SCRATCH_D1/0/

	COMMON /POINT/ BULL_POINT

	COMMON /REMOTE_FOLDER/ REMOTE_SET,REMOTE_UNIT

	COMMON /TAGS/ BULL_TAG,READ_TAG,BULL_NEWS_TAG

	COMMON /COMMAND_LINE/ INCMD
	CHARACTER*256 INCMD

	COMMON /CLOSE_FILES_INFO/ CLOSED_FILES

	COMMON /BULLPAR/ BULL_PARAMETER,LEN_P
	CHARACTER*64 BULL_PARAMETER

	COMMON /POST/ POSTTIME

	COMMON /NEXT/ NEXT

	COMMON /NEW_DIR/ NEW

	COMMON /BULL_USER_CUSTOM/ BULL_USER_CUSTOM

	COMMON /NEWGROUP/ NEWGROUP

	EXTERNAL CLI$_ABSENT,CLI$_NEGATED,CLI$_PRESENT,CLOSE_FILES
	EXTERNAL BULLETIN_SUBCOMMANDS

	CHARACTER DATETIME*24,SEARCH_STRING*80,OUTLINE*80

	INTEGER TODAY(2)

	CHARACTER*12 EXPIRES,DIR_TYPE

        INTEGER TIMADR(2)                       ! Buffer containing time

	DATA WAITEFN /0/

	NEXT = .TRUE.

	IF (WAITEFN.EQ.0) CALL LIB$GET_EF(WAITEFN)
        IER=SYS$BINTIM('0 00:00:05.00',TIMADR)
   
	CALL LIB$ERASE_PAGE(1,1)		! Clear the screen

	KILL = BTEST(BULL_USER_CUSTOM,1).AND.BTEST(BULL_USER_CUSTOM,3)
	IF (KILL) IER1 = 0

	IF (INCMD(:3).EQ.'DIR') THEN
	   IF (.NOT.CLI$PRESENT('SELECT_FOLDER')) THEN
	      IF (CLI$PRESENT('MARKED')) THEN
		 READ_TAG = 1 + IBSET(0,1)
	      ELSE IF (CLI$PRESENT('SEEN')) THEN
		 READ_TAG = 1 + IBSET(0,2)
	      ELSE IF (CLI$PRESENT('UNMARKED')) THEN
		 READ_TAG = 1 + IBSET(0,1) + IBSET(0,3)
	      ELSE IF (CLI$PRESENT('UNSEEN')) THEN
		 READ_TAG = 1 + IBSET(0,2) + IBSET(0,3)
	      ELSE IF (CLI$PRESENT('ALL')) THEN
		 READ_TAG = IBSET(0,1) + IBSET(0,2)
		 IF (REMOTE_SET.GE.3) THEN
		    BULL_POINT = F_START - 1
		 ELSE
		    BULL_POINT = 0
		 END IF
 	      END IF
	      IF (READ_TAG) THEN
	         IF (.NOT.(FOLDER_NUMBER.GE.0.OR.REMOTE_SET.GE.3)) THEN
		    WRITE (6,'('' ERROR: Invalid qualifier'',
     &			       '' with remote folder.'')')
		    READ_TAG = IBSET(0,1) + IBSET(0,2)
		    GO TO 9999
		 END IF
		 CALL GET_FIRST_TAG(FOLDER_NUMBER,IER,BULL_POINT)
	      END IF
	   END IF
	   SUBJECT = CLI$PRESENT('SUBJECT').OR.CLI$PRESENT('NOREPLIES')
	   REPLY = CLI$PRESENT('REPLY')
           REPLY_FIRST = REPLY
	   SEARCH = CLI$PRESENT('SEARCH')
	   FROM_SEARCH = CLI$PRESENT('FROM')
           ANY_SEARCH = SUBJECT.OR.REPLY.OR.SEARCH.OR.FROM_SEARCH
	   EXTRACTING = CLI$PRESENT('EXTRACT')
	   PRINTING = CLI$PRESENT('PRINT')
	   POSTTIME = CLI$PRESENT('POST')
	   NEW = CLI$PRESENT('NEW')
	ELSE
	   EXTRACTING = .FALSE.
	   PRINTING = .FALSE.
	   POSTTIME = .TRUE.
	   IF (INCMD(:3).EQ.'IND') THEN
	      SUBJECT = .FALSE.
	      REPLY = .FALSE.
              REPLY_FIRST = .FALSE.
	      SEARCH = .FALSE.
	      FROM_SEARCH = .FALSE.
              ANY_SEARCH = .FALSE.
	   END IF
	END IF
	OUTPUT = EXTRACTING.OR.PRINTING

C
C  Directory listing is first buffered into temporary memory storage before
C  being outputted to the terminal.  This is to be able to quickly close the
C  directory file, and to avoid the possibility of the user holding the screen,
C  and thus causing the directory file to stay open.  The temporary memory
C  is structured as a linked-list queue, where SCRATCH_D1 points to the header
C  of the queue.  See BULLSUBS.FOR for more description of the queue.
C

	CALL INIT_QUEUE(SCRATCH_D1,BULLDIR_ENTRY)
	SCRATCH_D = SCRATCH_D1

	CALL OPEN_BULLDIR_SHARED		! Get directory file

	CALL READDIR(0,IER)			! Does directory header exist?
	START = .FALSE.
	SINCE = .FALSE.
	NEWDIR = .FALSE.
	IF (IER.EQ.1.AND.NBULL.GT.0) THEN	! And are there messages?
	   IF (DIR_COUNT.EQ.0) THEN
	      NEWDIR = .TRUE.
	      EXPIRATION = CLI$PRESENT('EXPIRATION')
	      IF (CLI$PRESENT('START')) THEN	! Start number specified?
		 START = .TRUE.
	         IER = CLI$GET_VALUE('START',BULL_PARAMETER,LEN_P)
	         DECODE(LEN_P,'(I<LEN_P>)',BULL_PARAMETER) DIR_COUNT
		 IF (DIR_COUNT.LT.1) THEN
		    WRITE (6,'('' ERROR: Invalid starting message.'')')
		    CALL CLOSE_BULLDIR
		    DIR_COUNT = 0
		    GO TO 9999
		 END IF
	      ELSE IF (CLI$PRESENT('SINCE').OR.NEW) THEN
		 IF (CLI$PRESENT('SINCE')) THEN		! Was /SINCE specified?
 	           SINCE = .TRUE.
		   IER = CLI$GET_VALUE('SINCE',DATETIME)
	   	   IF (DATETIME.EQ.'TODAY') THEN	! TODAY is the default.
	      	     IER = SYS$BINTIM('-- 00:00:00.00',TODAY)
		     CALL GET_MSGKEY(TODAY,MSG_KEY)
		   ELSE
		     CALL SYS_BINTIM(DATETIME,MSG_BTIM)
		     CALL GET_MSGKEY(MSG_BTIM,MSG_KEY)
		   END IF
		   CALL READDIR_KEYGE(IER)
		 ELSE IF (NEW) THEN	! was /NEW specified?
		   IF (REMOTE_SET.LT.3) THEN
	   	     DIFF = COMPARE_BTIM(LAST_READ_BTIM(1,FOLDER_NUMBER+1),
     &			       F_NEWEST_BTIM)
		     IF (DIFF.GE.0) THEN
		       WRITE (6,'('' No new messages are present in'',
     &			'' folder '',A,''.'')') FOLDER(:TRIM(FOLDER))
		       CALL CLOSE_BULLDIR
		       GO TO 9999
		     ELSE
		       CALL GET_MSGKEY(LAST_READ_BTIM(1,FOLDER_NUMBER+1),
     &							MSG_KEY)
		     END IF
		     CALL READDIR_KEYGE(IER)
		   ELSE
		     CALL NEWS_GET_NEWEST_MESSAGE(IER)
		     IF (IER.EQ.0) THEN
		       WRITE (6,'('' No new messages are present in'',
     &			 '' folder '',A,''.'')')
     &			 FOLDER_NAME(:TRIM(FOLDER_NAME))
		       GO TO 9999
		     END IF
		   END IF
		 END IF

		 IF (IER.EQ.0) THEN
		    WRITE (6,'('' No messages past specified date.'')')
		    CALL CLOSE_BULLDIR
		    GO TO 9999
		 ELSE
		    DIR_COUNT = IER
		 END IF
	      ELSE
	         DIR_COUNT = BULL_POINT
		 IF (DIR_COUNT.EQ.0) DIR_COUNT = 1
	      END IF

	      NEGATED = CLI$PRESENT('NEGATED')
	      IF (CLI$PRESENT('SEARCH')) THEN
		 IER1 = CLI$GET_VALUE('SEARCH',SEARCH_STRING,SLEN)
	      ELSE IF (CLI$PRESENT('SUBJECT')) THEN
		 IER1 = CLI$GET_VALUE('SUBJECT',SEARCH_STRING,SLEN)
              ELSE IF (CLI$PRESENT('FROM')) THEN
                 IER1 = CLI$GET_VALUE('FROM',SEARCH_STRING,SLEN)
	      ELSE IF (CLI$PRESENT('REPLY')) THEN
		 SEARCH_STRING = ' '
	      ELSE IF (CLI$PRESENT('NOREPLIES')) THEN
		 SEARCH_STRING = 'RE:'
		 SLEN = 3
                 NEGATED = .TRUE.
	      END IF

	      MATCH_MODE = 0
	      IF (CLI$PRESENT('MATCH')) THEN
		 CALL CLI$GET_VALUE('MATCH',BULL_PARAMETER,LEN_P)
	         IF (BULL_PARAMETER(:LEN_P).EQ.'AND') MATCH_MODE = 1
	         IF (BULL_PARAMETER(:LEN_P).EQ.'XOR') MATCH_MODE = 2
	      END IF

	      IER1 = 0

	      IF (READ_TAG) THEN
	         IF (SUBJECT.OR.REPLY.OR.SEARCH.OR.FROM_SEARCH) THEN
		    WRITE (6,'('' ERROR: Qualifier not valid when '',
     &			''displaying only tagged messages.'')')
	            SUBJECT = .FALSE.
	            REPLY = .FALSE.
	      	    SEARCH = .FALSE.
	      	    FROM_SEARCH = .FALSE.
	      	    ANY_SEARCH = .FALSE.
		    CALL CLOSE_BULLDIR
		    GO TO 9999
		 END IF
	         IF (.NOT.(SINCE.OR.NEW.OR.START)) THEN
	            DIR_COUNT = 1
		 END IF
		 CALL READDIR(DIR_COUNT,IER1)
		 IF (IER1.EQ.DIR_COUNT+1) IER1 = 0
		 IF (REMOTE_SET.GE.3.OR.BTEST(READ_TAG,3)) THEN
		    MSG_NUM = DIR_COUNT-1
		 ELSE
		    CALL DECREMENT_MSG_KEY
	         END IF
	      END IF

	      IF (START.AND.DIR_COUNT.GT.NBULL) THEN
	         IF (READ_TAG) THEN
		    SBULL = NBULL + 1
		    GO TO 100
		 ELSE
		    START = .FALSE.
		    DIR_COUNT = NBULL
		 END IF
	      END IF
	      IF (SINCE.OR.NEW.OR.START) THEN
		 SBULL = DIR_COUNT
	         EBULL = DIR_COUNT + (PAGE_LENGTH - 7) - 1
	         IF (EBULL.GE.NBULL-2) EBULL = NBULL
	      ELSE
		 DIFF = 1
	         IF (REMOTE_SET.LT.3.AND.DIR_COUNT.NE.NBULL) THEN
		    CALL READDIR(DIR_COUNT,IER)
	   	    DIFF = COMPARE_BTIM(LAST_READ_BTIM(1,
     &			       FOLDER_NUMBER+1),MSG_BTIM)
	            IF (LAST_READ_BTIM(1,FOLDER_NUMBER+1).EQ.MSG_BTIM(1)
     &		   .AND.LAST_READ_BTIM(2,FOLDER_NUMBER+1).EQ.MSG_BTIM(2))
     &			DIFF = 0
	         ELSE IF (DIR_COUNT.NE.F_NBULL) THEN
		    CALL NEWS_GET_NEWEST_MESSAGE(DIFF)
		    IF (DIFF.NE.0) THEN
		       DIFF = DIFF - DIR_COUNT - 1
	            ELSE
	               DIFF = 1
	            END IF
                 END IF
	         IF (DIFF.GT.0.AND.
     &		     NBULL-DIR_COUNT+1.LE.PAGE_LENGTH-5) THEN
	            EBULL = NBULL
	            SBULL = NBULL - (PAGE_LENGTH-5) + 1
	            IF (SBULL.LT.1) SBULL = 1
	         ELSE
	            SBULL = DIR_COUNT
	            EBULL = DIR_COUNT + (PAGE_LENGTH - 7) - 1
	            IF (EBULL.GE.NBULL-2) EBULL = NBULL
	         END IF
	      END IF

	      IER1 = 0
	      IF (REMOTE_SET.LT.3) F_START = 1
	      IF (DIR_COUNT.GT.F_START.AND.KILL.AND..NOT.(ANY_SEARCH.OR.START
     &		 .OR.SINCE.OR.NEW).AND.NEWDIR.AND..NOT.READ_TAG) THEN
       	         IF (REMOTE_SET.EQ.3) NEWGROUP = .TRUE.
	         I = DIR_COUNT
		 NUM = 0
	         SBULL = DIR_COUNT
	 	 DO WHILE (NUM.LT.PAGE_LENGTH-5.AND.I.LE.NBULL)
		    CALL READDIR(I,IER)
		    IF (I.EQ.NBULL) IER1 = 1
		    IF (I.EQ.DIR_COUNT.AND.I+1.NE.IER) DIR_COUNT = I + 1
		    IF (I+1.EQ.IER) NUM = NUM + 1
		    I = I + 1
		 END DO
		 IF (IER1.EQ.0.AND.NUM.GT.PAGE_LENGTH-7) NUM = PAGE_LENGTH - 7
	         IF (IER1.NE.0.AND.NUM.LT.PAGE_LENGTH-5.AND.
     &				SBULL.GT.F_START) THEN
	            I = SBULL - 1
		    NEXT = .FALSE.
	 	    DO WHILE (NUM.LT.PAGE_LENGTH-5.AND.I.GE.F_START)
		       CALL READDIR(I,IER)
		       IF (I.EQ.NBULL) IER1 = 1
		       IF (I+1.EQ.IER) THEN
			  NUM = NUM + 1
		          DIR_COUNT = I
		       END IF
		       I = I - 1
		    END DO
		    NEXT = .TRUE.
	         END IF
	         SBULL = DIR_COUNT
	         EBULL = SBULL + NUM - 1
	      END IF
	   ELSE IF (DIR_COUNT.EQ.-1.AND..NOT.READ_TAG) THEN
	      SUBJECT = .FALSE.
	      REPLY = .FALSE.
              SEARCH = .FALSE.
              FROM_SEARCH = .FALSE.
	      SBULL = (SBULL - 1) - ((PAGE_LENGTH - 7) - 1)
	      IF (SBULL.LT.1) SBULL = 1
	      EBULL = SBULL + (PAGE_LENGTH - 7) - 1
	      IF (NBULL-SBULL+1.LE.PAGE_LENGTH-5) THEN
	         SBULL = NBULL - (PAGE_LENGTH-5) + 1
	         EBULL = NBULL
	         IF (SBULL.LT.1) SBULL = 1
	      END IF
	      IF ((REMOTE_SET.EQ.4.OR.KILL).AND.SBULL.GT.F_START) THEN
	         NUM = EBULL - SBULL + 1
	         I = EBULL
	         NEXT = .FALSE.
		 NUM1 = 0
		 EBULL = 0
	 	 DO WHILE (NUM.GT.0.AND.I.GE.F_START)
		    CALL READDIR(I,IER)
		    IF (I.EQ.NBULL) IER1 = 1
		    IF (I+1.EQ.IER) THEN
		       IF (EBULL.EQ.0) EBULL = I
		       NUM = NUM - 1
		       NUM1 = NUM1 + 1
		       SBULL = I
	            END IF
		    IF (NUM.GT.0) I = I - 1
		 END DO
       	         IF (REMOTE_SET.EQ.3) NEWGROUP = .TRUE.
	         NEXT = .TRUE.
		 NUM = NUM1
	         IF (NUM.LE.PAGE_LENGTH-7) THEN
	            IF (IER1.EQ.0.AND.I.LE.F_START) THEN
		       I = EBULL
	               DO WHILE (I.LT.NBULL.AND.NUM.LE.PAGE_LENGTH-5)
	                  I = I + 1
	                  CALL READDIR(I,IER)
			  IF (I+1.EQ.IER) NUM = NUM + 1 
	               END DO
	               IF (NUM.GT.PAGE_LENGTH-5) NUM = PAGE_LENGTH-7
                    ELSE IF (IER1.NE.0.AND.I.GT.F_START) THEN
		       I = F_START - 1
		       J = 0
	               DO WHILE (J.LT.3.AND.I.LT.NBULL)
	                  I = I + 1
	                  CALL READDIR(I,IER)
			  IF (I+1.EQ.IER) J = J + 1
	               END DO
	               IF (MSG_NUM.GE.SBULL) THEN 
			  NUM = NUM + 2 
	                  SBULL = F_START
	               END IF
	            END IF
	         END IF
	         EBULL = SBULL + NUM - 1
	      END IF
	   ELSE IF (DIR_COUNT.EQ.-1.AND.READ_TAG) THEN
100	      CALL GET_FIRST_TAG(FOLDER_NUMBER,IER,FIRST_BULL)
	      FIRST_BULL = FIRST_BULL + 1
	      DIR_COUNT = FIRST_BULL
	      IER1 = IER
	      IER = 0
	      FBULL = 0
	      EBULL = 0
	      LBULL = SBULL.GT.NBULL
	      DO WHILE (SBULL.GT.FIRST_BULL.AND.IER.EQ.0)
		 SBULL = SBULL - 1
	         CALL READDIR(SBULL,IER)
		 IF (IER.EQ.SBULL+1) THEN
	            CALL GET_THIS_TAG(FOLDER_NUMBER,IER,DIR_COUNT,DUMMY)
		    IF (IER.EQ.0) THEN
		       IF (FBULL.EQ.0) EBULL = DIR_COUNT
		       FBULL = FBULL +1
		       IF ((.NOT.LBULL.AND.FBULL.EQ.PAGE_LENGTH-7).OR.
     &			   (LBULL.AND.FBULL.EQ.PAGE_LENGTH-5)) THEN
		          IER = 1
		       END IF
		    ELSE
		       IER = 0
		    END IF
		 ELSE
		    IER = 1
		 END IF
	      END DO
	      IF (DIR_COUNT.EQ.FIRST_BULL.AND..NOT.LBULL) THEN
		 CALL READDIR(EBULL,IER)
		 IER = 0
		 DO WHILE (IER.EQ.0.AND.FBULL.LT.PAGE_LENGTH-7)
		    CALL GET_NEXT_TAG(FOLDER_NUMBER,IER,DIR_COUNT,DUMMY)
		    IF (IER.EQ.0) THEN
		       FBULL = FBULL + 1
		       EBULL = DIR_COUNT
		    END IF
		 END DO
		 DO I=1,3
		    CALL GET_NEXT_TAG(FOLDER_NUMBER,IER,DIR_COUNT,DUMMY)
		 END DO
		 IF (IER.NE.0) THEN
		    EBULL = DIR_COUNT
		    FBULL = FBULL + 2
	         END IF
	      END IF
	      CALL READDIR(EBULL,IER)
	      IF (EBULL+1.NE.IER) THEN
		 EBULL = EBULL + 1
	      ELSE
	         CALL GET_NEXT_TAG(FOLDER_NUMBER,IER,DUMMY,DUMMY1)
		 IF (IER.NE.0) EBULL = EBULL + 1
	      END IF
	      CALL READDIR(SBULL,IER)
	      IF (REMOTE_SET.GE.3.OR.BTEST(READ_TAG,3)) THEN
		 MSG_NUM = MSG_NUM-1
	      ELSE
		 CALL DECREMENT_MSG_KEY
	      END IF
	      EBULL = SBULL + FBULL - 1
	   ELSE
	      SBULL = DIR_COUNT
	      EBULL = DIR_COUNT + (PAGE_LENGTH - 7) - 1
	      IF (EBULL.GE.NBULL-2) EBULL = NBULL
	   END IF
	   IF (.NOT.PAGING.OR.OUTPUT) EBULL = NBULL
	   IF (INCMD(:3).EQ.'DIR') THEN
	      IF (CLI$GET_VALUE('END',BULL_PARAMETER,LEN_P)) THEN
	         DECODE(LEN_P,'(I<LEN_P>)',BULL_PARAMETER) EBULLe
	         EBULL = MIN(EBULL,NBULL)
	      END IF 
	   END IF
	   IF (ANY_SEARCH) THEN
       	      IF (REMOTE_SET.EQ.3) NEWGROUP = .TRUE.
	   ELSE IF ((.NOT.REMOTE_SET.OR.KILL).AND..NOT.READ_TAG) THEN
       	      IF (REMOTE_SET.EQ.3) NEWGROUP = .TRUE.
	      I1 = SBULLn
	      I = SBULL
	      DO WHILE (I.LE.EBULL)
	         CALL READDIR(I1,IER)		! Into the queue
		 IF (KILL.AND.I1.EQ.NBULL) IER1 = 1 
		 IF (IER.EQ.I1+1) THEN
	            CALL WRITE_QUEUE(%VAL(SCRATCH_D),SCRATCH_D,
     &					BULLDIR_ENTRY)D
		    I = I + 1L
	         ELSE IF (I1.GE.NBULL) THEN
		    EBULL = I - 1 
	         END IF
	         I1 = I1 + 1 
	      END DOE
	   ELSE IF (READ_TAG) THENI
	      I = 0
	      DO WHILE (I.LE.EBULL.AND.IER1.EQ.0)
		 CALL GET_NEXT_TAG(FOLDER_NUMBER,IER1,DIR_COUNT,TAG_TYPE) 
		 IF (I.EQ.0.AND.IER1.EQ.0) THEN 
		    EBULL = EBULL - SBULL + DIR_COUNTN
		    SBULL = DIR_COUNT_
		    I = SBULLH
		 END IF 
	 	 SYSTEM = SYSTEM.OR.ISHFT(TAG_TYPE,28)+
	         CALL WRITE_QUEUE(%VAL(SCRATCH_D),SCRATCH_D,BULLDIR_ENTRY)N
		 I = I + 1
	      END DOb
	      EBULL = I - 1
	      IF (IER1.NE.0) THEN
	         EBULL = EBULL - 1
	      ELSEE
	         CALL GET_NEXT_TAG(FOLDER_NUMBER,IER1,DUMMY,TAG_TYPE)
		 IF (IER1.EQ.0) THEN
		    IER = 0'
		    EBULL_SAVE = EBULL
		    DO I=1,2
		       IF (IER.EQ.0) THEN
			  SYSTEM = SYSTEM.OR.ISHFT(TAG_TYPE,28)
	                  CALL WRITE_QUEUE(%VAL(SCRATCH_D),SCRATCH_D,
     &							BULLDIR_ENTRY)L
			  EBULL = EBULL + 1
	                  CALL GET_NEXT_TAG(FOLDER_NUMBER,IER,DUMMY,R
     &				TAG_TYPE)
		       END IF 
		    END DO
		    IF (IER.NE.0) THEN
	               CALL GET_FIRST_TAG(FOLDER_NUMBER,IER,FIRST_BULL)
	               IF (SBULL.NE.FIRST_BULL+1) EBULL = EBULL_SAVED
		       IER1 = 1(
		    ELSE
		       EBULL = EBULL_SAVEE
		    END IF
		 END IFN
	      END IF 
	   ELSE
	      CALL REMOTE_DIRECTORY_COMMAND
     &				     (SBULL,EBULL,.FALSE.,SCRATCH_D,IER)
	      IF (IER.NE.0) THEN(
	         CALL CLOSE_BULLDIR
		 CALL DISCONNECT_REMOTER
		 GO TO 99991
	      END IF 
	   END IF
	ELSEB
	   NBULL = 0 
	END IF 

	IF (NBULL.EQ.0.OR.EBULL.LT.SBULL) THEN	
	   CALL CLOSE_BULLDIR			! We don't need file anymore
	   IF (READ_TAG) THEN
	      IF (BTEST(READ_TAG,1).AND.BTEST(READ_TAG,3)) THEN
		 DIR_TYPE = 'unmarked'
	      ELSE IF (BTEST(READ_TAG,2).AND.BTEST(READ_TAG,3)) THENE
		 DIR_TYPE = 'unseen'
	      ELSE IF (BTEST(READ_TAG,1)) THEN
		 DIR_TYPE = 'marked'
	      ELSE IF (BTEST(READ_TAG,2)) THENI
		 DIR_TYPE = 'seen'
	      END IF 
	      WRITE (6,'('' No '',A,'' messages are present in'',
     &		     '' folder '',A,''.'')')
     &		DIR_TYPE(:TRIM(DIR_TYPE)),FOLDER_NAME(:TRIM(FOLDER_NAME))L
	   ELSE
	      WRITE (6,'('' There are no messages present.'')')
	   END IF
	   DIR_COUNT = -1
	   GO TO 9999
	END IFA

C6
C  Directory entries are now in queue.  Output queue entries to screen.C
CO

	IF (REMOTE_SET.GE.3) THEN
	   WRITE (OUTLINE,'('' ['',I,''-'',I,'']'')')
     &						F_START,F_NBULLA
	ELSEA
	   WRITE (OUTLINE,'('' [1-'',I,'']'')') NBULL
	END IFU
	DO WHILE (INDEX(OUTLINE,'- ').GT.0)
	   I = INDEX(OUTLINE,'- ')N
	   OUTLINE(I+1:) = OUTLINE(I+2:)A
	END DOX
	DO WHILE (INDEX(OUTLINE,'[ ').GT.0)
	   I = INDEX(OUTLINE,'[ ') 
	   OUTLINE(I+1:) = OUTLINE(I+2:)T
	END DO
	DO WHILE (INDEX(OUTLINE,'  ').LT.TRIM(OUTLINE))
	   I = INDEX(OUTLINE,'  ')Y
	   OUTLINE(I:) = OUTLINE(I+1:)
	END DOL
	OUTLINE = FOLDER_NAME(:TRIM(FOLDER_NAME))//OUTLINE 
	BULL_PARAMETER = ' '1
	IF (READ_TAG) THEN_
	   IF (BTEST(READ_TAG,1)) THEN
	      BULL_PARAMETER = 'MARKED'
	   ELSE
	      BULL_PARAMETER = 'SEEN'
	   END IF
	   IF (BTEST(READ_TAG,3)) THENN
	      BULL_PARAMETER = 'UN'//BULL_PARAMETER
	   END IF
	END IF)
	IF (PRINTING) THEN 
	   BULL_PARAMETER = 'PRINTING '//BULL_PARAMETER
	ELSE IF (EXTRACTING) THEN
	   BULL_PARAMETER = 'EXTRACTING '//BULL_PARAMETER
	END IFS
	WRITE (6,'(1X,A,<PAGE_WIDTH-TRIM(BULL_PARAMETER)-
     &		TRIM(OUTLINE)>X,A)')
     &		BULL_PARAMETER(:TRIM(BULL_PARAMETER)),
     &		OUTLINE(:TRIM(OUTLINE)) 
	IF (EXPIRATION) THEN=
	   WRITE(6,1005)S
	ELSE 
	   WRITE(6,1000)	
	END IF

	TAG = (BULL_TAG.AND.REMOTE_SET.EQ.0).OR. 
     &			(BULL_NEWS_TAG.AND.REMOTE_SET.GE.3)

	IF (.NOT.ANY_SEARCH.AND.TAG.AND..NOT.READ_TAG) THEN
	   IF (INCMD(:3).NE.'   ') THEN
	      SCRATCH_D = SCRATCH_D1		! Init queue pointer to header)
	      CALL READ_QUEUE(%VAL(SCRATCH_D),SCRATCH_D,BULLDIR_ENTRY)E
	      CALL GET_THIS_OR_NEXT_TAG(FOLDER_NUMBER,IER,NEXT_TAG,TAG_TYPE)N
	      IF (IER.NE.0) NEXT_TAG = NBULL + 1 
	   END IF
	   SCRATCH_D = SCRATCH_D1		! Init queue pointer to header
	   DO I=SBULL,EBULL
	      SAVE_SCRATCH_D = SCRATCH_D(
	      CALL READ_QUEUE(%VAL(SCRATCH_D),SCRATCH_D,BULLDIR_ENTRY)R
	      IF (TAG.AND.MSG_NUM.EQ.NEXT_TAG) THEN
		 SYSTEM = SYSTEM.OR.ISHFT(TAG_TYPE,28)
	         CALL WRITE_QUEUE(%VAL(SAVE_SCRATCH_D),DUMMY,BULLDIR_ENTRY)
	         CALL GET_NEXT_TAG(FOLDER_NUMBER,IER,NEXT_TAG,TAG_TYPE)
	         IF (IER.NE.0) NEXT_TAG = NBULL + 1
	      END IFE
	   END DO
	END IFR

	CALL CLOSE_BULLDIR			! We don't need file anymore

	SCRATCH_D = SCRATCH_D1			! Init queue pointer to header

	I = SBULL
	START_SEARCH = I 
	IF (.NOT.REPLY_FIRST) THEN 
	   START_SEARCH = I - 1
	ELSE IF (.NOT.CLI$PRESENT('START')) THENi
	   START_SEARCH = BULL_POINTf
	END IFb
	IF (ANY_SEARCH.OR.OUTPUT) THENT
	   CALL OPEN_BULLDIR_SHARED
	   IF (SEARCH.OR.OUTPUT) CALL OPEN_BULLFIL_SHARED
	   CLOSED_FILES = .FALSE.
	END IF
	DO WHILE (I.LE.EBULL)
	   IF (.NOT.ANY_SEARCH) THEN 
	      CALL READ_QUEUE(%VAL(SCRATCH_D),SCRATCH_D,BULLDIR_ENTRY)e
	   ELSE
	      IF (CLOSED_FILES) THEN 
		 CLOSED_FILES = .FALSE.R
		 CALL OPEN_BULLDIR_SHAREDq
		 IF (SEARCH.OR.OUTPUT) CALL OPEN_BULLFIL_SHAREDN
	      END IFD
 	      CALL GET_SEARCH(FOUND,SEARCH_STRING,1,SLEN,0,c
     &		    START_SEARCH,.FALSE.,SUBJECT,REPLY_FIRST,.FALSE.,i
     &		    .TRUE.,FROM_SEARCH,NEGATED,MATCH_MODE)
	      IF (INCMD(:3).NE.'   '.AND.TAG.AND.FOUND.GT.0) THEN
	         CALL GET_THIS_OR_NEXT_TAG(FOLDER_NUMBER,IER,NEXT_TAG,
     &		    TAG_TYPE)$
	         IF (IER.NE.0) NEXT_TAG = NBULL + 1
		 NEXT = .FALSE.t
		 CALL READDIR(FOUND,IER)
		 NEXT = .TRUE.
	      END IFL
	      REPLY_FIRST = .FALSE.
	      IF (FOUND.GT.0) THENE
		 SEARCH_STRING = ' '
		 START_SEARCH = FOUND	
	         IF (TAG.AND.MSG_NUM.EQ.NEXT_TAG) THENR
	            CALL GET_NEXT_TAG(FOLDER_NUMBER,IER,NEXT_TAG,DUMMY)
	            IF (IER.NE.0) NEXT_TAG = NBULL + 1
		    NEXT = .FALSE.
		    CALL READDIR(FOUND,IER)	
		    NEXT = .TRUE.C
		    SYSTEM = SYSTEM.OR.ISHFT(TAG_TYPE,28) 
		    TAG_TYPE = DUMMY
	         END IF
	      ELSET
		 I = EBULL + 1
	      END IFT
	      IER = SYS$SETIMR(%VAL(WAITEFN),TIMADR,CLOSE_FILES,)
	   END IF
	   IF (I.LE.EBULL) THEN
	      CALL CONVERT_ENTRY_FROMBIN_FOLDER
	      IF (BTEST(SYSTEM,30)) THENM
		 OUTLINE  = '>' 
	      ELSEK
		 OUTLINE  = ' ')
	      END IF
	      IF (BTEST(SYSTEM,29)) THENS
		 OUTLINE(2:)  = '*'N
	      ELSE
		 OUTLINE(2:)  = ' 'T
	      END IF 
	      N = MAX(INT(LOG10(REAL(MSG_NUM)))+1,3)_
	      IF ((EXDATE(8:9).EQ.'18'.OR.INDEX(EXDATE,'1900').GT.0)0
     &		  .AND.REMOTE_SET.NE.3) THEN
	         WRITE(OUTLINE(3:),2010) MSG_NUM,DESCRIP(:54-N),FROM,
     &							'(DELETED)'
	      ELSE IF (EXPIRATION) THEN
	         IF (BTEST(SYSTEM,2)) THEN		! Shutdown bulletin?D
		    EXPIRES = 'Shutdown'
	         ELSE IF (BTEST(SYSTEM,1)) THEN		! Permanent bulletin?(
		    EXPIRES = 'Permanent' 
	         ELSE IF (EXDATE(8:9).EQ.'18'.AND.REMOTE_SET.EQ.3) THEN
		    EXPIRES = 'Unknown'g
	         ELSE
		    EXPIRES = EXDATE(:7)//EXDATE(10:11) 
	         END IF
	         WRITE(OUTLINE(3:),2010) MSG_NUM,DESCRIP(:54-N),FROM,
     &						EXPIRES(:9)2
	      ELSEo
	         WRITE(OUTLINE(3:),2010) MSG_NUM,DESCRIP(:54-N),FROM,
     &						DATE(:7)//DATE(10:11):
	      END IF
	      WRITE(6,'(1X,A)') OUTLINE
	      IF (OUTPUT) THENI
		 FOUND_MSG = .TRUE. 
		 CALL SYS$SETAST(%VAL(0)) 
		 NEXT = .FALSE.o
	         IF (PRINTING) THEN
	            CALL PRINT(MSG_NUM,CLOSED_FILES) 
	         ELSE
		    CALL FILE(MSG_NUM,CLOSED_FILES)
		    IF (MSG_NUM.GT.0) THEN
		       I = EBULL
	               FOUND = 0e
                    ELSE
		       MSG_NUM = -MSG_NUMI
	            END IF
	         END IF
		 NEXT = .TRUE.
		 CALL SYS$SETAST(%VAL(1))L
	      END IFR
	   END IF
	   I = I + 1O
	   IF (ANY_SEARCH) IER = SYS$CANTIM(,)_
	END DO 

	DIR_COUNT = MSG_NUM + 1			! Update directory counterL

	IF (ANY_SEARCH.OR.OUTPUT) THEN
	   IF (SEARCH.OR.OUTPUT) CALL CLOSE_BULLFIL
	   CALL CLOSE_BULLDIR
	   IF (ANY_SEARCH) THEN
	      IF (FOUND.GT.0) THENC
	         DIR_COUNT = FOUND + 1 
	      ELSER
	         DIR_COUNT = NBULL + 1R
	      END IF
	   END IF
	END IF*

	IF (DIR_COUNT.GT.NBULL.OR.((READ_TAG.OR.KILL).AND.IER1.NE.0)) THENI
						! Outputted all entries?
	   DIR_COUNT = -1			! Yes. Set counter to -1.
	   IF (PRINTING) THEN '
	      IF (CLI$PRESENT('NOW').AND.FOUND_MSG) THEN
	         INCMD = 'PRINT/NOW'I
	         IER = CLI$DCL_PARSE(INCMD,BULLETIN_SUBCOMMANDS)
	         CALL PRINT(MSG_NUM,CLOSED_FILES)
	      END IFT
	   ELSE IF (EXTRACTING.AND.FOUND_MSG) THEN 
	      CALL FILE(0,CLOSED_FILES)
	   END IF
	ELSEL
	   WRITE(6,1010)			! Else say there are moreL
	END IF 

9999	POSTTIME = .FALSE.E
	NEXT = .FALSE.E
	RETURN 

1000	FORMAT('    #',1X,'Description',43X,'From',9X,'Date',/)
1005	FORMAT('    #',1X,'Description',43X,'From',8X,'Expires',/)P
1010	FORMAT(1X,/,' Press RETURN for more...',/)N

2010	FORMAT(I<N>,1X,A<54-N>,1X,A12,1X,A9)U

	END


	SUBROUTINE CLOSE_FILESA

	IMPLICIT INTEGER (A-Z)L

	COMMON /CLOSE_FILES_INFO/ CLOSED_FILESU

	INQUIRE(UNIT=1,OPENED=IER)E
	IF (IER) CALL CLOSE_BULLFIL

	INQUIRE(UNIT=2,OPENED=IER) 
	IF (IER) CALL CLOSE_BULLDIR

	CLOSED_FILES = .TRUE.

	RETURNo
	END



	SUBROUTINE GET_MSGKEY(BTIM,MSG_KEY)

	IMPLICIT INTEGER (A-Z)

	INTEGER BTIM(2)

	CHARACTER*8 MSG_KEY,INPUT

	CALL LIB$MOVC3(8,BTIM(1),%REF(INPUT))

	DO I=1,8E
	   MSG_KEY(I:I) = INPUT(9-I:9-I)E
	END DO	

	RETURNt
	END



	SUBROUTINE FILE(FILE_NUM,OPEN_IT)
CL
C  SUBROUTINE FILE
C
C  FUNCTION:  Copies a bulletin to a file.
C 
	IMPLICIT INTEGER (A - Z) 

	COMMON /POINT/ BULL_POINT

	COMMON /BULLPAR/ BULL_PARAMETER,LEN_P
	CHARACTER*64 BULL_PARAMETER

	COMMON /REMOTE_FOLDER/ REMOTE_SET,REMOTE_UNIT

	COMMON /READ_DISPLAY/ LINE_OFFSET

	COMMON /FILE_DIRECTORY/ FILE_DIRECTORYE
	CHARACTER*64 FILE_DIRECTORY

	INCLUDE 'BULLDIR.INC'

	INCLUDE 'BULLFOLDER.INC'F

	EXTERNAL CLI$_ABSENTO

	CHARACTER*128 FILENAME	

	DATA OPENED /.FALSE./

	IF (CAPTIVE(1)) THENs
	   WRITE (6,'('' ERROR: Command invalid from CAPTIVE account.'')')n
	   RETURN
	END IF 

10	IF (FILE_NUM.EQ.0) THEN
	IF (.NOT.OPEN_IT) THEN
	   OPENED = .FALSE.
	   CLOSE (UNIT=3)
	   RETURN
	END IFT
	IER = CLI$GET_VALUE('BULLETIN_NUMBER',BULL_PARAMETER,LEN_P)
	IF (IER.NE.%LOC(CLI$_ABSENT)) THEN	! Was bulletin specified?
	   CALL GET_2_VALS(BULL_PARAMETER,LEN_P,SBULL,EBULL,IER)f
	   IF (EBULL.GT.F_NBULL) EBULL = F_NBULLu
	ELSE IF (OPENED) THEN
	   CALL CLOSE_BULLFIL
	   CALL CLOSE_BULLDIR
	   CLOSE (UNIT=3)			! Bulletin copy completed
	   OPENED = .FALSE.
	   RETURN
	ELSE IF (CLI$PRESENT('ALL')) THEN
	   SBULL = 1I
	   EBULL = F_NBULLL
	   IER = 0
	ELSE IF (BULL_POINT.EQ.0) THEN	! No.  Have we just read a bulletin?
	   WRITE(6,1010)		! No, then error.
	   RETURN
	ELSES
	   SBULL = BULL_POINT
	   EBULL = SBULL1
	   IER = 0 
	END IFT

	IF (SBULL.LE.0.OR.IER.NE.0.OR.EBULL.LT.SBULL) THENA
	   WRITE (6,1015)
	   IF (OPENED) THEN
	      CALL CLOSE_BULLFILI
	      CALL CLOSE_BULLDIRE
	      CLOSE (UNIT=3)			! Bulletin copy completed
	      OPENED = .FALSE.
	   END IF
	   WRITE (6,'(1X,A)') BULL_PARAMETER(:LEN_P)B
	   RETURN
	END IF1
	ELSEH
	   SBULL = FILE_NUM
	   EBULL = SBULLT
	END IFD

	IF (.NOT.OPENED) THEN
	   IER = CLI$GET_VALUE('EXTRACT',FILENAME,LEN_F)L

	   IF (.NOT.IER) THEN
	      FILENAME = FOLDER
	      DO I=1,LEN(FILENAME)l
	         IF (FILENAME(I:I).EQ.'.') FILENAME(I:I) = '_' 
	      END DOE
	      FILENAME = FILENAME(:TRIM(FILENAME))//'.TXT'.
	      LEN_F = TRIM(FILENAME)S
	   END IF

	   IF (TRIM(FILE_DIRECTORY).GT.0.AND.INDEX(FILENAME,':').EQ.0
     &	       .AND.INDEX(FILENAME,'[').EQ.0) THEN 
	      FILENAME = FILE_DIRECTORY(:TRIM(FILE_DIRECTORY))//FILENAME
	      LEN_F = TRIM(FILENAME)I
	   END IF

	   CALL STR$UPCASE(FILENAME,FILENAME)

	   CALL DISABLE_PRIVS

	   IF (CLI$PRESENT('NEW')) THEN
	      OPEN(UNIT=3,FILE=FILENAME(:LEN_F),ERR=900,S
     &	        RECL=LINE_LENGTH,STATUS='NEW',CARRIAGECONTROL='LIST')
	   ELSE
	      OPEN(UNIT=3,FILE=FILENAME(:LEN_F),IOSTAT=IER,
     &		RECL=LINE_LENGTH,	
     &		STATUS='OLD',CARRIAGECONTROL='LIST',ACCESS='APPEND')
	      IF (IER.NE.0) THENO
	         OPEN(UNIT=3,FILE=FILENAME(:LEN_F),ERR=900,
     &	           RECL=LINE_LENGTH,STATUS='NEW',CARRIAGECONTROL='LIST') 
	      ELSE IF (CLI$PRESENT('FF')) THENC
	         WRITE (3,'(A)') CHAR(12)
	      END IF 
	   END IF

	   CALL ENABLE_PRIVS			! Reset SYSPRV privileges6

	   HEAD = CLI$PRESENT('HEADER')

	   IF (OPEN_IT) THEN	
	      CALL OPEN_BULLDIR_SHARED 
	      CALL OPEN_BULLFIL_SHARED	! Open BULLETIN file
	   END IF
	   OPENED = .TRUE.e
	   FIRST = .TRUE.
	END IFA

	DO FBULL = SBULL,EBULL 
	   FBULL1 = FBULL
	   CALL READDIR(FBULL,IER)	! Get info for specified bulletinN

	   IF (IER.NE.FBULL+1.OR.FBULL.GT.EBULL.OR.(.NOT.CLI$PRESENTD
     &		('ALL').AND.FBULL1.EQ.SBULL.AND.FBULL.NE.SBULL)) THENu
	      IF (REMOTE_SET.LT.3.OR.FBULL1.EQ.SBULL) WRITE(6,1030) FBULL1t
	      IF (FBULL1.GT.SBULL) GO TO 100	
	      CLOSE (UNIT=3,STATUS='DELETE') 
	      OPENED = .FALSE.D
	      IF (OPEN_IT) THEN
	         CALL CLOSE_BULLFIL
	         CALL CLOSE_BULLDIR
              END IF
	      RETURNT
	   ELSE IF (REMOTE_SET) THENE
	      CALL REMOTE_READ_MESSAGE(FBULL,IER1)T
	      IF (IER1.GT.0) THEN
	         CALL DISCONNECT_REMOTE
	      ELSEE
	         CALL GET_REMOTE_MESSAGE(IER1)
	      END IF
	      IF (IER1.NE.0) GO TO 100S
	   END IF

	   IF (.NOT.FIRST.AND.CLI$PRESENT('FF')) THEN
	      WRITE (3,'(A)') CHAR(12) 
	   ELSE IF (FIRST) THEN
	      FIRST = .FALSE.
	   END IF

	   ILEN = LINE_LENGTH + 1

	   CALL GET_BULL_LINE(BLOCK,LENGTH,INPUT,ILEN)(
	   IF (ILEN.GT.0.AND.INPUT(:6).EQ.'From: ') THENF
	      IF (HEAD) WRITE(3,1060) INPUT(7:ILEN),DATE//' '//TIME(:8)
	      CALL GET_BULL_LINE(BLOCK,LENGTH,INPUT,ILEN)
	   ELSE IF (HEAD) THENn
	      WRITE(3,1060) FROM,DATE//' '//TIME(:8) 
	   END IF
	   IF (ILEN.GT.0.AND.INPUT(:6).EQ.'Subj: ') THENN
	      IF (HEAD) WRITE(3,1050) INPUT(7:ILEN)
	   ELSE
	      IF (HEAD) WRITE(3,1050) DESCRIP
	      IF (ILEN.GT.0) WRITE (3,'(A)') INPUT(:ILEN)
	   END IF

	   DO WHILE (ILEN.GT.0)		! Copy bulletin into filel
	      CALL GET_BULL_LINE(BLOCK,LENGTH,INPUT,ILEN)
	      IF (ILEN.GT.0) WRITE (3,'(A)') INPUT(:ILEN)
	   END DO
	END DO 

100     IF (FILE_NUM.GT.0) THEN 
	   FILE_NUM = -FILE_NUM
	   RETURN
	END IFc

	IER = OTS$CVT_L_TI(SBULL,BULL_PARAMETER,,,)
 	IF (SBULL.EQ.EBULL) THEN	! Show name of file created.0
	   WRITE(6,1040)t
     &	      BULL_PARAMETER(FIRST_ALPHA(BULL_PARAMETER):),
     &	      FILENAME(:LEN_F)r
	ELSEm
	   WRITE(6,1045)h
     &	      BULL_PARAMETER(FIRST_ALPHA(BULL_PARAMETER):) 
	   IER = OTS$CVT_L_TI(EBULL,BULL_PARAMETER,,,)I
           WRITE(6,1046)
     &	      BULL_PARAMETER(FIRST_ALPHA(BULL_PARAMETER):),
     &	      FILENAME(:LEN_F)a
	END IF

	GO TO 10F

900	WRITE(6,1000) FILENAME(:LEN_F)
	CALL ENABLE_PRIVS		! Reset BYPASS privileges/
	RETURNA

1000	FORMAT(' ERROR: Error in opening file ',A,'.'))
1010	FORMAT(' ERROR: You have not read any bulletin.')
1015	FORMAT(' ERROR: Specified message number has incorrect format:') 
1030	FORMAT(' ERROR: Following bulletin was not found: ',I)D
1040	FORMAT(' Message ',A,' written to ',A)N
1045	FORMAT(' Messages ',A,'-',$)
1046	FORMAT('+',A,' written to ',A)I
1050	FORMAT('Description: ',A,/)
1060	FORMAT(/,'From: ',A,/,'Date: ',A)

	END



	SUBROUTINE COPY2(OUT,IN)T

	CALL LIB$MOVC3(8,IN,OUT)L

	RETURN(
	END



	SUBROUTINE LOGIN 
C 
C  SUBROUTINE LOGIN(
CB
C  FUNCTION: Alerts user of new messages upon logging in.N
CL
	IMPLICIT INTEGER (A - Z) 

        INCLUDE 'BULLFILES.INC' 
  
	INCLUDE 'BULLDIR.INC'

	INCLUDE 'BULLUSER.INC')

	INCLUDE 'BULLFOLDER.INC'(

	COMMON /READIT/ READITT

	COMMON /BULLPAR/ BULL_PARAMETER,LEN_P
	CHARACTER*64 BULL_PARAMETER

	COMMON /PAGE/ PAGE_LENGTH,PAGE_WIDTH,PAGING
	LOGICAL PAGING 

	COMMON /POINT/ BULL_POINT

	COMMON /PROMPT/ COMMAND_PROMPTA
	CHARACTER*40 COMMAND_PROMPT

	COMMON /SYSTEM_FOLDERS/ SYSTEM_FLAG(FLONG),DUMMY(2)

	COMMON /COMMAND_SWITCHES/ LOGIN_SWITCH,SYSTEM_SWITCHF
	COMMON /COMMAND_SWITCHES/ SYSTEM_LOGIN_BTIM(2)
	COMMON /COMMAND_SWITCHES/ REVERSE_SWITCH,SEPARATE
	CHARACTER*4 SEPARATE 

	COMMON /REMOTE_FOLDER/ REMOTE_SET,REMOTE_UNIT

	CHARACTER TODAY*24,INREAD*4

	DATA CTRL_G/7/d

	DATA GEN_DIR1/0/	! General directory link list header
	DATA SYS_DIR1/0/	! System directory link list header=
	DATA SYS_NUM1/0/	! System message number link list header
	DATA SYS_BUL1/0/	! System bulletin link list header
	DATA ALL_DIR1/0/	! Full directory link list header (for remote)

	DATA PAGE/0/W

	DATA FIRST_WRITE/.TRUE./ 
	LOGICAL FIRST_WRITE

	COMMON /LOGIN_BTIM/ LOGIN_BTIM_SAVE(2)I

	DIMENSION NOLOGIN_BTIM(2),TODAY_BTIM(2)
	DIMENSION NEW_BTIM(2),PASSCHANGE(2),BULLCP_BTIM(2)U
	DIMENSION LOGIN_BTIM_OLD(2),LOGIN_BTIM_NEW(2)

	FOLDER_NAME = FOLDERF

	CALL SYS$ASCTIM(,TODAY,,)		! Get the present time
	CALL SYS_BINTIM(TODAY,TODAY_BTIM)

	CALL SYS_BINTIM('5-NOV-2956 00:00:00.00',NOLOGIN_BTIM)
	CALL SYS_BINTIM('5-NOV-1956 11:05:56',NEW_BTIM)

CE
C  Find user entry in BULLUSER.DAT to update information and
C  to get the last date that messages were read.
C 

	CALL OPEN_BULLUSER_SHARED

	CALL READ_USER_FILE_HEADER(IER)		! Get the header

	IF (IER.EQ.0) THEN			! Header is present.
	   UNLOCK 4
	   CALL READ_USER_FILE_KEYNAME(USERNAME,IER1)
						! Find if there is an entryS
	   IF (NEW_FLAG(1).LT.143.OR.NEW_FLAG(1).GT.143) THEN
	      NEW_FLAG(2)=0		! If old version clear GENERIC value
	      NEW_FLAG(1)=143		! Set new version number
	   END IF
	   IF (IER1.EQ.0) THEN			! There is a user entryE
	      IF (COMPARE_BTIM(LOGIN_BTIM,NOLOGIN_BTIM).GE.0) THENM
						! DISMAIL or SET LOGIN set
		 IF (CLI$PRESENT('ALL')) THENR
		    CALL COPY2(LOGIN_BTIM,TODAY_BTIM(1))
		 ELSE=
		    RETURN			! Don't notify 
	         END IF
	      END IF(
	      CALL COPY2(LOGIN_BTIM_SAVE,LOGIN_BTIM)L
	      CALL COPY2(LOGIN_BTIM,TODAY_BTIM)
	      REWRITE (4) USER_ENTRYN
	      IF (SYSTEM_FLAG(1).NE.0.AND.SYSTEM_FLAG(1).NE.1) READIT = 1
	      DO I = 1,FLONG 
		 IF (SET_FLAG(I).NE.0.OR.BRIEF_FLAG(I).NE.0.OR.U
     &		    (I.GT.1.AND.SYSTEM_FLAG(I).NE.0)) READIT = 1
	      END DOU
	   ELSE
	      CALL CLEANUP_LOGIN		! Good time to delete dead usersM
	      CALL COPY2(READ_BTIM,NEW_BTIM)	! Make new entry
	      DO I = 1,FLONGA
	         SET_FLAG(I) = SET_FLAG_DEF(I).
	         BRIEF_FLAG(I) = BRIEF_FLAG_DEF(I) 
		 NOTIFY_FLAG(I) = NOTIFY_FLAG_DEF(I)
	      END DO 
	      NEW_FLAG(1) = 143
	      NEW_FLAG(2) = 0
	      CALL CHECK_NEWUSER(USERNAME,DISMAIL,PASSCHANGE)
	      IF (DISMAIL.EQ.1) THENI
		 CALL COPY2(LOGIN_BTIM,NOLOGIN_BTIM)
	         CALL COPY2(LOGIN_BTIM_SAVE,LOGIN_BTIM)
	      ELSE 
	         CALL COPY2(LOGIN_BTIM_SAVE,NEW_BTIM)
		 CALL COPY2(LOGIN_BTIM,TODAY_BTIM)
	         DO I = 1,FLONG
		    IF (SET_FLAG(I).NE.0) READIT = 1
	         END DO
		 IF (COMPARE_BTIM(PASSCHANGE,NEWEST_BTIM).LT.0) IER1 = 0
			! Old password change indicates user is new to BULLETIN
			! but not to system, so don't limit message viewing.I
	      END IF 
	      CALL WRITE_USER_FILE(IER)
	      IF (IER.NE.0) THEN		! Error in writing to user file
		 WRITE (6,1070)			! Tell user of the error
		 CALL CLOSE_BULLUSER		! Close the user file 
		 CALL EXIT			! Go away... 
	      END IF 
	      IF (DISMAIL.EQ.1) RETURN		! Go away if DISMAIL setI
	      DIFF = -1				! Force us to look at messages
	      CALL OPEN_BULLINF_SHARED.
	      DO I=1,FOLDER_MAX
	         CALL COPY2(LAST_READ_BTIM(1,I),READ_BTIM) 
	      END DOB
	      WRITE (9,IOSTAT=IER) USERNAME, 
     &		((LAST_READ_BTIM(I,J),I=1,2),J=1,FOLDER_MAX)
	      CALL CLOSE_BULLINF 
	   END IF
	   CALL COPY2(LOGIN_BTIM,LOGIN_BTIM_SAVE)
	   CALL READ_USER_FILE_HEADER(IER2)	! Reset read back to header
	END IF(

	IF (IER.EQ.0.AND.MINUTE_DIFF(TODAY_BTIM,BBOARD_BTIM)E
     &			.GT.BBOARD_UPDATE) THEN	! Update BBOARD mail?
	   CALL COPY2(BBOARD_BTIM,TODAY_BTIM)
	   REWRITE (4) USER_HEADER		! Rewrite header
	   IF (.NOT.TEST_BULLCP()) CALL CREATE_PROCESS('BBOARD')	
	ELSE IF (IER.NE.0) THEN
	   CALL CLOSE_BULLUSER1
	   CALL EXIT			! If no header, no messages 
	END IF.

	IF (IER1.EQ.0) THEN		! Skip date comparison if new entry1
C	
C  Compare and see if messages have been added since the last time
C  that the user has logged in or used the BULLETIN facility.=
CR
	   DIFF1 = COMPARE_BTIM(LOGIN_BTIM,READ_BTIM)
	   IF (DIFF1.LT.0) THEN		! If read messages since last login,
	      CALL COPY2(LOGIN_BTIM,READ_BTIM)=
		! then use read date to compare with latest bulletin dateL
	   END IF			! to see if should alert user. 

	   IF (SYSTEM_SWITCH) THEN)
	      DIFF1 = COMPARE_BTIM(SYSTEM_LOGIN_BTIM,NEWEST_BTIM)
	   ELSE
	      DIFF1 = COMPARE_BTIM(LOGIN_BTIM,NEWEST_BTIM)-
	   END IF
	END IF 

	CALL COPY2(LOGIN_BTIM_SAVE,LOGIN_BTIM)  ! Destroyed in UPDATE_READ
	 
	IF (NEW_FLAG(2).NE.0.AND.NEW_FLAG(2).NE.-1) THENL
	   CALL LIB$MOVC3(4,NEW_FLAG(2),%REF(BULL_PARAMETER))
	   CALL SUBTIME(LOGIN_BTIM,BULL_PARAMETER(:4),IER)1
	ELSE IF (DIFF1.GT.0) THEN
	   BULL_POINT = -1_
	   IF (READIT.EQ.1) THENN
	      CALL UPDATE_READ(1)
	      CALL COPY2(LOGIN_BTIM_NEW,LOGIN_BTIM)
	      CALL READ_IN_FOLDERS 
	      CALL MODIFY_SYSTEM_LIST(1) 
	   END IF
	   CALL CLOSE_BULLUSERI
	   RETURN
	END IFD

	CALL READ_IN_FOLDERS 
	CALL MODIFY_SYSTEM_LIST(1)U
        FOLDER_FILE = FOLDER_DIRECTORY(:TRIM(FOLDER_DIRECTORY))//FOLDERT

	ENTRY LOGIN_FOLDERI

	IF (NEW_FLAG(2).EQ.0.OR.NEW_FLAG(2).EQ.-1.OR.FOLDER_SET) THEN
	   CALL COPY2(LOGIN_BTIM,LOGIN_BTIM_SAVE)
	END IFG

	IF (REMOTE_SET.EQ.1) THEN	! If system remote folder, use remote
					! info, not local login timeL
	   IF (LAST_SYS_BTIM(1,FOLDER_NUMBER+1).NE.0) THENB
	    CALL COPY2(LOGIN_BTIM,LAST_SYS_BTIM(1,FOLDER_NUMBER+1))
	    LAST_SYS_BTIM(1,FOLDER_NUMBER+1) = 0F
	    LAST_SYS_BTIM(2,FOLDER_NUMBER+1) = 0A
	   ELSE
	    DIFF1 = COMPARE_BTIM(LOGIN_BTIM,L
     &			LAST_READ_BTIM(1,FOLDER_NUMBER+1)) 
	    IF (DIFF1.LT.0) THEN
	      CALL COPY2(LOGIN_BTIM,LAST_READ_BTIM(1,FOLDER_NUMBER+1)) 
	    ELSED
	      DIFF = MINUTE_DIFF(LOGIN_BTIM,F_NEWEST_BTIM)U
	      IF (DIFF.GE.0.AND.DIFF.LE.15) THEN  ! BULLCP updates every 15 min
	         IER = SYS$BINTIM('0 00:15',BULLCP_BTIM)A
	         BULLCP_BTIM(1) = -BULLCP_BTIM(1) ! Convert to -delta time0
	         BULLCP_BTIM(2) = -BULLCP_BTIM(2)-1
	         CALL LIB$SUBX(LOGIN_BTIM,BULLCP_BTIM,LOGIN_BTIM)
	      END IFX
	    END IFN
	   END IF
	END IFU

	ENTRY SHOW_SYSTEM

	JUST_SYSTEM = (.NOT.LOGIN_SWITCH.AND.SYSTEM_SWITCH).OR.
     &	        (BTEST(FOLDER_FLAG,2)
     &		.AND..NOT.TEST_SET_FLAG(FOLDER_NUMBER)
     &		.AND..NOT.TEST_BRIEF_FLAG(FOLDER_NUMBER)) 

	NGEN = 0			! Number of general messages
	NSYS = 0			! Number of system messages.
	BULL_POINT = -1

	IF (IER1.NE.0.AND.FOLDER_NUMBER.GT.0) THENE
	   IF (LOGIN_SWITCH) THEN
	      IF (READIT.EQ.1) THEN
	         CALL COPY2(LOGIN_BTIM_SAVE,LOGIN_BTIM)
	         CALL UPDATE_READ(1)
	         CALL COPY2(LOGIN_BTIM_NEW,LOGIN_BTIM) 
	      END IFC
	      CALL CLOSE_BULLUSER
	   END IF
	   RETURN	! Don't overwhelm new user with lots of non-general msgsF
	END IF(

	IF (BTEST(FOLDER_FLAG,2).AND.SYSTEM_SWITCH) THEN3
			! Can folder have SYSTEM messages and /SYSTEM specified?E
	   CALL COPY2(LOGIN_BTIM,SYSTEM_LOGIN_BTIM) ! Use specified login timee
						    ! for system messages.
	END IF 

	IF (LOGIN_SWITCH) THENF
	   IF (READIT.EQ.1) THEN	
	      CALL COPY2(LOGIN_BTIM_OLD,LOGIN_BTIM)
	      CALL UPDATE_READ(1)
	      CALL COPY2(LOGIN_BTIM_NEW,LOGIN_BTIM)
	      CALL COPY2(LOGIN_BTIM,LOGIN_BTIM_OLD)
	   END IF
	   CALL CLOSE_BULLUSER 
	END IFH

	IF (READIT.EQ.1.AND.FOLDER_NUMBER.GE.0.AND.REMOTE_SET.LT.3) THEN	
	   IF (LAST_SYS_BTIM(1,FOLDER_NUMBER+1).NE.0) THEN1
	      DIFF1 = COMPARE_BTIM(LOGIN_BTIM,V
     &				LAST_SYS_BTIM(1,FOLDER_NUMBER+1))E
	      IF (DIFF1.LT.0) THEN 
	         CALL COPY2(LOGIN_BTIM,LAST_SYS_BTIM(1,FOLDER_NUMBER+1))N
	      END IF 
	      CALL COPY2(LAST_SYS_BTIM(1,FOLDER_NUMBER+1),T
     &			 LOGIN_BTIM_NEW)D
	   END IF

	   IF (TEST2(BRIEF_FLAG,FOLDER_NUMBER)G
     &		       .AND.TEST2(SET_FLAG,FOLDER_NUMBER)) THENN
	      IF (.NOT.TEST2(SYSTEM_FLAG,FOLDER_NUMBER)) GO TO 9999
	   END IF
	END IFN

	CALL OPEN_BULLDIR_SHARED	! Get bulletin directory
	IF (.NOT.REMOTE_SET) THEN
	   CALL READDIR(0,IER)		! Get header info
	ELSEL
	   NBULL = F_NBULLI
	END IF 
	   
	CALL INIT_QUEUE(GEN_DIR1,BULLDIR_ENTRY)
	CALL INIT_QUEUE(SYS_DIR1,BULLDIR_ENTRY)
	CALL INIT_QUEUE(SYS_NUM1,%DESCR(ICOUNT))(
	GEN_DIR = GEN_DIR1M
	SYS_DIR = SYS_DIR1I
	SYS_NUM = SYS_NUM1 
	START = 1
	REVERSE = 0
	IF ((.NOT.TEST_SET_FLAG(FOLDER_NUMBER).OR..
     &		.NOT.TEST_BRIEF_FLAG(FOLDER_NUMBER))
     &		.AND..NOT.BTEST(FOLDER_FLAG,7)) THEN
	   IF (REVERSE_SWITCH) REVERSE = 1	
	   IF (IER1.EQ.0) THENU
	      CALL GET_NEWEST_MSG(LOGIN_BTIM,START)
	      IF (START.EQ.-1) START = NBULL + 1	
	   END IF
	END IFE

	IF (REMOTE_SET) THEN 
	   CALL INIT_QUEUE(ALL_DIR1,BULLDIR_ENTRY)T
	   ALL_DIR = ALL_DIR1
	   CALL REMOTE_DIRECTORY_COMMAND(START,NBULL,
     &					 .NOT.REVERSE,ALL_DIR,IER) 
	   IF (IER.NE.0) THEN
	      CALL CLOSE_BULLDIRV
	      CALL DISCONNECT_REMOTE 
	      GO TO 9999L
	   END IF
	   LAST_DIR = ALL_DIR
	   ALL_DIR = ALL_DIR1
	END IF,

	DO ICOUNT1 = NBULL,START,-1
	   IF (REVERSE) THEN 
	      ICOUNT = NBULL + START - ICOUNT1E
	   ELSE
	      ICOUNT = ICOUNT1E
	   END IF
	   IF (REMOTE_SET) THEN
	      IF (ALL_DIR.EQ.LAST_DIR) GO TO 100L
	      CALL READ_QUEUE(%VAL(ALL_DIR),ALL_DIR,BULLDIR_ENTRY) 
	      IER = ICOUNT + 1G
	   ELSE
	      CALL READDIR(ICOUNT,IER)E
	   END IF
	   IF (IER1.EQ.0.AND.IER.EQ.ICOUNT+1) THEN ! Is this a totally new user?T
	      IF (.NOT.REVERSE.AND..NOT.BTEST(FOLDER_FLAG,7)) THEN 
	         DIFF = COMPARE_BTIM(LOGIN_BTIM,MSG_BTIM) ! No, so compare date
	         IF (DIFF.GT.0) GO TO 100
	      END IFD
	      IF (.NOT.BTEST(FOLDER_FLAG,2)) SYSTEM = SYSTEM.AND.(.NOT.1)
			! Show system msg in non-system folder as general msg
	      IF (USERNAME.NE.FROM.OR.SYSTEM) THEN
				  	! Is bulletin system or from same user?
		 IF (SYSTEM) THEN	! Is it system bulletin?  
		    NSYS = NSYS + 1D
		    CALL WRITE_QUEUE(%VAL(SYS_DIR),SYS_DIR,BULLDIR_ENTRY)e
		    CALL WRITE_QUEUE(%VAL(SYS_NUM),SYS_NUM,%DESCR(ICOUNT))
	         ELSE IF (.NOT.JUST_SYSTEM) THEN
		    IF (BTEST(FOLDER_FLAG,7)) THEN
		       DIFF = COMPARE_BTIM
     &			      (LAST_READ_BTIM(1,FOLDER_NUMBER+1),MSG_BTIM).
		    ELSE IF (.NOT.SYSTEM_SWITCH) THENL
		       DIFF = -1
		    ELSE
	               DIFF = COMPARE_BTIM(LOGIN_BTIM_SAVE,MSG_BTIM)N
		    END IF
		    IF (DIFF.LT.0) THEN:
		       IF (.NOT.REVERSE.OR.BULL_POINT.EQ.-1) THENO
		          BULL_POINT = ICOUNT - 1Y
		          IF (.NOT.BTEST(FOLDER_FLAG,2).AND.
     &			   TEST_BRIEF_FLAG(FOLDER_NUMBER).AND. 
     &			   TEST_SET_FLAG(FOLDER_NUMBER)) GO TO 100F
		       END IF1
		       NGEN = NGEN + 1
		       SYSTEM = ICOUNT
		       CALL WRITE_QUEUE(%VAL(GEN_DIR),GEN_DIR,BULLDIR_ENTRY)
		    END IF
	         END IF
	      END IFU
	   ELSE IF (IER.EQ.ICOUNT+1) THEN
			! Totally new user, save only permanent system msgs
	      IF ((SYSTEM.AND.7).EQ.3.OR.
     &		  (SYSTEM.AND.BTEST(FOLDER_FLAG,7))) THENR
	         NSYS = NSYS + 1,
		 CALL WRITE_QUEUE(%VAL(SYS_DIR),SYS_DIR,BULLDIR_ENTRY)
		 CALL WRITE_QUEUE(%VAL(SYS_NUM),SYS_NUM,%DESCR(ICOUNT)),
	      ELSE IF (NGEN.EQ.0.OR.	! And save only the first non-system msg
     &		     BTEST(FOLDER_FLAG,7)) THEN ! and SET ALWAYS folder messages
		 SYSTEM = ICOUNT	! Save bulletin number for displayT
		 IF (.NOT.REVERSE.OR.BULL_POINT.EQ.-1) THEN.
		    BULL_POINT = ICOUNT - 13
		    IF (.NOT.BTEST(FOLDER_FLAG,2).AND.
     &			TEST_BRIEF_FLAG(FOLDER_NUMBER).AND.
     &		 	TEST_SET_FLAG(FOLDER_NUMBER)) GO TO 100E
		 END IF 
		 NGEN = NGEN + 1
		 CALL WRITE_QUEUE(%VAL(GEN_DIR),GEN_DIR,BULLDIR_ENTRY)
	      END IF_
	   END IF
	END DO 
100	CALL CLOSE_BULLDIR
CA
C  Review new directory entries.  If there are system messages, 
C  copy the system bulletin into GEN_DIR file BULLSYS.SCR for outputting
C  to the terminal.  If there are simple messages, just output the
C  header information.
CF
	IF (TEST_BRIEF_FLAG(FOLDER_NUMBER).AND.
     &			   TEST_SET_FLAG(FOLDER_NUMBER)) NGEN = 0

	IF (NGEN.EQ.0.AND.NSYS.EQ.0) GO TO 9999

	IF (NSYS.GT.0) THEN		! Are there any system messages?
	   IF (FIRST_WRITE) THENO
	      PAGE = 4		! Don't erase MAIL/PASSWORD notifiese
	      FIRST_WRITE = .FALSE.	! if this is first write to screen.
	   END IF
	   LENF = TRIM(FOLDER_NAME)
	   S1 = (PAGE_WIDTH-(LENF+16))/2_
	   S2 = PAGE_WIDTH - S1 - (LENF + 16)
	   WRITE (6,'(''+'',A,$)') CTRL_G
	   WRITE (6,1026) FOLDER_NAME(:LENF)		! Yep...U
	   PAGE = PAGE + 1E
	   CTRL_G = 0		! Don't ring bell for non-system bulls
	   CALL OPEN_BULLFIL_SHARED
	   CALL INIT_QUEUE(SYS_BUL1,INPUT).
	   SYS_BUL = SYS_BUL1
	   SYS_DIR = SYS_DIR1
	   SYS_NUM = SYS_NUM1
	   NSYS_LINE = 0L
	   DO J=1,NSYS 
	      CALL READ_QUEUE(%VAL(SYS_DIR),SYS_DIR,BULLDIR_ENTRY)E
	      IF (REMOTE_SET) THENE
	         CALL READ_QUEUE(%VAL(SYS_NUM),SYS_NUM,%DESCR(ICOUNT))D
	         WRITE (REMOTE_UNIT,'(2A)',IOSTAT=IER) 5,ICOUNT
	         IF (IER.GT.0) THEN
	            CALL DISCONNECT_REMOTE
	         ELSE
	            CALL GET_REMOTE_MESSAGE(IER)I
	         END IF
		 IF (IER.GT.0) THENT
		    CALL CLOSE_BULLFIL
		    GO TO 9999
		 END IFM
	      END IFG
 	      INPUT = ' 'Y
	      CALL WRITE_QUEUE(%VAL(SYS_BUL),SYS_BUL,INPUT)
	      NSYS_LINE = NSYS_LINE + 1
	      ILEN = LINE_LENGTH + 1 
	      CALL GET_BULL_LINE(BLOCK,LENGTH,INPUT,ILEN)
	      IF (ILEN.GT.0.AND.INPUT(:6).EQ.'From: ') THEN
	         CALL GET_BULL_LINE(BLOCK,LENGTH,INPUT,ILEN)_
	      END IF 
	      IF (ILEN.GT.0.AND.INPUT(:6).EQ.'Subj: ') THEN
	         CALL GET_BULL_LINE(BLOCK,LENGTH,INPUT,ILEN)
	      END IF.
	      DO WHILE (ILEN.GT.0)	! Copy bulletin to SYS_BUL link list
		 CALL WRITE_QUEUE(%VAL(SYS_BUL),SYS_BUL,INPUT)
		 NSYS_LINE = NSYS_LINE + 1
		 CALL GET_BULL_LINE(BLOCK,LENGTH,INPUT,ILEN)
	      END DO=
	      IF (ILEN.LT.0) THEN
		 CALL CLOSE_BULLFIL 
		 GO TO 9999(
	      END IFN
	      IF (J.LT.NSYS.AND.SEPARATE.NE.' ') THEN
 	         INPUT = ' '
	         CALL WRITE_QUEUE(%VAL(SYS_BUL),SYS_BUL,INPUT) 
		 DO I=1,PAGE_WIDTH
		    INPUT(I:I) = SEPARATEE
		 END DOI
		 CALL WRITE_QUEUE(%VAL(SYS_BUL),SYS_BUL,INPUT)
	         NSYS_LINE = NSYS_LINE + 2T
	      END IF_
	   END DO
	   CALL CLOSE_BULLFIL
	   SYS_BUL = SYS_BUL1
	   ILEN = 0
	   I = 1	
	   WIDTH = PAGE_WIDTH
	   LEFT = .FALSE.
	   DO WHILE (I.LE.NSYS_LINE.OR.ILEN.GT.0)  ! Write out system messages'
	      IF (ILEN.EQ.0) THEN
	         CALL READ_QUEUE(%VAL(SYS_BUL),SYS_BUL,INPUT)
		 ILEN = TRIM(INPUT)E
		 I = I + 1
	      END IFE
	      IF (SYS_BUL.NE.0) THENe
		 IF (PAGE.EQ.PAGE_LENGTH-2.AND.PAGING) THEN 
							! If at end of screen
		    WRITE(6,1080)	! Ask for input to proceed to next pagen
		    CALL GET_INPUT_NOECHO_PROMPT(INREAD(:1),
     &			'HIT any key for next page....') 
	            WRITE (6,'(1X)'),
	            CALL LIB$ERASE_PAGE(1,1)		! Clear the screeno
		    PAGE = 1
	            INREAD = '+',
		 ELSE IF (WIDTH.EQ.PAGE_WIDTH.OR.LEFT) THEN 
		    PAGE = PAGE + 1
	            INREAD = ' 'O
		 END IF 
		 IF (LEFT) THENI
 		    WRITE(6,1050) INREAD(:1)//INPUT(:ILEN)A
		    LEFT = .FALSE.
	            ILEN = 0N
	            INREAD = '+'L
		 ELSE IF (ILEN.LE.WIDTH) THEN 
 		    WRITE(6,1060) INREAD(:1)//INPUT(:ILEN)L
		    WIDTH = PAGE_WIDTH
		    ILEN = 0
		 ELSEU
		    DO WHILE (WIDTH.GT.0.AND.INPUT(WIDTH:WIDTH).NE.' ')	
	               WIDTH = WIDTH - 1 
		    END DO
		    WRITE(6,1060) INREAD(:1)//INPUT(:WIDTH)$
	            INPUT = INPUT(WIDTH+1:)
		    ILEN = ILEN - WIDTH 
		    DO WHILE (INPUT(:1).EQ.' '.AND.ILEN.GT.0)
		       ILEN = ILEN - 1
		       INPUT = INPUT(2:)
	            END DOR
                    IF (INPUT(ILEN:ILEN).EQ.' ') THENL
	               CONTINUE
		    ELSE IF (ALPHA(INPUT(ILEN:ILEN))) THEN
		       INPUT = INPUT(:ILEN)//' '
		       ILEN = ILEN + 1
		    ELSE
		       INPUT = INPUT(:ILEN)//'  '
		       ILEN = ILEN + 2
		    END IF
		    WIDTH = PAGE_WIDTH - ILEND
		    IF (WIDTH.GT.0) THEN
		       IF (ILEN.GT.0) LEFT = .TRUE. 
		    ELSE
	               WIDTH = PAGE_WIDTH
		    END IF
		 END IFP
	      END IFN
	   END DO
	   IF (NGEN.EQ.0) THEN 
	      WRITE (6,'(A)')		! Write delimiting blank lineT
	   END IF
	   PAGE = PAGE + 1P
	END IFN

	ENTRY REDISPLAY_DIRECTORY

	GEN_DIR = GEN_DIR1T
	IF (NGEN.GT.0) THEN		! Are there new non-system messages?
	   LENF = TRIM(FOLDER_NAME)
	   S1 = (PAGE_WIDTH-13-LENF)/2r
	   S2 = PAGE_WIDTH-S1-13-LENF
	   IF (PAGE+7+NGEN.GT.PAGE_LENGTH.AND.PAGE.GT.0) THEN
	      WRITE(6,1080)		! Ask for input to proceed to next page#
	      CALL GET_INPUT_NOECHO_PROMPT(INREAD(:1),	! Get terminal input
     &			'HIT any key for next page....')<
	      WRITE (6,'(1X)')9
	      CALL LIB$ERASE_PAGE(1,1)	! Clear the screen
	      WRITE (6,'(''+'',A,$)') CTRL_GS
	      WRITE(6,1028) 'New '//FOLDER_NAME(:LENF)//' messages'
	      PAGE = 1F
	   ELSE
	      IF (FIRST_WRITE) THEN
		 PAGE = 4		  ! Don't erase MAIL/PASSWORD notifies
	         FIRST_WRITE = .FALSE. ! if this is first write to screen.I
	      END IF
	      WRITE (6,'(''+'',A,$)') CTRL_GG
	      WRITE(6,1027) 'New '//FOLDER_NAME(:LENF)//' messages'
	      PAGE = PAGE + 1
	   END IF
	   WRITE(6,1020)R
	   WRITE(6,1025)
	   PAGE = PAGE + 2_
	   I = 0)
	   DO WHILE (I.LT.NGEN)
	      I = I + 1
	      CALL READ_QUEUE(%VAL(GEN_DIR),GEN_DIR,BULLDIR_ENTRY)
	      CALL CONVERT_ENTRY_FROMBIN_FOLDER
	      N = MAX(INT(LOG10(REAL(SYSTEM)))+1,3)
	      N1 = MAX(1,6-N)
	      IF (PAGE.EQ.PAGE_LENGTH-2.AND.PAGING) THEN ! If at end of screenS
		 WRITE(6,1080)	! Ask for input to proceed to next page
		 CALL GET_INPUT_NOECHO_PROMPT(INREAD(:1),
     &		'HIT Q(Quit listing) or any other key for next page....')R
	         CALL STR$UPCASE(INREAD(:1),INREAD(:1))
	         WRITE (6,'(1X)')
	         CALL LIB$ERASE_PAGE(1,1)		! Clear the screen
		 PAGE = 1N
		 IF (INREAD(:1).EQ.'Q') THEN
		    I = NGEN		! Quit directory listing
		    WRITE(6,'(''+Quitting directory listing.'')')I
		 ELSE=
		    WRITE(6,1040) '+'//DESCRIP(:53),FROM,DATE(:6),SYSTEM
		 END IFC
					! Bulletin number is stored in SYSTEM
	      ELSEG
		 PAGE = PAGE + 1
		 WRITE(6,1040) ' '//DESCRIP(:53),FROM,DATE(:6),SYSTEMB
	      END IF
	   END DO
	   IF ((.NOT.FOLDER_SET.AND.BTEST(SET_FLAG(1),0).AND.DIFF1.LE.0)S
     &		.OR.(FOLDER_SET.AND.TEST_SET_FLAG(FOLDER_NUMBER))) THEN 
	      PAGE = 0	! Don't reset page counter if READNEW not set,
	   END IF	! as no prompt to read is generated.I
	END IFT
C	
C  Instruct users how to read displayed messages if READNEW not selected.
C 
	IF (.NOT.TEST_BRIEF_FLAG(FOLDER_NUMBER).AND.L
     &		TEST_SET_FLAG(FOLDER_NUMBER)) THEN
	   WRITE(6,1030).
	ELSE IF (NGEN.EQ.0) THEN
	   ILEN = 57 + INDEX(COMMAND_PROMPT,'>') - 1 
	   S1 = (PAGE_WIDTH-ILEN)/2
	   S2 = PAGE_WIDTH - S1 - ILEN(
	   WRITE(6,1035) 'The '//COMMAND_PROMPT(:ILEN-57)//
     &		'/SYSTEM command can be used to reread these messages.'P
	   PAGE = PAGE + 1I
	ELSES
	   FLEN = TRIM(FOLDER_NAME)
	   IF (FOLDER_NUMBER.EQ.0) FLEN = -1P
	   ILEN = 30 + INDEX(COMMAND_PROMPT,'>') - 1 + FLEN
	   S1 = (PAGE_WIDTH-ILEN)/2
	   S2 = PAGE_WIDTH - S1 - ILEN 
	   IF (FOLDER_NUMBER.EQ.0) THEN
	      WRITE(6,1035) 'Type ' //COMMAND_PROMPT(:ILEN-29)//E
     &		' to read these messages.'
	   ELSE
	      WRITE(6,1035) 'Type '//COMMAND_PROMPT(:ILEN-30-FLEN)
     &		//' '//FOLDER_NAME(:FLEN)//.
     &		' to read these messages.'
	   END IF
	   PAGE = PAGE + 1)
	END IF 

9999	IF (LOGIN_SWITCH) THEN:
	   CALL COPY2(LOGIN_BTIM,LOGIN_BTIM_NEW)N
	   CALL COPY2(LOGIN_BTIM_SAVE,LOGIN_BTIM_OLD)
	END IFI
	RETURNL

1020	FORMAT(' Description',43X,'From',9X,'Date',3X,'Number')
1025	FORMAT(' -----------',43X,'----',9X,'----',3X,'------')
1026	FORMAT(' ',<S1>('*'),A,' System Messages',<S2>('*'))I
1027	FORMAT(/,' ',<S1>('*'),A,<S2>('*'))
1028	FORMAT('+',<S1>('*'),A,<S2>('*'))
1030	FORMAT(' ',<PAGE_WIDTH>('*'))
1035	FORMAT(' ',<S1>('*'),A,<S2>('*'))
1040	FORMAT(A<53>,2X,A12,1X,A6,<N1>X,I<N>)
1050	FORMAT(A,$)
1060	FORMAT(A)
1070	FORMAT(' ERROR: Cannot add new entry to user file.')A
1080	FORMAT(' ',/)

	END


	(

	SUBROUTINE GET_NODE_NUMBER_OTHER(NODE_NUMBER,NODE_AREA,NODE_NAME)

	IMPLICIT INTEGER (A-Z)A

	INCLUDE '($SYIDEF)'

	CHARACTER*(*) NODE_NAME

	CALL INIT_ITMLST	! Initialize item list
				! Now add items to listB
	CALL ADD_2_ITMLST(4,SYI$_NODE_AREA,%LOC(NODE_AREA))
	CALL ADD_2_ITMLST(4,SYI$_NODE_NUMBER,%LOC(NODE_NUMBER))
	CALL END_ITMLST(GETSYI_ITMLST)	! Get address of itemlist1

	IER = SYS$GETSYIW(,,NODE_NAME(:TRIM(NODE_NAME)),s
     &			%VAL(GETSYI_ITMLST),,,)	! Get Info command.

	IF (.NOT.IER) THENR
	   WRITE (6,'('' ERROR: Specified node name not found.'')')
	   NODE_AREA = 0 
	END IFE

	RETURNB
	END

