//setup
compile getdirs.prg
set procedure to
set procedure to getdirs.prg
clear

//log results
set alternate to compileall.log
set alternate on

?"About to compile all of the following files in this and all sub directories"
?" "
?"CC - WFM - REP - PRG - MNU - DMD - CFM - POP - CRP - LAB - CDM"
?" "
?" "
?" "
//get list of directories to iterate through
cRootDir = SET("DIRECTORY")
Private cSetDirBack
cSetDirBack = 'SET DIRECTORY TO "'+cRootDir+'"'

//first compile all files on base directory
LoopThroughFileTypes(cRootDir)

//create list of all subdirectories and compile all files on those
aSubDirs = GetDirs(cRootDir)
if aSubDirs.size > 0
   for nItem = 1 to aSubDirs.size
      //?aSubDirs[nItem]
      LoopThroughFileTypes(cRootDir+aSubDirs[nItem])
   endfor
endif

//clean up
&cSetDirBack.
release nObjs
set alternate to
set alternate off

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function compileObjects (cDir,aFiles, cFileType)
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Private cCompile, SetCDir

  SetCDir = 'SET DIRECTORY TO "'+cDir+'"'
  &SetCDir.
  
  nObjs = 0
  if aFiles.size>0
      nObjs = 0
      aErrors = new Array()
      for x = 1 to aFiles.size/5
         if lower(aFiles[x,1])<>"compileall.prg" and ;
            lower(aFiles[x,1])<>"getdirs.prg" and ;
            NOT "backup" $ lower(aFiles[x,1]) 

            cCompile = "Compile '"+cDir+"\"+aFiles[x,1]+"'"
            try
               &cCompile.
               nObjs++
               ?aFiles[x,1]+REPLICATE(".",35-LEN(aFiles[x,1]))+"COMPILED OK"	  
            catch (exception e)
               aErrors.add(aFiles[x,1]+REPLICATE(" ",25-LEN(aFiles[x,1]))+e.message)
            endtry
         endif
      endfor
      ?iif(nObjs>0,iif(nObjs=1,nObjs+" "+cFileType+" COMPILED",nObjs+" "+cFileType+"s COMPILED"),"")
      if nObjs>0
         ?" "
   endif
   if aErrors.size>0
      ?iif(aErrors.size=1,"EXCEPTION:","EXCEPTIONS:")
      for nError = 1 to aErrors.size
         ?aErrors[nError]
      endfor
      ?" "
   endif  
   
  endif
return

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function loopThroughFileTypes (cDir)
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   ?REPLICATE("=",LEN("DIRECTORY: "+cDir))
   ?"DIRECTORY: "+cDir
   ?REPLICATE("-",LEN("DIRECTORY: "+cDir))

   lNoFiles=true

   a = new array()
   a.dir(cDir+"\*.cc")
   if a.size>0
      lNoFiles=false
      compileObjects(cDir,a,"CC")
   endif                            

   a = new array()
   a.dir(cDir+"\*.wfm")
   if a.size>0
      lNoFiles=false
      compileObjects(cDir,a,"WFM")
   endif

   a = new array()
   a.dir(cDir+"\*.rep")
   if a.size>0
      lNoFiles=false
      compileObjects(cDir,a,"REP")
   endif

   a = new array()
   a.dir(cDir+"\*.prg")
   if a.size>0
      lNoFiles=false
      compileObjects(cDir,a,"PRG")
   endif

   a = new array()
   a.dir(cDir+"\*.mnu")
   if a.size>0
      lNoFiles=false
      compileObjects(cDir,a,"MNU")      
   endif

   a = new array()
   a.dir(cDir+"\*.dmd")
   if a.size>0
      lNoFiles=false
      compileObjects(cDir,a,"DMD")
   endif

   a = new array()
   a.dir(cDir+"\*.cfm")
   if a.size>0
      lNoFiles=false
      compileObjects(cDir,a,"CFM")
   endif

   a = new array()
   a.dir(cDir+"\*.pop")
   if a.size>0
      lNoFiles=false
      compileObjects(cDir,a,"POP")
   endif

   a = new array()
   a.dir(cDir+"\*.crp")
   if a.size>0
      lNoFiles=false
      compileObjects(cDir,a,"CRP")
   endif

   a = new array()
   a.dir(cDir+"\*.lab")
   if a.size>0
      lNoFiles=false
      compileObjects(cDir,a,"LAB")
   endif

   a = new array()
   a.dir(cDir+"\*.cdm")
   if a.size>0
      lNoFiles=false
      compileObjects(cDir,a,"CDM")
   endif

   if lNoFiles
      ?"no Compilable files found"
   endif

   ?REPLICATE("=",LEN("DIRECTORY: "+cDir))
   ?" "
   ?" "
   ?" "
   ?" "

return
