;*ADFMount
;---------
;Mount an ADF image into ADFFS
;Entry:
; R0 = pointer to command tail
; R1 = no. of parameters
; R12 = possibly pointer to module private word

 .GZ_validation    DCD &00088B1F	;GZ validation string
 .zlibMode         DCB "rb", 0		;zlib read-only
                   ALIGN


.Entry_ADFMount
 STMFD   R13!, {R1-R12, R14}
 TEMP    R14
 ADR     R12, VARS			;get our private area

 MOV     R11, R0			;store command tail for later

 LDR     R0, [R12, #prevent_Hourglass - VARS]
 TEQ     R0, #0				;should we display an Hourglass?
 MOVEQ   R0, #1				;YES
 SWIEQ   XHourglass_Start		; |+ PRM2-751

 MOV     R0, #0
 STR     R0, GZ_File_Handle		;ensure GZ file handle is blank

 MOV     R0, #23			;read catalogue information
 MOV     R1, R11			;point to filename
 SWI     XOS_File			;PRM2-38
 BVS     Entry_ADFMount_Exit		;exit on error

 TEQ     R0, #3				;is it a directory?
 BNE     _not_a_directory		;NO
 CMP     R4, #0				;YES, does it have a size?
 BHI     _possibly_MultiFS		;YES, might be a DOS disc image

 ._not_a_directory
 TEQ     R0, #1				;did we find a file?
 MOVNE   R2, R0				;NO, generate an error
 MOVNE   R0, #19			; |
 SWINE   XOS_File			; |  PRM2-45
 BVS     Entry_ADFMount_Exit		; +- exit and report an error

					;**************************************
					;read first 4 bytes to see if it's a GZip
 ._possibly_MultiFS			;**************************************
 MOV     R1, R11			;filename
 MOV     R0, #&4F			;open read only
 SWI     XOS_Find			;PRM2-78
 MOVVS   R2, #0				;generate an error
 MOVVS   R0, #19			; |
 SWIVS   XOS_File			; |  PRM2-45
 BVS     Entry_ADFMount_Exit		; +- exit and report an error
 MOV     R10, R0			;preserve the file handle

 MOV     R1, R0				;file handle
 MOV     R0, #4				;read absolute
 ADD     R2, R12, #APD_tmp - VARS	;pointer to temp buffer
 MOV     R3, #4				;bytes to read
 SWI     XOS_GBPB			;PRM2-69
 BVS     Entry_ADFMount_Exit		;exit on error

 MOV     R0, #0				;close file
 MOV     R1, R10			;file handle
 SWI     XOS_Find			;PRM2-77
 BVS     Entry_ADFMount_Exit		;exit on error

 BL      Entry_ADFEject			;Dismount current disc

 ADR     R14, Entry_ADFMount_Exit	;return address for errors

 LDR     R0, [R12, #APD_tmp - VARS]	;load the 4 bytes we just read
 LDR     R1, GZ_validation		;..and the GZ header validation
 TEQ     R1, R0				;do they match?
 BNE     Entry_ADFMount_ADF		;NO, not an APD or JFD must be ADF

					;**************************************
					;GZ support (APD, JFD)
					;**************************************
 MOV     R0, R11			;file name pointer
 ADR     R1, zlibMode			;zlib mode
 SWI     XZLib_GZOpen			;open file via zlib
 BVS     Entry_ADFMount_Exit
 TEQ     R0, #0				;did we get a handle back?
 ADREQ   R0, Error_File_Error		;NO
 BEQ     Entry_ADFMount_Exit		; |+ exit with error
 STR     R0, GZ_File_Handle		;file handle
					;R4 contains file length

 ADD     R1, R12, #APD_tmp - VARS	;buffer address
 MOV     R2, #4				;bytes to read
 SWI     XZLib_GZRead			;read the first 4 bytes
 BVS     Entry_ADFMount_Exit

 LDR     R0, [R12, #APD_tmp - VARS]
 LDR     R1, _APD_validation
 TEQ     R0, R1
 BICEQ   R0, R0, #&FF << 24		;     reduce to APD/JFD
 STREQ   R0, FileType_Loaded
 BEQ     Entry_ADFMount_APD

 LDR     R1, _JFD_validation
 TEQ     R0, R1
 BICEQ   R0, R0, #&FF << 24		;     reduce to APD/JFD
 STREQ   R0, FileType_Loaded
 BEQ     Entry_ADFMount_JFD
B       Entry_ADFMount_Exit		;unknown GZ file, exit

 ._JFD_validation	DCB "JFDI"
 ._APD_validation	DCB "APDX"




;Entry_ADFMount_mounted
;----------------------
;Once a file type has been loaded, it returns here

.Entry_ADFMount_mounted
 MVN     R0, #0
 STR     R0, head_track			;reset head position

 MOV     R0, #Max_File_Length - 1	;restrict loop
 ._L1
   LDRB    R2, [R11], #1		;load byte from command line
   TEQ     R2, #ASC(" ")		;skip spaces
   TEQNE   R2, #ASC("""")
 BEQ     _L1				;loop until first byte of filename
 SUB     R11, R11, #1			;R11=pointer to first char of filename
 ADR     R3, Current_File_Mounted
 TEQ     R3, R11			;check we're not loading a multi-disc
 BEQ     _P1
					;copy the file path/name
 ._L2					;..which is used by ADFFlush
   LDRB    R2, [R11], #1
   TEQ     R2, #ASC("""")		;terminate if we hit closing quote
   TEQ     R2, #13
   MOVEQ   R2, #0

   STRB    R2, [R3], #1
   TEQ     R2, #0
   SUBNES  R0, R0, #1
 BNE     _L2

 ._P1
 BL      get_current_discname
 BL      Check_Ambiguous_Disc_Name	;R0=0 for no error
 TEQ     R0, #0
 BLNE    get_current_discname		;try getting the discname again
 MOVVC   R0, #0
 BVC     Entry_ADFMount_Exit		;VC if discname known

 ADR     R1, ADFFS_Filer_Name		;disc name unknown, open ADFS::0.$
 ADR     R2, ADFFS_Current_DiscName
 ._L3
   LDRB    R0, [R1], #1
   STRB    R0, [R2], #1
   TEQ     R0, #0
 BNE     _L3

.Entry_ADFMount_Exit
 MOV     R10, R0			;preserve R0
 LDR     R0, GZ_File_Handle		;GZ file handle
 TEQ     R0, #0				;is there a GZ file open?
 MOVNE   R2, #0				;YES
 STRNE   R2, GZ_File_Handle		;|   clear handle
 SWINE   XZLib_GZClose			;|+  close GZ file if open

 LDR     R0, [R12, #prevent_Hourglass - VARS]
 TEQ     R0, #0				;should we display an Hourglass?
 SWIEQ   XHourglass_Off			;YES, PRM2-748
 LOCK    R14

 TEQ     R10, #0			;did an error occur?
 SUBEQS  R0, R10, R10			;ensure V cleared
 LDMEQFD R13!, {R1-R12, PC}		;NO, exit

 MOV     R0, #WimpSysInfo_ActiveTasks	;get number of WIMP tasks
 SWI     XWimp_ReadSysInfo		;PRM3-216
 TEQ     R0, #0				;R0>0 if WIMP is active
 MOV     R0, R10			;preserve R0
 BEQ     _no_wimp

 MOV     R1, #%1			;flags
 ADR     R2, ADFFS_Title		;error title
 SWI     Wimp_ReportError		;PRM3-176
LDMFD   R13!, {R1-R12, PC}

 ._no_wimp
 MOV     R1, #1<<31			;error
 ADDS    R1, R1, R1			;set V
LDMFD   R13!, {R1-R12, PC}




;*ADFOpen
;--------
;Mount an ADF image and open a filer window
;Entry:
; R0 = pointer to command tail
; R1 = no. of parameters
; R12 = point to module private word

.Entry_ADFOpen
 STMFD   R13!, {R1-R12, R14}

 BL      Entry_ADFMount			;mount the disc
 TEQ     R0, #0				;did an error occur?
 LDMNEFD R13!, {R1-R12, PC}		;YES, exit

 ADR     R0, Open_Filer_Window		;open the filer window
 MOV     R2, R0				;find end of discname
 ._L1
   LDRB    R1, [R2], #1
   TEQ     R1, #0
 BNE     _L1

 MOV     R1, #ASC(".")			;append .$
 STRB    R1, [R2, #-1]
 MOV     R1, #ASC("$")
 STRB    R1, [R2, #0]
 MOV     R1, #0
 STRB    R1, [R2, #1]			;and terminate
 SWI     XOS_CLI			;PRM1-961
 STRB    R1, [R2, #-1]			;terminate at actual name
LDMFD   R13!, {R1-R12, PC}




;get_current_discname
;--------------------
;Get the current ADF disc name
;
;Exit:
; R0, R1, R4, R5 corrupt
; R2 - disc name pointer
; V set on error

.get_current_discname
 STMFD    R13!, {R0-R5, R14}

 MOV     R0, #37			;Get the disc name PRM2-121 using
 ADR     R1, ADFFS_Filer_Name		;..convert path to canonicalised name
 ADR     R2, ADFFS_Current_DiscName
 MOV     R3, #0
 MOV     R4, #0
 MOV     R5, #32
 SWI     XOS_FSControl			;PRM2-80
 MOVVS   R4, #0				;error, blank the disc name
 STRVSB  R4, [R2, #0]			;probably "FileCore in use" caused
					;by ADFS looking for a disc change
 LDMVSFD R13!, {R0-R5, PC}
 CMP     R5, #0
 MOVMI   R5, #0				;if the disc name is too long, blank it
 RSBPL   R5, R5, #32 - 2		;terminate before the ".$"
 MOV     R0, #0
 STRB    R0, [R2, R5]			;terminate the discname
LDMFD   R13!, {R0-R5, PC}




.Check_Ambiguous_Disc_Name
 STMFD   R13!, {R1-R12, R14}
 ADR     R10, ADFFS_Current_DiscName

 MOV     R0, #17			;Check for Ambiguous Disc Name
 MOV     R1, R10
 SWI     XOS_File			;PRM2-38 do we get an error?
 BVC     _exit				;NO

 LDR     R14, [R0]			;is the error "Ambiguous Disc Name"?
 EORS    R14, R14, #Error_Ambiguous_Disc_Name
 BNE     _exit				;NO

 ADR     R9, Star_Dismount_DiscName	;YES, issue: *DISMOUNT ADFS::<discname>
 ADD     R10, R10, #6			;R10 now start of discname
 ._L1
   LDRB    R0, [R10], #1
   TEQ     R0, #ASC(".")		;copy to end of floppy name
   MOVEQ   R0, #0
   STRB    R0, [R9], #1
   TEQ     R0, #0
 BNE     _L1

 ADR     R0, Star_Dismount
 SWI     XOS_CLI
LDMFD   R13!, {R1-R12, PC}

 ._exit
 MOV     R0, #0
LDMFD   R13!, {R1-R12, PC}
