/*
 * Copyright 2012 Red Hat, Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.commonjava.maven.plugin.unsigner;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.Mojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.apache.maven.project.MavenProject;

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

/**
 * @goal unsign
 * @phase package
 */
public class UnsignGoal
    implements Mojo
{

    private Log log;

    /**
     * @parameter default-value="${project}"
     * @readonly
     */
    private MavenProject project;

    /**
     * @parameter default-value="false" expression="${unsigner.skip}"
     */
    private boolean skip;

    /**
     * @parameter default-value="false" expression="${unsigner.processAttachments}"
     */
    private boolean skipAttachments;

    /**
     * {@inheritDoc}
     * @see org.apache.maven.plugin.Mojo#execute()
     */
    public void execute()
        throws MojoExecutionException, MojoFailureException
    {
        if ( skip )
        {
            getLog().info( "Skipping unsigner per configuration." );
            return;
        }

        final Unsigner unsigner = new Unsigner();

        int unsigned = 0;
        if ( "jar".equals( project.getPackaging() ) )
        {
            final Artifact artifact = project.getArtifact();
            final File src = artifact.getFile();
            unsigner.unsign( src );
            unsigned++;
        }

        final List<Artifact> attached = project.getAttachedArtifacts();
        if ( !skipAttachments && attached != null )
        {
            for ( final Artifact attachment : attached )
            {
                if ( "jar".equals( attachment.getType() ) )
                {
                    final File src = attachment.getFile();
                    unsigner.unsign( src );
                    unsigned++;
                }
            }
        }

        getLog().info( "Unsigned " + unsigned + " jars." );
    }

    /**
     * {@inheritDoc}
     * @see org.apache.maven.plugin.Mojo#setLog(org.apache.maven.plugin.logging.Log)
     */
    public void setLog( final Log log )
    {
        this.log = log;
    }

    /**
     * {@inheritDoc}
     * @see org.apache.maven.plugin.Mojo#getLog()
     */
    public synchronized Log getLog()
    {
        if ( log == null )
        {
            log = new SystemStreamLog();
        }

        return log;
    }
}

@@ -639,16 +639,61 @@
 igner;%0A%0A
+import java.io.File;%0Aimport java.util.List;%0A%0A
 import o
@@ -1023,53 +1023,8 @@
 t;%0A%0A
-import java.io.File;%0Aimport java.util.List;%0A%0A
 /**%0A
@@ -1515,32 +1515,40 @@
 * %7B@inheritDoc%7D%0A
+     * %0A
      * @see org.
@@ -1906,44 +1906,445 @@
 f ( 
-%22jar%22.equals( project.getPackaging()
+project.getArtifact() != null && project.getArtifact()%0A                                                     .getFile() != null && project.getArtifact()%0A                                                                                  .getFile()%0A                                                                                  .getName()%0A                                                                                  .endsWith( %22.jar%22
  ) )
@@ -2376,25 +2376,16 @@
 nal 
-Artifact artifact
+File src
  = p
@@ -2404,17 +2404,16 @@
 tifact()
-;
 %0A       
@@ -2421,45 +2421,187 @@
     
-final File src = artifact.getFile();%0A
+                        .getFile();%0A            if ( src.exists() && !src.isDirectory() )%0A            %7B%0A                getLog().info( %22Unsigning project artifact: %22 + src );%0A    
     
@@ -2636,32 +2636,36 @@
  );%0A            
+    
 unsigned++;%0A    
@@ -2652,32 +2652,46 @@
     unsigned++;%0A
+            %7D%0A
         %7D%0A%0A     
@@ -2915,124 +2915,464 @@
 f ( 
-%22jar%22.equals( attachment.getType() ) )%0A                %7B%0A                    final File src = attachment.getFile();%0A
+attachment.getFile() != null && attachment.getFile()%0A                                                               .getName()%0A                                                               .endsWith( %22.jar%22 ) )%0A                %7B%0A                    final File src = attachment.getFile();%0A                    if ( src.exists() && !src.isDirectory() )%0A                    %7B%0A                        getLog().info( %22Unsigning project artifact: %22 + src );%0A    
     
@@ -3423,32 +3423,36 @@
                 
+    
 unsigned++;%0A    
@@ -3439,32 +3439,54 @@
     unsigned++;%0A
+                    %7D%0A
                 
@@ -3600,32 +3600,40 @@
 * %7B@inheritDoc%7D%0A
+     * %0A
      * @see org.
@@ -3811,24 +3811,32 @@
 inheritDoc%7D%0A
+     * %0A
      * @see 

improve handling for packaging != jar, and provide a little more logging about what the plugin is doing