package org.jvending.masa.plugin.proguard;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.jvending.masa.CommandExecutor;
import org.jvending.masa.ExecutionException;
import org.jvending.masa.MasaUtil;

/**
 *
 * @goal proguard
 * 
 */
public class ProguardMojo
    extends AbstractMojo
{

    /**
     * The maven project.
     * 
     * @parameter expression="${project}"
     */
    public MavenProject project;

    /**
     * @parameter expression="${session}"
     */
    public MavenSession session;
    

    /**
     * @parameter default-value="${project.build.directory}/${project.build.finalName}-small.jar";
     */
    private File outjars;
    
    /**
     * @parameter default-value="${project.basedir}/proguard.cfg";
     */
    private File configFile;

    public void execute()
        throws MojoExecutionException
    {
        File inputFile =
            new File( project.getBuild().getDirectory() + File.separator + project.getBuild().getFinalName() + ".jar" );
      
    	File proFile =   MasaUtil.getProguardJarFile(project);
    	
		if(proFile == null) {
			getLog().info("Proguard not configured for this build");
			return;
		}
		
		File reportDirectory = new File(project.getBasedir(), "proguard");
		if(!reportDirectory.exists()) {
			reportDirectory.mkdirs();
		}
		
		CommandExecutor executor = CommandExecutor.Factory
				.createDefaultCommmandExecutor();
		executor.setLogger(this.getLog());
		
		List<String> commands = new ArrayList<String>();
		
		//Run proguard jar
		commands.add("-jar");
		commands.add(proFile.getAbsolutePath());

		commands.add("-injars");
		commands.add(inputFile.getAbsolutePath());
		
		commands.add("-outjars");
		commands.add(outjars.getAbsolutePath());

		commands.add("-include");
		commands.add(configFile.getAbsolutePath());
		
		//Reporting		
		commands.add("-printseeds");
		commands.add(new File(reportDirectory, "seeds.txt").getAbsolutePath());

		commands.add("-printmapping");
		commands.add(new File(reportDirectory, "mapping.txt").getAbsolutePath());
		
		commands.add("-printusage");
		commands.add(new File(reportDirectory, "usage.txt").getAbsolutePath());
		
		commands.add("-dump");
		commands.add(new File(reportDirectory, "dump.txt").getAbsolutePath());
		
	    for ( Artifact artifact : (Set<Artifact>) project.getDependencyArtifacts())
	    {
	    	commands.add("-libraryjars");
	    	commands.add(artifact.getFile().getAbsolutePath());
	    }
		
		try {
			executor.executeCommand("java", commands, project.getBasedir(),
					false);
		} catch (ExecutionException e) {
			throw new MojoExecutionException("", e);
		}

		
	//	mavenProjectHelper.attachArtifact(project, "jar", project.getArtifact()
	//			.getClassifier(), outjars);

    }
}

@@ -1111,16 +1111,81 @@
 figFile;
+%0A    %0A    /**%0A     * @parameter%0A     */%0A    private boolean skip;
 %0A%0A    pu
@@ -1248,16 +1248,262 @@
 n%0A    %7B%0A
+    %09if(skip) %7B%0A    %09%09getLog().info(%22Plugin configured to skip proguard for this build%22);%0A    %09%09return;%0A    %09%7D%0A    %09%0A    %09if(isSkip()) %7B%0A    %09%09getLog().info(%22Property configured to skip proguard for this build%22);%0A    %09%09return;    %09%09%0A    %09%7D%0A    %09%0A
         
@@ -3262,123 +3262,106 @@
 %09%09%7D%0A
-%0A%09%09%0A%09//%09mavenProjectHelper.attachArtifact(project, %22jar%22, project.getArtifact()%0A%09//%09%09%09.getClassifier(), outjars
+    %7D%0A    %0A    private static boolean isSkip() %7B%0A    %09return Boolean.getBoolean(%22proguard.skip%22
 );%0A
-%0A
     

Disable options for Proguard