;*********************************************************************
;
; MAILOF.M68    -       MAIL LOGOFF Routine
;
;                  Written by: Dave Heyliger - AMUS Staff
;
;       Purpose: To inform the system that YOU are OFF of the terminal.
;
;          Used: Upon logoff routine via a "OFFLIN.CMD" file in CMD:
;
;*********************************************************************

       SEARCH  SYS                             ;grab the regulars
       SEARCH  SYSSYM
       SEARCH  TRM

       ;define a version number
       VMAJOR=1.                               ;original program by
       VMINOR=0.                               ;Dave Heyliger - AMUS
       VEDIT=0

       PHDR    -2,0,PH$REE!PH$REU              ;may be run logged out

       .OFINI
       .OFDEF  SCRAP,20.                       ;scrap buffer area workspace
       .OFDEF  MEMPTR,4                        ;EMAIL.SYS memory location
       .OFSIZ  IMPSIZ

       GETIMP  IMPSIZ,A3                       ;A3 points to variables

       ;point to EMAIL.SYS memory in system memory - A4 dedicated
SCHSYS: LEA     A2,SYSFIL                       ;point to ascii file
       LEA     A0,SCRAP(A3)                    ;A0 points to buffer area
       FILNAM  @A0                             ;create filename
       SRCH    @A0,A4                          ;A4 will contain mem pointer
       BEQ     SAVPTR                          ;search found EMAIL.SYS
       TYPECR  <?EMAIL.SYS not installed. Contact you SYSOP>
BYEBYE: EXIT

       ;save pointer and get the users JOBIDX - this is their "ID"
SAVPTR: MOV     A4,MEMPTR(A3)                   ;save the pointer to EMAIL.SYS
       JOBIDX  A6                              ;their "ID"

       ;get the number of "blocks" to scan in system memory
SCAN:   MOV     508.(A4),D4                     ;D4 contains block size
       MOV     #20.,D3                         ;number of user slots/block

       ;clear out all users from this terminal
LOOP:   PUSH    A4                              ;save pointer to bucket
       ADD     #14,A4                          ;point to IDX slot
       CMP     A6,@A4                          ;match?
       BNE     10$                             ;nope
5$:     CLR     @A4                             ;yup, clear it
       BR      30$                             ;get next bucket
10$:    ADD     #4,A4                           ;get next slot
       CMP     A6,@A4                          ;match?
       BNE     20$                             ;nope
       BR      5$                              ;yup
20$:    ADD     #4,A4                           ;get next slot
       CMP     A6,@A4                          ;match?
       BEQ     5$                              ;yup

       ;IDX not found in the array bucket, try next array bucket
30$:    DEC     D3                              ;one less array variable
       BEQ     NXTBLK                          ;time for next "block"
       POP     A4                              ;get original bucket pointer
       ADD     #24.,A4                         ;point to next user slot
       BR      LOOP                            ;and look again

       ;block has been searched - user not found. Get the next block
NXTBLK: DEC     D4                              ;one less block
       BEQ     DONE                            ;everyone off
       ADD     #56.,A4                         ;weird number
                                               ;(24. for the user, 32. extra)
       MOV     #20.,D3                         ;20 users/block
       BR      LOOP                            ;try searching again

       ;all users on this terminal are now off
DONE:   EXIT                                    ;back to the dot

SYSFIL: ASCII   /EMAIL.SYS/
       EVEN
END