COMPILE SUBROUTINE SAP_MFS(Code, BFS, Handle, Name, FMC, Record, Status)
#pragma precomp SRP_PreCompiler

DECLARE Subroutine Msg, FSMsg, Set_Status, SAP_Monitor, SRP_Stopwatch
DECLARE FUNCTION Set_Status, Get_Status, SAP_Monitor, RetStack

COMMON /FILENAME/ Files_Array, Handles_Array

FS = DELETE(BFS,1,1,1)
NEXTFS = FS<1,1,1>
@FILE.ERROR = ''

$INSERT FILE.SYSTEM.ONGOSUB
$INSERT FILE.SYSTEM.EQUATES
$INSERT MSG_EQUATES
$INSERT DICT_EQUATES
$INSERT NCR_EQUATES
$INSERT WO_LOG_EQUATES

EQU ReadRec$ TO 1
	
RETURN

/* Directly called functions - don't pass to next file system */

INSTALL:
FLUSH:
UNLOCK.ALL:
FLUSH.CACHE:

Status = 1
RETURN

/* Not available to MFS */
	
LOCK.SEMAPHORE:
UNLOCK.SEMAPHORE:
SET.USER.SEMAPHORE:

RETURN

/* Directory level calls */

CREATE.MEDIA:
OPEN.MEDIA:
CLOSE.MEDIA:
READ.MEDIA:
WRITE.MEDIA:
GROUP.NUMBER:
RECORD.COUNT:
CREATE.FILE:
RENAME.FILE:
MOVE.FILE:
DELETE.FILE:

Goto NEXT.FS

OPEN.FILE:
	
	* Call BFS in order to get file handle
	
	FS = DELETE(BFS,1,1,1)
	NEXTFS = FS<1,1,1>
	CALL @NEXTFS(Code, FS, Handle, Name, FMC, Record, Status)
	
	* Load handle and file name into labelled common
	
	IF Status THEN
		LOCATE Name IN Files_Array USING @FM SETTING POS THEN
			Handles_Array<POS> = Record
		END ELSE
			Files_Array<-1> = Name
			Handles_Array<-1> = Record
		END
	END
	RETURN

CLEARFILE:
REMAKE.FILE:
SELECT:
READNEXT:
CLEARSELECT:

READ.RECORD:
READO.RECORD:
	
	GOTO NEXT.FS
	
* * * * * * *
WRITE.RECORD:
* * * * * * *

	* Get original record and compare with RECORD being written
	
	LOCATE Handle IN Handles_Array USING @FM SETTING POS ELSE
		mesg = "Missing file handle in SAP_MFS!|"
		mesg := handle
		MSG('',mesg)
		GOTO NEXT.FS
	END
	
	FileName = Files_Array<POS>[1,'*']		;* Extract corresponding file name
		
	OrgRecord	= ''
	NextFS 		= BFS<1,1,2>
	
	CALL @NextFS(ReadRec$, FS, Handle, Name, FMC, OrgRecord, Status)  ;* Read old record
	
	RecKey = Name

* 	If InList(RetStack(), 'SCHED_DET_ACTIONS', @FM) else
    	SAP_Monitor('Write',FileName:@RM:RecKey:@RM:Record:@RM:OrgRecord)
* 	end
		
	GOTO NEXT.FS
	
	
* * * * * * *
DELETE.RECORD:
* * * * * * *

	LOCATE Handle IN Handles_Array USING @FM SETTING POS ELSE
		mesg = "Missing file handle in SAP_MFS!|"
		mesg := handle
		MSG('',mesg)
		GOTO NEXT.FS
	END
	
	FileName = Files_Array<POS>[1,'*']		;* Extract corresponding file name
	
	OrgRecord	= ''
	NextFS 		= BFS<1,1,2>
	
	CALL @NextFS(ReadRec$, FS, Handle, Name, FMC, OrgRecord, Status)  ;* Read old record
	
	RecKey = Name
	
* 	If InList(RetStack(), 'SCHED_DET_ACTIONS', @FM) else
	    SAP_Monitor('Delete',FileName:@RM:RecKey:@RM:Record:@RM:OrgRecord)
* 	end
	
	GOTO NEXT.FS

/*   Unused calls  */
LOCK.RECORD:
UNLOCK.RECORD:
RESERVED:
OMNI.SCRIPT:

CREATE.INDEX:
DELETE.INDEX:
SELECT.INDEX:
UPDATE.INDEX:
READNEXT.INDEX:

NEXT.FS:

	FS = DELETE(BFS,1,1,1)
	NEXTFS = FS<1,1,1>
	CALL @NEXTFS(Code, FS, Handle, Name, FMC, Record, Status)
	RETURN
