/*
 * Copyright (C) 2007-2008 JVending Masa
 *
 * 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.jvending.masa.plugin.aapt;

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.project.MavenProject;
import org.jvending.masa.CommandExecutor;
import org.jvending.masa.ExecutionException;
import org.jvending.masa.MasaUtil;

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

/**
 * @goal package
 * @phase package
 * @description
 */
public final class AaptPackagerMojo
    extends AbstractMojo
{

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

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

    /**
     * @parameter default-value="${project.build.directory}/processed-resources"
     */
    public File resourceDirectory;

    /**
     * @parameter default-value="assets"
     */
    public File assetsDirectory;

    /**
     * @parameter
     */
    public File androidManifestFile;
 
    /**
     * @parameter
     */
    public String renameManifestPackage;

    public void execute()
        throws MojoExecutionException, MojoFailureException
    {

        CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();
        executor.setLogger( getLog() );

        if ( androidManifestFile == null )
        {
            androidManifestFile = new File(  project.getBasedir(), "AndroidManifest.xml" );
        }

        if ( !androidManifestFile.exists() )
        {
            throw new MojoExecutionException( "Android manifest file not found: File = "
                + androidManifestFile.getAbsolutePath() );
        }

        File androidJar = MasaUtil.getAndroidJarFile( project );
        if ( !androidJar.exists() )
        {
            throw new MojoExecutionException( "Android jar file not found: File = " + androidJar.getAbsolutePath() );
        }
        File outputFile = new File( project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".ap_" );

        List<String> commands = new ArrayList<String>();
        commands.add( "package" );

        commands.add( "-f" );
        commands.add( "-M" );
        commands.add( androidManifestFile.getAbsolutePath() );
        if ( resourceDirectory.exists() )
        {
            commands.add( "-S" );
            commands.add( resourceDirectory.getAbsolutePath() );
        }
        if ( assetsDirectory.exists() )
        {
            commands.add( "-A" );
            commands.add( assetsDirectory.getAbsolutePath() );
        }
        commands.add( "-I" );
        commands.add( androidJar.getAbsolutePath() );
        commands.add( "-F" );
        commands.add( outputFile.getAbsolutePath() );
        
        //    --rename-manifest-package
        if(renameManifestPackage != null) {
        	commands.add("--rename-manifest-package");
        	commands.add(renameManifestPackage);
        }

        String aaptCommand = MasaUtil.getToolnameWithPath( session, project, "aapt" );
        getLog().info( aaptCommand + " " + commands.toString() );
        try
        {
            executor.executeCommand( aaptCommand, commands, project.getBasedir(), false );
        }
        catch ( ExecutionException e )
        {
            throw new MojoExecutionException( "", e );
        }
    }
}

@@ -1865,16 +1865,112 @@
 ackage;%0D
+%0A    %0D%0A    /**%0D%0A     * @parameter%0D%0A     */%0D%0A    public boolean includeVersionCodeInApkFile;    %0D
 %0A%0D%0A    p
@@ -2814,32 +2814,42 @@
  );%0D%0A        %7D%0D%0A
+        %0D%0A
         File out
@@ -3166,32 +3166,60 @@
 olutePath() );%0D%0A
+    %09commands.add( %22-S%22 );%0D%0A
         if ( res
@@ -3286,29 +3286,75 @@
 dd( 
-%22-S%22 );%0D%0A     
+resourceDirectory.getAbsolutePath() );%0D%0A        %7D else %7B%0D%0A
        
+ %09
 comm
@@ -3363,33 +3363,57 @@
 ds.add( 
-resourceDirectory
+ new File(  project.getBasedir(), %22res%22 )
 .getAbso
@@ -3414,33 +3414,32 @@
 etAbsolutePath()
- 
 );%0D%0A        %7D%0D%0A 

Add default resource directory for packager.