subroutine mods_mfs(Code, BFS, Handle, Name, FMC, Record, Status)

$insert file.system.equates
$insert fserrors_hdr
$insert fserrors_100
$insert logical
$insert mods_common

declare function msg
declare subroutine record_lock

equ DictIdsNotToSave$ to '%PROTECT.SPEC%':@fm:'%RECORDS%':@fm:'%PROTECT%':@fm:'%SK%':@fm:'%FIELDS%'

FSBranch = 'ReadRecord/ReadORecord/WriteRecord/DeleteRecord/LockRecord/UnlockRecord/Select/'
FSBranch:= 'Readnext/ClearSelect/ClearTable/OpenTable/CreateTable/RenameTable/MoveTable/'
FSBranch:= 'DeleteTable/OpenMedia/CreateMedia/ReadMedia/WriteMedia/UnlockAll/Flush/Install/'
FSBranch:= 'Reserved/Reserved/Reserved/OmniScript/CloseMedia/RecordCount/RemakeTable/'
FSBranch:= 'CreateIndex/DeleteIndex/UpdateIndex/SelectIndex/ReadnextIndex'
convert '/' to @fm in FSBranch

FS = delete(BFS,1,1,1)
NextFS = FS<1,1,1>
@file.error = ''

on Code gosub ReadRecord, ReadORecord, WriteRecord, DeleteRecord, LockRecord, UnlockRecord, Select, Readnext, ClearSelect, ClearTable, OpenTable, CreateTable, RenameTable, MoveTable, DeleteTable, OpenMedia, CreateMedia, ReadMedia, WriteMedia, UnlockAll, Flush, Install, Reserved, Reserved, Reserved, OmniScript, CloseMedia, RecordCount, RemakeTable, CreateIndex, DeleteIndex, UpdateIndex, SelectIndex, ReadnextIndex


return

*============================ OPEN TABLE =========================================*
OpenTable:
   if index( Name, 'TEMP_', 1 ) then
      * REMOVE THE OLD TEMP_ REFERENCE CAUSE MOST LIKELY IT WILL BE A DIFFERENT
      * PHYSICAL TABLE THE NEXT TIME AROUND
      * THIS IS DUE TO RTI CREATING A TEMP_TABLE WHEN EDITING A DICTIONARY
      * WHY THEY DO THIS IS BEYOND ME
      locate Name in TableName@ using @fm setting Dpos then
         TableName@ = delete( TableName@, Dpos, 0, 0 )
         Handle@    = delete( Handle@, Dpos, 0, 0 )
      end
   end
   FoundInEither = 0
   locate Name in TableName@ using @fm setting Fpos then
      FoundInEither = 1
   end
   locate Handle in Handle@ using @fm setting FFpos then
      FoundInEither = 1
   end
   if FoundInEither else
      TableName@<-1> = Name
   end
   gosub NextFS
   if FoundInEither else
      Handle@<-1>    = Record
   end
return   
*============================ WRITE RECORD ======================================*
WriteRecord:
   gosub NextFS
   ModAction = 'WR'
   gosub WriteMod
return
*============================ DELETE RECORD =====================================*
DeleteRecord:
   gosub NextFS
   ModAction = 'DR'
   gosub WriteMod
return
*================================================================================*
Readnext:
ReadnextIndex:
ReadORecord:
ReadRecord:
CreateMedia:
OpenMedia:
ReadMedia:
WriteMedia:
CloseMedia:
ClearTable:
CreateTable:
DeleteTable:
MoveTable:
RemakeTable:
RenameTable:
Select:
ClearSelect:
LockRecord:
UnlockRecord:
UpdateIndex:
SelectIndex:
CreateIndex:
DeleteIndex:
Reserved:
OmniScript:
RecordCount:
NextFS:
call @nextfs(Code, FS, Handle, Name, FMC, Record, Status)
return
*============================================================================
/*
Install, unlock all and flush are called directly, no need to call next FS.
*/
Install:
Flush:
UnlockAll:

Status = TRUE$

return
*============================================================================*
WriteMod:
   locate Handle in Handle@ using @fm setting Fpos then
      TableUsed = TableName@<Fpos>
   end else
      TableUsed = 'ERROR'
   end
   swap 'TEMP_' with '' in TableUsed ;* to remove the temporary dictionary items          
   ModKey = ''
   locate Name in DictIdsNotToSave$ using @fm setting DPos then
       * do not save this item
   end else
	   open 'MODS' to ModsTable else
	      Void = msg( '', 'Unable to open MODS...' )
	      return 0
	   end
       if ModAction = 'DR' then
		   ModKey = @appid<1>:'`':field( TableUsed, '*', 1 ):'`':Name:'`':date():'`':time():'`':ModAction
		   record_lock( ModsTable, 'MODS', ModKey )		   
		   write '' on ModsTable, ModKey else ;* write null cause it is a deletion
		      Void = msg( '', 'Unable to write ':ModKey:' on MODS...' )
		      return 0
		   end
       end else 
		   ModKey = @appid<1>:'`':field( TableUsed, '*', 1 ):'`':Name:'`':date():'`':time():'`':ModAction
		   record_lock( ModsTable, 'MODS', ModKey )
		   write Record on ModsTable, ModKey else
		      Void = msg( '', 'Unable to write ':ModKey:' on MODS...' )
		      return 0
		   end
	   end
	   unlock ModsTable, ModKey else
	      Void = msg( '', 'Unable to unlock ':ModKey:' in the MODS table...' )
	      return 0
	   end
   end
*
return
*============================================================================*
