package de.saumya.mojo.gem;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.FileUtils;

import de.saumya.mojo.ruby.gems.GemException;
import de.saumya.mojo.ruby.script.Script;
import de.saumya.mojo.ruby.script.ScriptException;

/**
 * installs a set of given gems without resolving any transitive dependencies
 * 
 * @goal process-resources
 * @phase process-resources
 */
public class ProcessResourcesMojo extends AbstractGemMojo {

    /** @parameter  */
    protected List<String> includeRubyResources;

    /** @parameter  */
    protected List<String> excludeRubyResources = Collections.emptyList();
    
    @Override
    protected void executeWithGems() throws MojoExecutionException,
            ScriptException, IOException, GemException {
        File jrubydir = new File(project.getBuild().getOutputDirectory(), ".jrubydir");
        if ( includeRubyResources != null ) {
            jrubydir.delete();
            DirectoryScanner scanner = scan(includeRubyResources.toArray(new String[includeRubyResources.size()]),
                    excludeRubyResources.toArray(new String[excludeRubyResources.size()])      );
            processBaseDirectory(scanner, jrubydir);
            processNestedDiretories(scanner);
        }
        if ( rubySourceDirectory.exists() ) {
            File[] dirs = rubySourceDirectory.listFiles(new FileFilter() {
                
                public boolean accept(File f) {
                    return f.isDirectory();
                }
            });
            String[] includes = new String[ dirs.length + 1 ];
            includes[ 0 ] =  "*";
            int index = 1;
            for( File dir: dirs ) {
                includes[ index ++ ] = dir.getName() + "/*";
            }
            DirectoryScanner scanner = scan(includes, new String[0]);
           
            processBaseDirectory(scanner, jrubydir);
            processNestedDiretories(scanner);            
        }
    }

    private void processNestedDiretories(DirectoryScanner scanner) throws IOException, ScriptException {
        String[] directories = scanner.getIncludedDirectories();   
        if (directories.length > 0) {
            StringBuilder script = new StringBuilder("require 'jruby/commands';");
            for( String dir: directories) {
                if (!dir.contains("/")) {   
                    script.append("JRuby::Commands.generate_dir_info('" +
                           new File( project.getBuild().getOutputDirectory(), dir ).getAbsolutePath() + "', false);");
                }
            }
            Script s = this.factory.newScript(script.toString());
            s.execute();
        }
    }

    private void processBaseDirectory(DirectoryScanner scanner, File jrubydir)
            throws IOException {
        String[] files = scanner.getIncludedFiles();
        if (files.length > 0) {
            StringBuilder fileList;
            if (jrubydir.exists()) {
                fileList = new StringBuilder(FileUtils.fileRead(jrubydir));
            }
            else {
                fileList = new StringBuilder(".\n");
            }
            for (String file: files) {
                if (!file.contains("/")) {
                    fileList.append(file).append("\n");
                }
            }
            FileUtils.fileWrite(jrubydir, fileList.toString());
        }
    }

    private DirectoryScanner scan(String[] includes, String[] excludes) {
        DirectoryScanner scanner = new DirectoryScanner();

        scanner.setBasedir(project.getBuild().getOutputDirectory());
        scanner.addDefaultExcludes();
        scanner.setIncludes(includes);
        scanner.setExcludes(excludes);
        scanner.scan();
        return scanner;
    }
}

@@ -1511,41 +1511,351 @@
     
-File%5B%5D dirs = rubySourceDirectory
+processDir(jrubydir, rubySourceDirectory);            %0A        %7D%0A        if ( libDirectory.exists() && includeLibDirectoryInResources ) %7B%0A            processDir(jrubydir, libDirectory);            %0A        %7D%0A    %7D%0A%0A    private void processDir(File jrubydir, File dir) throws IOException,%0A            ScriptException %7B%0A        File%5B%5D dirs = dir
 .lis
@@ -1888,33 +1888,25 @@
             
-    %0A    
+%0A
             
@@ -1949,28 +1949,24 @@
             
-    
 return f.isD
@@ -1985,34 +1985,26 @@
             
-    %7D%0A    
+%7D%0A
         %7D);%0A
@@ -2007,28 +2007,24 @@
 %7D);%0A        
- 
-   
 String%5B%5D inc
@@ -2062,36 +2062,32 @@
  + 1 %5D;%0A        
-    
 includes%5B 0 %5D = 
@@ -2100,20 +2100,16 @@
         
-    
 int inde
@@ -2119,28 +2119,24 @@
  1;%0A        
- 
-   
 for( File di
@@ -2134,18 +2134,16 @@
 ( File d
-ir
 : dirs )
@@ -2153,28 +2153,24 @@
             
-  
-  
 includes%5B in
@@ -2181,18 +2181,16 @@
 ++ %5D = d
-ir
 .getName
@@ -2204,34 +2204,26 @@
 *%22;%0A        
-  
-  %7D%0A    
+%7D%0A
         Dire
@@ -2277,39 +2277,33 @@
 g%5B0%5D);%0A         
-  %0A    
+%0A
         processB
@@ -2335,36 +2335,32 @@
 bydir);%0A        
- 
-   
 processNestedDir
@@ -2376,38 +2376,16 @@
 canner);
-            %0A        %7D
 %0A    %7D%0A%0A

add jrubydir to src/main/ruby and lib as well