/*
 * Copyright 2005 The Apache Software Foundation.
 * 
 * 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.vafer.jdeb.maven;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProjectHelper;
import org.apache.tools.tar.TarEntry;
import org.vafer.jdeb.Console;
import org.vafer.jdeb.DataConsumer;
import org.vafer.jdeb.DataProducer;
import org.vafer.jdeb.Processor;
import org.vafer.jdeb.changes.TextfileChangesProvider;
import org.vafer.jdeb.descriptors.PackageDescriptor;

/**
 * Creates deb archive
 *
 * @goal deb
 * @requiresDependencyResolution compile
 * @execute phase="package"
 */
public final class DebMojo extends AbstractPluginMojo {

	/**
     * @component
     */
    private MavenProjectHelper projectHelper;

    private File deb;
    private File controlDir;
    
	/**
     * Main entry point
     * @throws MojoExecutionException on error
     */
    public void execute()
        throws MojoExecutionException
    {
    	if (deb == null) {
    		deb = new File(buildDirectory, "target.deb");
    	}
    	
    	if (controlDir == null) {
    		controlDir = new File("src/deb/control");
    	}
    	
    	if (!controlDir.exists() || !controlDir.isDirectory()) {
    		throw new MojoExecutionException(controlDir + " needs to be a directory");
    	}
    	
    	final File file = getProject().getArtifact().getFile();
		final File[] controlFiles = controlDir.listFiles();
		final DataProducer[] data = new DataProducer[] { new DataProducer() {
			public void produce( final DataConsumer receiver ) {
				try {
					receiver.onEachFile(new FileInputStream(file), file.getName(), "", "root", 0, "root", 0, TarEntry.DEFAULT_FILE_MODE, file.length());
				} catch (Exception e) {
					getLog().error(e);
				}
			}			
		}};

		final File changesIn = null;
    	final File changesOut = null;
    	final File keyring = null;
    	final String key = null;
    	final String passphrase = null;
		
		final Processor processor = new Processor(new Console()
		{
			public void println( final String s )
			{
				getLog().info(s);
			}			
		});
		
		try
		{

			final PackageDescriptor packageDescriptor = processor.createDeb(controlFiles, data, deb);

			getLog().info("Attaching created debian archive " + deb);
			projectHelper.attachArtifact( getProject(), "deb-archive", deb.getName(), deb );

			if (changesOut != null)
			{
				// for now only support reading the changes form a textfile provider
				final TextfileChangesProvider changesProvider = new TextfileChangesProvider(new FileInputStream(changesIn), packageDescriptor);
				
				processor.createChanges(packageDescriptor, changesProvider, (keyring!=null)?new FileInputStream(keyring):null, key, passphrase, new FileOutputStream(changesOut));

				// write the release information to this file
				changesProvider.save(new FileOutputStream(changesIn));
				
				getLog().info("Attaching created debian changes file " + changesOut);
				projectHelper.attachArtifact( getProject(), "deb-changes", changesOut.getName(), changesOut );
			}			
		}
		catch (Exception e)
		{
			getLog().error("Failed to create debian package" + deb, e);
		}    	
    }    

}

@@ -785,16 +785,62 @@
 eption;%0A
+import org.apache.maven.project.MavenProject;%0A
 import o
@@ -1468,16 +1468,434 @@
 rolDir;%0A
+%09private File changesIn = null;%0A%09private File changesOut = null;%0A%09private File keyring = null;%0A%09private String key = null;%0A%09private String passphrase = null;%0A    %0A    %0A    private static String debNameFromProject( MavenProject project ) %7B%0A    %09final StringBuffer sb = new StringBuffer();%0A    %09sb.append(project.getArtifactId()).append(%22_%22).append(project.getVersion()).append(%22.deb%22);%0A    %09return sb.toString();%0A    %7D%0A
     %0A%09/*
@@ -2110,20 +2110,40 @@
 ry, 
-%22target.deb%22
+debNameFromProject(getProject())
 );%0A 
@@ -2217,25 +2217,130 @@
 ile(
-%22src/deb/control%22
+getProject().getBasedir(), %22src/deb/control%22);%0A    %09%09getLog().info(%22Using default path to control directory %22 + controlDir
 );%0A 
@@ -2975,173 +2975,8 @@
 %7D;%0A%0A
-%09%09final File changesIn = null;%0A    %09final File changesOut = null;%0A    %09final File keyring = null;%0A    %09final String key = null;%0A    %09final String passphrase = null;%0A
 %09%09%0A%09
@@ -4157,16 +4157,17 @@
  package
+ 
 %22 + deb,

archive name