# This script assumes the following beans are in place
# $directories - a list of directories containing the plugins
# $plugins - java plugin types

#include Java

import com.approximatrix.draco.application.plugin.RegressionPlugin;
import com.approximatrix.draco.application.plugin.AnalysisPlugin;
import com.approximatrix.draco.application.plugin.PluginType

import java.lang.System

require '/pengine/model/plugin'

# Prepare the system by loading preferences
load '/pengine/initialize.rb'

$current_filename = nil

def register(klass)
  System.out.print("Registering: ")
  System.out.println(klass.name)
  
  toregister = nil
  
  if klass.type == PluginType.analysis
    toregister = AnalysisPlugin.new(klass.name,$current_filename,klass.to_s)
  elsif klass.type == PluginType.regression
    toregister = RegressionPlugin.new(klass.name,$current_filename,klass.to_s)
  else
    System.err.print("Failed to determine plugin type for class: ")
    System.err.print(klass.name)
    System.err.print("  file: ")
    System.err.println($current_filename)
  end
  
  
  if(not toregister.nil?)
    already_registered = false
    $registry.each do |x|
      if(x.getName == toregister.getName)
        already_registered = true
        break
      end
    end
    
    $registry << toregister unless already_registered
    
  end 
  
end

def register_files(directory)

    search = File.join(directory,'*.rb')
    
    #System.out.println(search)
    
    Dir.glob(search) { |filename|
    
      #System.out.println(filename)
      
      $current_filename = filename
      #begin
        load filename
      #rescue
      #  System.err.print("Failed on file: ")
      #  System.err.println(filename)
      #end
      
    }
    
end

$directories.each { |directory|
    register_files(directory)
}
