/*
 * SPDXVersion: SPDX-1.1
 * Creator: Person: Nuno Brito (nuno.brito@triplecheck.de)
 * Creator: Organization: TripleCheck (contact@triplecheck.de)
 * Created: 2013-09-19T00:00:00Z
 * LicenseName: AGPL-3.0+
 * FileName: RunPlugins.java  
 * FileType: SOURCE
 * FileCopyrightText: <text> Copyright 2013 Nuno Brito, TripleCheck </text>
 * FileComment: <text> Methods to handle RunPlugins files and to integrate them
 *  with the user interface. </text> 
 */

package script;

import main.script.log;
import GUI.ListItem;
import definitions.Messages;
import definitions.is;
import java.io.File;
import java.util.ArrayList;
import main.engine;


/**
 *
 * @author Nuno Brito, 19th of September 2013 in Darmstadt, Germany.
 */
public class RunPlugins {

    public static ArrayList<ListItem> menu = new ArrayList();
    
    /**
     * Find all RunPlugins within our plugin folder
     * @param what A piece of text that has to be present in the file name
     * @return a list of plugin files
     */
    public static ArrayList<File> findPluginFiles(String what){
        File folder = engine.getPluginsFolder();
        // pre-flight check
        if(folder.exists() == false){
            // folder doesn't exist, let's create a folder just in case
            utils.files.mkdirs(folder);
            return null;
        }
        // find all RunPlugins available
        ArrayList<File> foundFiles = utils.files.findFiles(folder);
        // filter the ones that we don't want
        ArrayList<File> files = new ArrayList();
        for(File file : foundFiles){
            String filename = file.getAbsolutePath();
            // svn folder are a known problem
            if(filename.contains(".svn")){
                continue;
            }
            // we only want beanshell files
            if(filename.endsWith(what) == false){
                continue;
            }
            // all is good now, add up this file
            files.add(file);
        }
        
        //core.temp.put("pluginFiles", files);
        return files;
    }
    
  
    
  /**
     * Adds the RunPlugins 
     */
    public static void listPlugins(){
        // remove all previously installed plugins
        log.hooks.removeAll();
        // get our available plugin files
        ArrayList<File> pluginFiles = findPluginFiles(".bsh");
        for(File file : pluginFiles){
           processPlugin(file);
        }

        // process the java-like RunPlugins
        ArrayList<File> pluginJava = findPluginFiles(".java");
        for(File file : pluginJava){
            engine.script.runJava(file, is.methodStartUp, is.plugin);
            log.write(is.INFO, Messages.AddedPlugin, file.getName());
        }
    }
    
    

    
    /**
     * Process each script with instructions
     */
    static void processPlugin(File file){
         engine.script.run(file, "startup", "Plugin");
         log.write(is.INFO, Messages.AddedPlugin, file.getName());
    }    
   
   
}

@@ -2295,21 +2295,19 @@
 %3E plugin
-Files
+Bsh
  = findP
@@ -2322,24 +2322,61 @@
 es(%22.bsh%22);%0A
+        if (pluginBsh != null) %7B%0A    
         for(
@@ -2366,32 +2366,33 @@
 %0A            for
+ 
 (File file : plu
@@ -2394,24 +2394,28 @@
 : plugin
-Files)%7B%0A
+Bsh) %7B%0A     
         
@@ -2430,32 +2430,46 @@
 ssPlugin(file);%0A
+            %7D%0A
         %7D%0A%0A     
@@ -2566,24 +2566,62 @@
 s(%22.java%22);%0A
+        if (pluginJava != null) %7B%0A    
         for(
@@ -2611,32 +2611,33 @@
 %0A            for
+ 
 (File file : plu
@@ -2644,18 +2644,23 @@
 ginJava)
+ 
 %7B%0A
+    
         
@@ -2725,32 +2725,36 @@
 n);%0A            
+  
+  
 log.write(is.INF
@@ -2791,24 +2791,38 @@
 getName());%0A
+            %7D%0A
         %7D%0A  

Fix NPEs if there are no plugins