package org.ops4j.pax.bnd.maven;

import static org.ops4j.pax.bnd.maven.Utils.getWorkspace;
import static org.ops4j.pax.bnd.maven.Utils.printInfo;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;

import aQute.bnd.build.Project;
import aQute.bnd.build.Workspace;

@Mojo(name = "bundle")
public class BundleMojo extends AbstractMojo {

    @Component
    protected MavenSession session;

    @Parameter(property = "supportedProjectTypes")
    protected List<String> supportedProjectTypes = Arrays.asList(new String[] { "jar", "bundle" });

    @Component
    protected MavenProject project;

    @Component
    private MavenProjectHelper m_projectHelper;

    @Component
    private ArtifactHandlerManager m_artifactHandlerManager;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        File[] files = null;

        try {
            Workspace workspace = printInfo("Building using BND Build..", getLog(), getWorkspace(new File(session.getExecutionRootDirectory())));
            Project workspaceProject = workspace.getProject(project.getArtifactId());
            if (project == null)
                throw new MojoExecutionException("Something is broken with your workspace. Cannot find " + project.getArtifactId() + "(from pom.xml) in BND Workspace.");

            files = workspaceProject.build();
            Utils.printInfo("Build done: " + workspaceProject.getName(), getLog(), workspace);
            workspace.close();
        } catch (Exception e) {
            throw new MojoExecutionException("Problem building the project.", e);
        }

        // attach bundle to maven project
        File jarFile = files[0];

        Artifact mainArtifact = project.getArtifact();

        if ("bundle".equals(mainArtifact.getType())) {
            // workaround for MNG-1682: force maven to install artifact using
            // the "jar" handler
            mainArtifact.setArtifactHandler(m_artifactHandlerManager.getArtifactHandler("jar"));
        }

        mainArtifact.setFile(jarFile);

    }

}

@@ -1649,259 +1649,292 @@
 t = 
-workspace.getProject(project.getArtifactId());%0A            if (project == null)%0A                throw new MojoExecutionException(%22Something is broken with your workspace. Cannot find %22 + project.getArtifactId() + %22(from pom.xml) in BND Workspace.%22
+retrieveProject(workspace);%0A            getLog().info(%22Setting Maven Artifact Version to: %22 + workspaceProject.getBundleVersion());%0A            project.setVersion(workspaceProject.getBundleVersion());%0A            project.getArtifact().setVersion(workspaceProject.getBundleVersion()
 );%0A
-%0A
     
@@ -1949,16 +1949,29 @@
 files = 
+buildProject(
 workspac
@@ -1982,15 +1982,8 @@
 ject
-.build(
 );%0A 
@@ -2224,32 +2224,176 @@
  e);%0A        %7D%0A%0A
+        if (files.length != 1) %7B%0A            throw new MojoExecutionException(%22Ambiguous output from BND to attach as maven result%22);%0A        %7D%0A
         // attac
@@ -2773,33 +2773,32 @@
 r%22));%0A        %7D%0A
-%0A
         mainArti
@@ -2820,16 +2820,847 @@
 rFile);%0A
+    %7D%0A%0A    private Project retrieveProject(Workspace workspace) throws Exception, MojoExecutionException %7B%0A        Project workspaceProject = workspace.getProject(project.getArtifactId());%0A        if (project == null)%0A            throw new MojoExecutionException(%22Something is broken with your workspace. Cannot find %22 + project.getArtifactId() + %22(from pom.xml) in BND Workspace.%22);%0A        return workspaceProject;%0A    %7D%0A%0A    private File%5B%5D buildProject(Project workspaceProject) throws Exception %7B%0A        File%5B%5D files = workspaceProject.build();%0A        List%3CString%3E errors = workspaceProject.getErrors();%0A        for (String error : errors) %7B%0A            getLog().error(error);%0A        %7D%0A        if (errors.size() %3E 0) %7B%0A            throw new MojoExecutionException(%22There are build errors!.%22);%0A        %7D%0A        return files;
 %0A    %7D%0A%0A

set maven version from bnd workspace bundleversion