package org.codehaus.mojo.natives.c;

/*
 * The MIT License
 *
 * Copyright (c) 2004, The Codehaus
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
*/

import org.codehaus.mojo.natives.NativeBuildException;
import org.codehaus.mojo.natives.linker.AbstractLinker;
import org.codehaus.mojo.natives.linker.LinkerConfiguration;
import org.codehaus.plexus.util.cli.Commandline;

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

/**
 * @author <a href="mailto:dantran@gmail.com">Dan Tran</a>
 * @version $Id$
 */

public class CLinker 
    extends AbstractLinker
{
	
	public CLinker ()
	{
	}
		
	protected Commandline createLinkerCommandLine( List objectFiles, LinkerConfiguration config )
        throws NativeBuildException	
	{
	    Commandline cl = new Commandline();

	    cl.setWorkingDirectory( config.getWorkingDirectory().getPath() );

        if ( config.getExecutable() == null || config.getExecutable().length() == 0 )
        {
            cl.setExecutable( "gcc" );
        }
        else
        {
            cl.createArgument().setValue( config.getExecutable() );
        }

        cl.addArguments( config.getStartOptions() );
        
	    cl.createArgument().setValue( this.getLinkerOutputOption() + config.getOutputFilePath() );

	    for ( int i = 0; i < objectFiles.size(); ++i )
	    {
	    	File objFile = (File) objectFiles.get(i);

		    cl.createArgument().setValue( objFile.getPath() );
	    }

        cl.addArguments( config.getMiddleOptions() );
        
	    File [] externalLibs = config.getExternalLibraries();
	    
	    for ( int i = 0; i < externalLibs.length; ++i )
	    {
		    cl.createArgument().setValue( externalLibs[i].getPath() );
	    }

        cl.addArguments( config.getEndOptions() );
        
	    return cl;
		
	}
	
	protected String getLinkerOutputOption()
	{
		return "-o ";
	}
}

@@ -2238,47 +2238,354 @@
     
-cl.createArgument().setValue( this.getL
+String linkerOutputOption = this.getLinkerOutputOption();%0D%0A%09    if ( linkerOutputOption.endsWith(%22 %22) )%0D%0A%09    %7B%0D%0A%09    %09%09cl.createArgument().setValue( linkerOutputOption.substring( 0, linkerOutputOption.length()-1 ) );%0D%0A%09    %09%09cl.createArgument().setValue( config.getOutputFilePath() );%0D%0A%09    %7D else%0D%0A%09    %7B%0D%0A%09    %09%09cl.createArgument().setValue( l
 inke
@@ -2597,18 +2597,16 @@
 utOption
-()
  + confi
@@ -2630,16 +2630,33 @@
 th() );%0D
+%0A%09    %7D%0D%0A%09    %09%09%0D
 %0A%0D%0A%09    

add special case code for options ending in space so they are separate arguements