************************************************************
*                                                          *
* DELETE                                                   *
*                                                          *
* This routine will delete the file specified in the       *
* name compare buffer.  If the file is open, it will       *
* be closed before deleting the file.                      *
*                                                          *
* The following steps are taken.                           *
*  - Check that the file exists                            *
*  - Check that the file is not protected                  *
*  - Remove the entry from the directory and               *
*    decrement the number of file in the directory         *
*  - Deallocate all AUs associated with the file.          *
*                                                          *
************************************************************

DEL0   ANDI R12,>FF00
       AI   R12,24
       LDCR @B02,4       Select RAM bank 2

* First check to see if the file is open
*
       BLWP @IFO
       JNE  DELE20

* OK, the file is open.  See if it has the protection bit set
*
       MOVB @2(R5),R7    Get bank where FDR is stored
       MOV  @4(R5),R8    Get address of FDR
       MOV  @48(R5),R4   Get AU of FDR
       CLR  @2(R5)       Close the file
       MOV  R8,R5
       JMP  DELE21
*      LDCR R7,4             Select RAM bank where FDR is located
*      MOV  @12(R8),R0       Get file flags
*      COC  @PROBIT,R0       Check to see if file is protected
*      JEQ  DELPRO
*
*      LDCR @B02,4       Select bank 2 again
*      CLR  @2(R5)       Close the file
*      MOV  @48(R5),R0   Save AU of FDR
*
* Copy the FDR to the buffer
*
*      LI   R2,DFDR
*      LI   R3,256
*DELE2  LDCR R7,4
*      MOV  *R8+,R4
*      LDCR @B02,4       Select RAM bank 2
*      MOV  R4,*R2+
*      DECT R3
*      JNE  DELE2
*
*      MOV  R0,@DFDR     Put AU in the delete buffer
*      B    @DELE50
*
*
* DELPRO - Here is where we error out if we're trying
*          to delete a file that is protected
*

DELPRO BL   @DSRERR
       DATA >0100            File is protected

* We branch to here if the file is not open
*
DELE20 BL   @GETFDR          See if the file exists
       CI   R5,0
       JEQ  DELERR
       CLR  R7               Bank where FDR is
       LDCR @B04,4
       MOV  @SAVEAU,R4       Get AU of FDR

* See if the file is protected
*
DELE21 LDCR R7,4
       MOV  @12(R5),R0        Get file flags
       COC  @PROBIT,R0
       JEQ  DELPRO

* Copy the FDR to DFDR and store the AU of the FDR in the
* first word.
*
       LDCR @B02,4       Save AU of the FDR
       MOV  R4,@DFDR

DELE22 AI   R5,40        Point to data chain
       LI   R0,216       216 bytes of data chain pointers
       LI   R1,DFDR+40
DELE23 LDCR R7,4         Select RAM bank 0
       MOV  *R5+,R2
       LDCR @B02,4       Select RAM bank 2
       MOV  R2,*R1+
       DECT R0
       JNE  DELE23
*      JMP  DELE50

* Now that we have the Delete FDR (DFDR) buffer all set up,
* delete the actual file
*
*DELE50
* Step 1: Decrement the number of files in the directory
*
       BL   @GETDDR
       CI   R5,0         This shouldn't happen, but check anyway
       JEQ  DELERR
       MOV  @SAVEAU,R7   Get AU of the parent directory
       MOV  R6,R3
       SRL  R3,8
       SLA  R3,1
       MPY  @SAUTBL(R3),R7
       LI   R4,SECBUF
       SRL  R8,1
       JNC  DELE51
       AI   R4,>100
DELE51 SRL  R7,1
       JNC  DELE52
       AI   R8,>8000
DELE52 LDCR @ZERO,4
       BLWP @BANKIT
       DATA SCSIRD
       JNE  DELERR
       SB   @B01,@22(R4)     Decrement # of files
       BLWP @BANKIT          Write DDR back out
       DATA SCSIWT
       JNE  DELERR

* Now decrement the number in the cache
*
       LDCR @B03,4
       SB   @B01,@22(R5)

* Step 2: Remove AU of the FDR from the index
*
       MOV  @24(R5),R7
       LDCR @B04,4       Select RAM bank 4
       MPY  @SAUTBL(R3),R7
       LI   R4,SECBUF
       SRL  R8,1
       JNC  DELE53
       AI   R4,>100
DELE53 SRL  R7,1
       JNC  DELE54
       AI   R8,>8000
DELE54 LDCR @ZERO,4
       BLWP @BANKIT
       DATA SCSIRD
       JNE  DELERR

       LDCR @B02,4       Select RAM bank 2
       MOV  @DFDR,R5     Get AU of the FDR
       LDCR @ZERO,4      Select RAM bank 0

DELE55 C    *R4+,R5      Is this the AU we're looking for?
       JEQ  DELE56
       CI   R4,>5EFE
       JEQ  DELERR
       CI   R4,>5FFE
       JNE  DELE55
*      JMP  DELE55

DELERR BL   @DSRERR
       DATA >0700        File error
DELE56 CI   R4,>5EFE     Do we need to move any more down?
       JEQ  DELE57
       CI   R4,>5FFE
       JEQ  DELE57
       MOV  *R4,@-2(R4)
       INCT R4
       JMP  DELE56

DELE57 CLR  @-2(R4)
       BLWP @BANKIT      Write index back out to disk
       DATA SCSIWT
       JNE  DELERR

* Now deallocate all AU's the file had used
*
       LDCR @B02,4       Select RAM bank 2
       MOV  @DFDR,R7     Get AU that FDR used to be in
       MOV  R7,R8
       LDCR @ZERO,4      Select RAM bank 0
       BLWP @DEALL

       LI   R4,DFDR+40

DELE58 LDCR @B02,4       Select RAM bank 2
       MOV  *R4+,R7
       JEQ  DELE59
       MOV  *R4+,R8
       LDCR @ZERO,4      Select RAM bank 0
       BLWP @DEALL
       CI   R4,DFDR+254  Are we done?
       JNE  DELE58

DELE59 LDCR @ZERO,4
       B    @DSRRT
