/*
 * Copyright 2011 DTO Solutions, Inc. (http://dtosolutions.com)
 *
 *  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.
 */

/*
* LocalFileCopier.java
* 
* User: Greg Schueler <a href="mailto:greg@dtosolutions.com">greg@dtosolutions.com</a>
* Created: 3/21/11 5:43 PM
* 
*/
package com.dtolabs.rundeck.core.execution.impl.local;

import com.dtolabs.rundeck.core.common.Framework;
import com.dtolabs.rundeck.core.common.INodeEntry;
import com.dtolabs.rundeck.core.execution.ExecutionContext;
import com.dtolabs.rundeck.core.execution.impl.common.BaseFileCopier;
import com.dtolabs.rundeck.core.execution.service.DestinationFileCopier;
import com.dtolabs.rundeck.core.execution.service.FileCopier;
import com.dtolabs.rundeck.core.execution.service.FileCopierException;

import java.io.File;
import java.io.InputStream;

/**
 * LocalFileCopier is ...
 *
 * @author Greg Schueler <a href="mailto:greg@dtosolutions.com">greg@dtosolutions.com</a>
 */
public class LocalFileCopier extends BaseFileCopier implements FileCopier, DestinationFileCopier {
    public static final String SERVICE_PROVIDER_TYPE = "local";

    public LocalFileCopier(Framework framework) {
        this.framework = framework;
    }

    public String copyScriptContent(ExecutionContext context, String script, INodeEntry node) throws
        FileCopierException {

        return copyFile(context, null, null, script, node);
    }

    private Framework framework;

    public String copyFileStream(final ExecutionContext context, InputStream input, INodeEntry node) throws
        FileCopierException {

        return copyFile(context, null, input, null, node);
    }

    public String copyFile(final ExecutionContext context, File scriptfile, INodeEntry node) throws
        FileCopierException {
        return copyFile(context, scriptfile, null, null, node);
    }

    private String copyFile(final ExecutionContext context, File scriptfile, InputStream input, String script,
                            INodeEntry node) throws FileCopierException {
        return copyFile(context, scriptfile, input, script, node, null);
    }

    private String copyFile(final ExecutionContext context, File scriptfile, InputStream input, String script,
            INodeEntry node, final String destination) throws FileCopierException {

        //write the temp file and replace tokens in the script with values from the dataContext
        if(null!=destination) {
            return writeLocalFile(scriptfile, input, script, new File(destination)).getAbsolutePath();
        }else{
            return writeScriptTempFile(context, scriptfile, input, script, node).getAbsolutePath();
        }
    }

    public String copyFileStream(ExecutionContext context, InputStream input, INodeEntry node, String destination) throws FileCopierException {
        return copyFile(context, null, input, null, node, destination);
    }

    public String copyFile(ExecutionContext context, File file, INodeEntry node, String destination) throws FileCopierException {
        return copyFile(context, file, null, null, node, destination);
    }

    public String copyScriptContent(ExecutionContext context, String script, INodeEntry node, String destination) throws FileCopierException {
        return copyFile(context, null, null, script, node, destination);
    }
}

@@ -2633,32 +2633,45 @@
 String copyFile(
+%0A            
 final ExecutionC
@@ -2677,32 +2677,44 @@
 Context context,
+%0A           
  File scriptfile
@@ -2706,32 +2706,44 @@
 File scriptfile,
+%0A           
  InputStream inp
@@ -2737,32 +2737,44 @@
 putStream input,
+%0A           
  String script,%0A
@@ -2801,16 +2801,28 @@
 ry node,
+%0A           
  final S
@@ -2830,32 +2830,37 @@
 ring destination
+%0A    
 ) throws FileCop
@@ -2863,32 +2863,36 @@
 eCopierException
+%0A   
  %7B%0A%0A        //wr
@@ -2987,134 +2987,195 @@
     
-if(null!=destination) %7B%0A            return writeLocalFile(scriptfile, input, script, new File(destination)).getAbsolutePath();
+return writeScriptTempFile(%0A                context,%0A                scriptfile,%0A                input,%0A                script,%0A                node,%0A                null != destination ?
 %0A   
@@ -3183,15 +3183,8 @@
     
-%7Delse%7B%0A
     
@@ -3195,75 +3195,73 @@
     
-return writeScriptTempFile(context, scriptfile, input, script, node
+    new File(destination) :%0A                        null%0A        
 ).ge
@@ -3281,18 +3281,8 @@
 ();%0A
-        %7D%0A
     

Fix regression #994 #993