compile insert SyncDB_Insert

******************************************************************************
*
* This program is proprietary and is not to be used by or disclosed
* to others, nor is it to be copied without written permission from
* Revelation Technologies, Inc.
*
* Name       :  SyncDB_Insert
* Description:  Ensure that the @tables/@volumes arrays are synchronized.
*               Requires System Compiler with SysCommon already defined.
*
* History    :  (date, initials, notes)
*  03/09/98  cp   Original programmer, based on the original SyncDB utility.
*  08/04/10  rjc  Add cleanup of empty volume references
******************************************************************************

point Vols     to sc_Array_Volumes(0)
point VolFiles to sc_Array_Volumes(3)
point Files    to sc_Array_Tables(0)
point FileVols to sc_Array_Tables(1)

* first verify volume structures
cVols = count(Vols, @fm) + (Vols # "")
for iVol = 1 to cVols
  Vol = Vols<iVol>
  cFiles = count(VolFiles<iVol>, @vm) + (VolFiles<iVol> # "")
  for iFile = 1 to cFiles
    File = VolFiles<iVol,iFile>
    locate File in Files using @fm setting Pos then
     * check volume reference
      if FileVols<Pos> # Vol then
        VolFiles = delete(VolFiles, iVol, iFile, 0)
      end
    end else
     * file does not exist but is referenced by volume
      VolFiles = delete(VolFiles, iVol, iFile, 0)
    end
  next iFiles
next iVol

cFiles = count(Files, @fm) + (Files # "")
for iFile = cFiles to 1 step -1
  File = Files<iFile>
  Vol  = FileVols<iFile>
 * check volume reference
  locate Vol in Vols using @fm setting Pos then
    locate File in VolFiles<Pos> using @vm setting Pos else
     * file not referenced by a volume
      for i = 0 to 5
        sc_Array_Tables(i) = delete(sc_Array_Tables(i), iFile, 0, 0)
      next i
    end
  end else
   * file references unattached volume
    for i = 0 to 5
      sc_Array_Tables(i) = delete(sc_Array_Tables(i), iFile, 0, 0)
    next i
  end
next iFile


* rjc  08-04-10  Clean out empty volumes. 
cVols = count(Vols, @fm) + (Vols # "")
for iVol = cVols To 1 step -1
  Vol = Vols<iVol>
  cFiles = count(VolFiles<iVol>, @vm) + (VolFiles<iVol> # "")
  Begin Case
    Case vol _eqc 'SYSCOLUMNS*SYSCOLUMNS'
	    * SYSCOLUMNS is special case, do nothing
	Case cfiles = 0
	    * Empty vol, remove it
      for i = 0 To 6
	       sc_Array_Volumes(i) = delete(sc_Array_Volumes(i), iVol, 0, 0)
	  next
  End case
next iVol
